Generate Structured Objects
UsegenerateObject() to get validated JSON responses:
Stream Structured Objects
Stream JSON as it’s being generated: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 validated. Use object-delta events for progressive UI updates while JSON is still streaming.Complex Schema Examples
- Nested Objects
- Arrays
- Unions and Discriminated Types
- Optional and Default Values
Schema Descriptions
Add descriptions to help the model understand your schema:Type Inference
TypeScript automatically infers types from Zod schemas:Error Handling
Handle validation and parsing errors: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 custom validation after generation:
Next Steps
Chat Completion
Generate text responses without structure
Tool Calling
Combine structured outputs with tool calls
Streaming
Stream regular text responses
Zod Documentation
Learn more about Zod schemas