Documentation
    Preparing search index...

    Interface ILlmStructuredOutput<T>

    LLM structured output schema with parsing and validation utilities.

    ILlmStructuredOutput<T> is generated by typia.llm.structuredOutput<T>() to provide everything needed for handling LLM structured outputs: the JSON schema for prompting, and functions for parsing, coercing, and validating responses.

    Structured outputs allow LLMs to generate data conforming to a predefined schema instead of free-form text. This is useful for:

    • Extracting structured data from conversations
    • Generating typed responses for downstream processing
    • Ensuring consistent output formats across LLM calls

    Workflow:

    1. Pass parameters schema to LLM provider
    2. Receive LLM response (JSON string or pre-parsed object)
    3. Use parse for raw strings or coerce for pre-parsed objects
    4. Use validate to check the result and get detailed errors

    Jeongho Nam - https://github.com/samchon

    interface ILlmStructuredOutput<T = unknown> {
        coerce: (input: unknown) => T;
        parameters: ILlmSchema.IParameters;
        parse: (input: string) => IJsonParseResult<T>;
        validate: (input: unknown) => IValidation<T>;
    }

    Type Parameters

    • T = unknown

      The expected output type

    Index

    Properties

    coerce: (input: unknown) => T

    Coerce pre-parsed output to match expected schema types.

    Use this only when the SDK already parses JSON internally. For raw JSON strings from LLM output, use parse instead — it handles both lenient parsing and type coercion in one step.

    LLMs often return values with incorrect types even after parsing:

    • "42"42 (when schema expects number)
    • "true"true (when schema expects boolean)
    • "null"null (when schema expects null)
    • "{...}"{...} (when schema expects object)
    • "[...]"[...] (when schema expects array)

    This function recursively coerces these double-stringified values based on the parameters schema.

    Type validation is NOT performed — use validate after coercion.

    Type Declaration

      • (input: unknown): T
      • Parameters

        • input: unknown

          Pre-parsed output object from SDK

        Returns T

        Coerced output with corrected types

    JSON schema for the structured output.

    Pass this schema to LLM providers (OpenAI, Anthropic, Google, etc.) to constrain the output format. The schema includes $defs for shared type definitions and properties for the output structure.

    Most LLM providers accept this directly in their structured output or response format configuration.

    parse: (input: string) => IJsonParseResult<T>

    Lenient JSON parser with schema-based type coercion.

    Handles incomplete or malformed JSON commonly produced by LLMs:

    • Unclosed brackets, strings, trailing commas
    • JavaScript-style comments (// and multi-line)
    • Unquoted object keys, incomplete keywords (tru, fal, nul)
    • Markdown code block extraction, junk prefix skipping

    Also coerces double-stringified values based on the schema:

    • "42"42 (when schema expects number)
    • "true"true (when schema expects boolean)
    • "null"null (when schema expects null)
    • "{...}"{...} (when schema expects object)
    • "[...]"[...] (when schema expects array)

    Type validation is NOT performed — use validate after parsing.

    If the SDK (e.g., LangChain, Vercel AI, MCP) already parses JSON internally and provides a pre-parsed object, use coerce instead.

    Type Declaration

      • (input: string): IJsonParseResult<T>
      • Parameters

        • input: string

          Raw JSON string from LLM output

        Returns IJsonParseResult<T>

        Parse result with data on success, or partial data with errors

    validate: (input: unknown) => IValidation<T>

    Validates LLM-generated output against the schema.

    LLMs frequently make type errors such as returning strings instead of numbers or missing required properties. Use this validator to check output before further processing.

    When validation fails, use LlmJson.stringify from @typia/utils to format the error for LLM feedback. The formatted output shows the invalid JSON with inline error comments, helping the LLM understand and correct its mistakes in the next turn.

    Type Declaration

      • (input: unknown): IValidation<T>
      • Parameters

        • input: unknown

          The output generated by the LLM

        Returns IValidation<T>

        Validation result with success status and any errors