@typia/interface 12.0.0-dev.20260307 → 12.0.0-dev.20260309

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,6 +83,6 @@ export declare namespace ILlmApplication {
83
83
  * @ignore
84
84
  */
85
85
  interface __IPrimitive<Class extends object = any> extends Omit<ILlmApplication<Class>, "config" | "functions"> {
86
- functions: Omit<ILlmFunction, "parse">[];
86
+ functions: Omit<ILlmFunction, "parse" | "coerce">[];
87
87
  }
88
88
  }
@@ -1,3 +1,4 @@
1
+ import { tags } from "..";
1
2
  import { IJsonParseResult } from "./IJsonParseResult";
2
3
  import { ILlmSchema } from "./ILlmSchema";
3
4
  import { IValidation } from "./IValidation";
@@ -20,11 +21,11 @@ export interface ILlmFunction {
20
21
  * Function name for LLM invocation.
21
22
  *
22
23
  * The identifier used by the LLM to call this function. Must be unique within
23
- * the application. OpenAI limits function names to 64 characters.
24
+ * the application.
24
25
  *
25
- * @maxLength 64
26
+ * OpenAI limits function names to 64 characters.
26
27
  */
27
- name: string;
28
+ name: string & tags.MaxLength<64>;
28
29
  /**
29
30
  * Schema for function parameters.
30
31
  *
@@ -82,10 +83,39 @@ export interface ILlmFunction {
82
83
  *
83
84
  * Type validation is NOT performed — use {@link validate} after parsing.
84
85
  *
86
+ * If the SDK (e.g., LangChain, Vercel AI, MCP) already parses JSON internally
87
+ * and provides a pre-parsed object, use {@link coerce} instead to apply
88
+ * schema-based type coercion without re-parsing.
89
+ *
85
90
  * @param str Raw JSON string from LLM output
86
91
  * @returns Parse result with data on success, or partial data with errors
87
92
  */
88
93
  parse: (str: string) => IJsonParseResult<unknown>;
94
+ /**
95
+ * Coerce pre-parsed arguments to match expected schema types.
96
+ *
97
+ * **Use this only when the SDK (e.g., LangChain, Vercel AI, MCP) already
98
+ * parses JSON internally.** For raw JSON strings from LLM output, use
99
+ * {@link parse} instead — it handles both lenient parsing and type coercion in
100
+ * one step.
101
+ *
102
+ * LLMs often return values with incorrect types even after parsing:
103
+ *
104
+ * - `"42"` → `42` (when schema expects number)
105
+ * - `"true"` → `true` (when schema expects boolean)
106
+ * - `"null"` → `null` (when schema expects null)
107
+ * - `"{...}"` → `{...}` (when schema expects object)
108
+ * - `"[...]"` → `[...]` (when schema expects array)
109
+ *
110
+ * This function recursively coerces these double-stringified values based on
111
+ * the {@link parameters} schema.
112
+ *
113
+ * Type validation is NOT performed — use {@link validate} after coercion.
114
+ *
115
+ * @param data Pre-parsed arguments object from SDK
116
+ * @returns Coerced arguments with corrected types
117
+ */
118
+ coerce: (data: unknown) => unknown;
89
119
  /**
90
120
  * Validates LLM-generated arguments against the schema.
91
121
  *
@@ -93,10 +123,10 @@ export interface ILlmFunction {
93
123
  * numbers or missing required properties. Use this validator to check
94
124
  * arguments before execution.
95
125
  *
96
- * When validation fails, use `stringifyValidationFailure()` from
97
- * `@typia/utils` to format the error for LLM feedback. The formatted output
98
- * shows the invalid JSON with inline error comments, helping the LLM
99
- * understand and correct its mistakes in the next turn.
126
+ * When validation fails, use {@link LlmJson.stringify} from `@typia/utils` to
127
+ * format the error for LLM feedback. The formatted output shows the invalid
128
+ * JSON with inline error comments, helping the LLM understand and correct its
129
+ * mistakes in the next turn.
100
130
  *
101
131
  * @param args The arguments generated by the LLM
102
132
  * @returns Validation result with success status and any errors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typia/interface",
3
- "version": "12.0.0-dev.20260307",
3
+ "version": "12.0.0-dev.20260309",
4
4
  "description": "Superfast runtime validators with only one line",
5
5
  "main": "lib/index.js",
6
6
  "exports": {
@@ -94,6 +94,6 @@ export namespace ILlmApplication {
94
94
  ILlmApplication<Class>,
95
95
  "config" | "functions"
96
96
  > {
97
- functions: Omit<ILlmFunction, "parse">[];
97
+ functions: Omit<ILlmFunction, "parse" | "coerce">[];
98
98
  }
99
99
  }
@@ -1,3 +1,4 @@
1
+ import { tags } from "..";
1
2
  import { IJsonParseResult } from "./IJsonParseResult";
2
3
  import { ILlmSchema } from "./ILlmSchema";
3
4
  import { IValidation } from "./IValidation";
@@ -21,11 +22,11 @@ export interface ILlmFunction {
21
22
  * Function name for LLM invocation.
22
23
  *
23
24
  * The identifier used by the LLM to call this function. Must be unique within
24
- * the application. OpenAI limits function names to 64 characters.
25
+ * the application.
25
26
  *
26
- * @maxLength 64
27
+ * OpenAI limits function names to 64 characters.
27
28
  */
28
- name: string;
29
+ name: string & tags.MaxLength<64>;
29
30
 
30
31
  /**
31
32
  * Schema for function parameters.
@@ -89,11 +90,41 @@ export interface ILlmFunction {
89
90
  *
90
91
  * Type validation is NOT performed — use {@link validate} after parsing.
91
92
  *
93
+ * If the SDK (e.g., LangChain, Vercel AI, MCP) already parses JSON internally
94
+ * and provides a pre-parsed object, use {@link coerce} instead to apply
95
+ * schema-based type coercion without re-parsing.
96
+ *
92
97
  * @param str Raw JSON string from LLM output
93
98
  * @returns Parse result with data on success, or partial data with errors
94
99
  */
95
100
  parse: (str: string) => IJsonParseResult<unknown>;
96
101
 
102
+ /**
103
+ * Coerce pre-parsed arguments to match expected schema types.
104
+ *
105
+ * **Use this only when the SDK (e.g., LangChain, Vercel AI, MCP) already
106
+ * parses JSON internally.** For raw JSON strings from LLM output, use
107
+ * {@link parse} instead — it handles both lenient parsing and type coercion in
108
+ * one step.
109
+ *
110
+ * LLMs often return values with incorrect types even after parsing:
111
+ *
112
+ * - `"42"` → `42` (when schema expects number)
113
+ * - `"true"` → `true` (when schema expects boolean)
114
+ * - `"null"` → `null` (when schema expects null)
115
+ * - `"{...}"` → `{...}` (when schema expects object)
116
+ * - `"[...]"` → `[...]` (when schema expects array)
117
+ *
118
+ * This function recursively coerces these double-stringified values based on
119
+ * the {@link parameters} schema.
120
+ *
121
+ * Type validation is NOT performed — use {@link validate} after coercion.
122
+ *
123
+ * @param data Pre-parsed arguments object from SDK
124
+ * @returns Coerced arguments with corrected types
125
+ */
126
+ coerce: (data: unknown) => unknown;
127
+
97
128
  /**
98
129
  * Validates LLM-generated arguments against the schema.
99
130
  *
@@ -101,10 +132,10 @@ export interface ILlmFunction {
101
132
  * numbers or missing required properties. Use this validator to check
102
133
  * arguments before execution.
103
134
  *
104
- * When validation fails, use `stringifyValidationFailure()` from
105
- * `@typia/utils` to format the error for LLM feedback. The formatted output
106
- * shows the invalid JSON with inline error comments, helping the LLM
107
- * understand and correct its mistakes in the next turn.
135
+ * When validation fails, use {@link LlmJson.stringify} from `@typia/utils` to
136
+ * format the error for LLM feedback. The formatted output shows the invalid
137
+ * JSON with inline error comments, helping the LLM understand and correct its
138
+ * mistakes in the next turn.
108
139
  *
109
140
  * @param args The arguments generated by the LLM
110
141
  * @returns Validation result with success status and any errors