Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

ResponseFormat.md

149 lines3.2 KB

1---
2id: ResponseFormat
3title: ResponseFormat
4---
5​
6Defined in: [packages/ai/src/types.ts:1123](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1123)
7​
8Structured output format specification.
9​
10Constrains the model's output to match a specific JSON structure.
11Useful for extracting structured data, form filling, or ensuring consistent response formats.
12​
13## See
14​
15 - https://platform.openai.com/docs/guides/structured-outputs
16 - https://sdk.vercel.ai/docs/ai-sdk-core/structured-outputs
17​
18## Type Parameters
19​
20### TData
21​
22`TData` = `any`
23​
24TypeScript type of the expected data structure (for type safety)
25​
26## Properties
27​
28### \_\_data?
29​
30```ts
31optional __data?: TData;
32```
33​
34Defined in: [packages/ai/src/types.ts:1201](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1201)
35​
36**`Internal`**
37​
38Type-only property to carry the inferred data type.
39​
40This is never set at runtime - it only exists for TypeScript type inference.
41Allows the SDK to know what type to expect when parsing the response.
42​
43***
44​
45### json\_schema?
46​
47```ts
48optional json_schema?: object;
49```
50​
51Defined in: [packages/ai/src/types.ts:1140](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1140)
52​
53JSON schema specification (required when type is "json_schema").
54​
55Defines the exact structure the model's output must conform to.
56OpenAI's structured outputs will guarantee the output matches this schema.
57​
58#### description?
59​
60```ts
61optional description?: string;
62```
63​
64Optional description of what the schema represents.
65​
66Helps document the purpose of this structured output.
67​
68##### Example
69​
70```ts
71"User profile information including name, email, and preferences"
72```
73​
74#### name
75​
76```ts
77name: string;
78```
79​
80Unique name for the schema.
81​
82Used to identify the schema in logs and debugging.
83Should be descriptive (e.g., "user_profile", "search_results").
84​
85#### schema
86​
87```ts
88schema: Record<string, any>;
89```
90​
91JSON Schema definition for the expected output structure.
92​
93Must be a valid JSON Schema (draft 2020-12 or compatible).
94The model's output will be validated against this schema.
95​
96##### See
97​
98https://json-schema.org/
99​
100##### Example
101​
102```ts
104 * type: "object",
105 * properties: {
106 * name: { type: "string" },
107 * age: { type: "number" },
108 * email: { type: "string", format: "email" }
109 * },
110 * required: ["name", "email"],
111 * additionalProperties: false
112 * }
113```
114​
115#### strict?
116​
117```ts
118optional strict?: boolean;
119```
120​
121Whether to enforce strict schema validation.
122​
123When true (recommended), the model guarantees output will match the schema exactly.
124When false, the model will "best effort" match the schema.
125​
126Default: true (for providers that support it)
127​
128##### See
129​
130https://platform.openai.com/docs/guides/structured-outputs#strict-mode
131​
132***
133​
134### type
135​
136```ts
137type: "json_object" | "json_schema";
138```
139​
140Defined in: [packages/ai/src/types.ts:1132](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1132)
141​
142Type of structured output.
143​
144- "json_object": Forces the model to output valid JSON (any structure)
145- "json_schema": Validates output against a provided JSON Schema (strict structure)
146​
147#### See
148​
149https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format