feat(typia): new functions llm.parse() and llm.coerce().#1771
feat(typia): new functions llm.parse() and llm.coerce().#1771
llm.parse() and llm.coerce().#1771Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds transformer-backed typia.llm.parse() / typia.llm.coerce() (plus createParse() / createCoerce()) to enable schema-driven lenient JSON parsing and type coercion of LLM arguments, and wires the new functionality through the core programmers and transform pipeline.
Changes:
- Introduces new LLM programmers (
LlmParseProgrammer,LlmCoerceProgrammer) and corresponding transformers for call-expression rewriting. - Adds runtime internal helpers (
_parseLlmArguments,_coerceLlmArguments) delegating to@typia/utilsLlmJson. - Adds schema-level tests and generator inputs exercising the new public APIs.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test-typia-schema/src/features/llm.parse/test_llm_parse_object.ts | Adds schema-package test coverage for typia.llm.parse() success path. |
| tests/test-typia-schema/src/features/llm.parse/test_llm_parse_coercion.ts | Adds schema-package test coverage for parse-time coercion (string → number/boolean). |
| tests/test-typia-schema/src/features/llm.parse/test_llm_createParse_object.ts | Adds schema-package test coverage for typia.llm.createParse(). |
| tests/test-typia-schema/src/features/llm.coerce/test_llm_createCoerce_object.ts | Adds schema-package test coverage for typia.llm.createCoerce(). |
| tests/test-typia-schema/src/features/llm.coerce/test_llm_coerce_object.ts | Adds schema-package test coverage for typia.llm.coerce(). |
| tests/test-typia-generate/src/input/generate_llm.ts | Extends generator input to include llm.parse/coerce APIs. |
| packages/utils/src/utils/internal/parseLenientJson.ts | Reorders/annotates lenient JSON parser with expanded documentation. |
| packages/utils/src/utils/internal/coerceLlmArguments.ts | Widens coercion entrypoint parameter to unknown. |
| packages/utils/src/utils/LlmJson.ts | Widens LlmJson.coerce() input to unknown; keeps parse+optional coercion behavior. |
| packages/typia/src/llm.ts | Exposes new public llm.parse/coerce/createParse/createCoerce APIs and types. |
| packages/typia/src/internal/_parseLlmArguments.ts | Adds internal runtime delegate for transformer-emitted parse calls. |
| packages/typia/src/internal/_coerceLlmArguments.ts | Adds internal runtime delegate for transformer-emitted coerce calls. |
| packages/transform/src/features/llm/LlmSchemaTransformer.ts | Adjusts schema emission typing via satisfies + as pattern. |
| packages/transform/src/features/llm/LlmParseTransformer.ts | Adds transformer for typia.llm.parse<T>() call rewriting. |
| packages/transform/src/features/llm/LlmParametersTransformer.ts | Adjusts parameters emission typing via satisfies + as pattern. |
| packages/transform/src/features/llm/LlmCreateParseTransformer.ts | Adds transformer for typia.llm.createParse<T>() factory rewriting. |
| packages/transform/src/features/llm/LlmCreateCoerceTransformer.ts | Adds transformer for typia.llm.createCoerce<T>() factory rewriting. |
| packages/transform/src/features/llm/LlmControllerTransformer.ts | Adjusts controller emission typing via satisfies + as pattern. |
| packages/transform/src/features/llm/LlmCoerceTransformer.ts | Adds transformer for typia.llm.coerce<T>() call rewriting. |
| packages/transform/src/features/llm/LlmApplicationTransformer.ts | Adjusts application emission typing via satisfies + as pattern. |
| packages/transform/src/CallExpressionTransformer.ts | Registers new LLM transformers under the llm.* call map. |
| packages/core/src/programmers/llm/index.ts | Exports the new LLM programmers. |
| packages/core/src/programmers/llm/LlmParseProgrammer.ts | Implements schema generation + code emission for parse wrappers. |
| packages/core/src/programmers/llm/LlmCoerceProgrammer.ts | Implements schema generation + code emission for coerce wrappers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| export function coerce< | ||
| Parameters extends Record<string, any>, | ||
| Config extends Partial<ILlmSchema.IConfig> = {}, | ||
| >(input: Parameters): Parameters; |
There was a problem hiding this comment.
llm.coerce() is intended to fix incorrectly-typed values from an SDK/LLM, but the public signature currently requires input: Parameters, which forces callers to cast malformed data to Parameters before coercion (defeating the purpose and conflicting with the JSDoc describing wrong types). Consider changing the signature to accept input: unknown (while still requiring the explicit generic) and return Parameters.
| >(input: Parameters): Parameters; | |
| >(input: unknown): Parameters; |
| export function createCoerce< | ||
| Parameters extends Record<string, any>, | ||
| Config extends Partial<ILlmSchema.IConfig> = {}, | ||
| >(): (input: Parameters) => Parameters; |
There was a problem hiding this comment.
createCoerce() currently returns a function typed as (input: Parameters) => Parameters, which again forces callers to pre-cast unknown/malformed SDK objects to Parameters before coercion. To make the coercer usable with real LLM outputs, consider returning (input: unknown) => Parameters (while keeping the output typed).
| >(): (input: Parameters) => Parameters; | |
| >(): (input: unknown) => Parameters; |
| [ | ||
| IdentifierFactory.parameter( | ||
| "input", | ||
| ts.factory.createTypeReferenceNode(typeName), |
There was a problem hiding this comment.
The generated coercer currently types its parameter as the target type (input: <TypeName>). That makes the emitted function awkward to use for coercing untyped/incorrect SDK data (callers must cast first). Consider changing the parameter type to unknown and keeping only the return type as <TypeName> so callers can pass raw values and still get a typed result.
| ts.factory.createTypeReferenceNode(typeName), | |
| ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword), |
This pull request introduces new functionality for handling LLM (Large Language Model) argument parsing and coercion within the codebase. It adds two new programmers (
LlmCoerceProgrammerandLlmParseProgrammer) and their corresponding transformers, enabling type-safe parsing and coercion of LLM arguments. The changes also integrate these features into the transformer infrastructure and expose them through the public API.New LLM argument parsing and coercion features:
LlmCoerceProgrammerandLlmParseProgrammerfor generating code to coerce and parse LLM arguments based on TypeScript types, including schema generation, validation, and code emission. [1] [2]_coerceLlmArgumentsand_parseLlmArgumentsfor runtime handling of LLM argument coercion and parsing, delegating toLlmJson. [1] [2]Transformer infrastructure integration:
LlmCoerceTransformer,LlmCreateCoerceTransformer,LlmParseTransformer, andLlmCreateParseTransformer, which validate types, extract configuration, and emit code using the new programmers. [1] [2] [3] [4]CallExpressionTransformerinfrastructure, enabling their use via function calls in user code. [1] [2]Public API exposure:
LlmCoerceProgrammerandLlmParseProgrammerfrom the LLM programmers index, making them available for external use.llm.tsto includeIJsonParseResultfor LLM parsing results.