JSON Schema Support
You can provide schemas as standard JSON schema objects:Pydantic Model Schemas
Pydantic models provide a more Pythonic way to define schemas:Alternative Schema Format
You can also define schemas using a simplified format:Parsing JSON Responses
Convert JSON string responses to Python objects:- JSON Module
- Pydantic Model
Complex Schema Examples
Nested Objects
Arrays and Enums
Data Extraction Use Cases
Extract Information from Text
Resume Parser
JSON with Streaming
You can stream JSON responses, though the response won’t be valid JSON until complete:Schema Type Reference
Use Cases
API Backends
Generate structured responses for REST APIs
Data Extraction
Extract structured data from unstructured text
Form Generation
Generate form data in consistent formats
Report Generation
Create structured reports and summaries
Best Practices
- Always set
response_mime_type='application/json'when using schemas - Use Pydantic models for type safety and validation
- Don’t include schema examples in your prompt
- Keep schemas simple and focused on essential fields
- Use
Optionalfields for data that might not always be present - Validate parsed JSON against your schema
- Use enums to constrain string values to specific options
- Test your schemas with various inputs
- Handle parsing errors gracefully
- For streaming, only parse JSON after receiving the complete response