Generate Structured Objects
UsegenerateObject() to get validated JSON responses:
Stream Structured Objects
Stream JSON as it’s being generated:Schemas
schema accepts two kinds of schemas. Tool parameters accept the same two kinds.
core-ai converts a Standard Schema with its
~standard.jsonSchema.input() method (draft 2020-12), once per schema object. Because the input type is converted, transforms such as z.string().transform(Number) work, and object holds their output. A schema that cannot be converted, such as one containing z.date(), throws a ValidationError before any provider call. Call toJsonSchema() to see the JSON Schema core-ai sends.
Supported libraries
Libraries without Standard JSON Schema support, such as TypeBox, Yup, Superstruct, io-ts,
zod/mini, and Zod before 4.2, work through plain JSON Schema. Pass the JSON Schema they produce, for example z.toJSONSchema(schema) for an older Zod version, and validate the result with the library yourself.
Plain JSON Schema
core-ai uses plain JSON Schema as is and never validates the output against it.object is the parsed JSON, typed unknown:
Object stream events
ThestreamObject() function returns an ObjectStream — an async iterable with .result and .events. It emits these event types:
The
object event is emitted once, when the full object payload has been parsed and, for a Standard Schema, validated. object-delta events carry raw, unvalidated text. Use them for progressive UI updates while JSON is still streaming.Complex Schema Examples
OpenAI and Anthropic structured outputs accept only closed objects, so they reject open objects such as
z.record() or .catchall(). Model key/value data as an array of objects instead.- Nested Objects
- Arrays
- Unions and Discriminated Types
- Optional and Default Values
Schema Descriptions
Add descriptions to help the model understand your schema:Type Inference
result.object is typed with the Standard Schema’s output type (InferSchemaOutput<TSchema>). For plain JSON Schema it is unknown.
Error Handling
generateObject() and streamObject() throw these structured output errors:
StructuredOutputValidationErrorwhen the output does not match a Standard Schema.issueslists the schema library’s messages aspath.to.key: message. When the schema’s validator throws instead, such as a transform callingnew URL(), its error is thecause.StructuredOutputParseErrorwhen the output is not valid JSON.StructuredOutputNoObjectGeneratedErrorwhen the model returns no object.
Streaming with UI Updates
Update your UI as the object is built:Configuration Options
Customize structured output generation:Best Practices
Use descriptive schema names and descriptions
Use descriptive schema names and descriptions
Help the model understand what you want:
Use lower temperature for consistent structure
Use lower temperature for consistent structure
Reduce temperature for more predictable JSON structure:
Use object-delta for progressive updates
Use object-delta for progressive updates
Build previews from raw JSON chunks, then switch to the validated object when it arrives:
Validate complex constraints
Validate complex constraints
Add validation for constraints the schema does not express:
Next Steps
Chat Completion
Generate text responses without structure
Tool Calling
Combine structured outputs with tool calls
Streaming
Stream regular text responses
Standard Schema
Find schema libraries that implement Standard Schema