Skip to content

feat(typia): new functions llm.parse() and llm.coerce().#1771

Merged
samchon merged 2 commits intomasterfrom
feat/llm.ts
Mar 7, 2026
Merged

feat(typia): new functions llm.parse() and llm.coerce().#1771
samchon merged 2 commits intomasterfrom
feat/llm.ts

Conversation

@samchon
Copy link
Owner

@samchon samchon commented Mar 7, 2026

This pull request introduces new functionality for handling LLM (Large Language Model) argument parsing and coercion within the codebase. It adds two new programmers (LlmCoerceProgrammer and LlmParseProgrammer) 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:

  • Added LlmCoerceProgrammer and LlmParseProgrammer for generating code to coerce and parse LLM arguments based on TypeScript types, including schema generation, validation, and code emission. [1] [2]
  • Implemented internal functions _coerceLlmArguments and _parseLlmArguments for runtime handling of LLM argument coercion and parsing, delegating to LlmJson. [1] [2]

Transformer infrastructure integration:

  • Added new transformers: LlmCoerceTransformer, LlmCreateCoerceTransformer, LlmParseTransformer, and LlmCreateParseTransformer, which validate types, extract configuration, and emit code using the new programmers. [1] [2] [3] [4]
  • Registered the new LLM transformers in the CallExpressionTransformer infrastructure, enabling their use via function calls in user code. [1] [2]

Public API exposure:

  • Exported LlmCoerceProgrammer and LlmParseProgrammer from the LLM programmers index, making them available for external use.
  • Updated imports in llm.ts to include IJsonParseResult for LLM parsing results.

@samchon samchon self-assigned this Mar 7, 2026
@samchon samchon added documentation Improvements or additions to documentation enhancement New feature or request labels Mar 7, 2026
@samchon samchon marked this pull request as ready for review March 7, 2026 17:52
Copilot AI review requested due to automatic review settings March 7, 2026 17:52
@samchon samchon merged commit e791620 into master Mar 7, 2026
7 checks passed
@samchon samchon deleted the feat/llm.ts branch March 7, 2026 17:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/utils LlmJson.
  • 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;
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
>(input: Parameters): Parameters;
>(input: unknown): Parameters;

Copilot uses AI. Check for mistakes.
export function createCoerce<
Parameters extends Record<string, any>,
Config extends Partial<ILlmSchema.IConfig> = {},
>(): (input: Parameters) => Parameters;
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
>(): (input: Parameters) => Parameters;
>(): (input: unknown) => Parameters;

Copilot uses AI. Check for mistakes.
[
IdentifierFactory.parameter(
"input",
ts.factory.createTypeReferenceNode(typeName),
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
ts.factory.createTypeReferenceNode(typeName),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants