Skip to main content
Gemini models can generate structured JSON output that conforms to a specific schema. This is useful for extracting structured data, creating APIs, or ensuring consistent output formats.
Don’t duplicate your schema in your input prompt or provide examples of expected JSON output. This can lower the quality of generated output.

JSON Schema Support

You can provide schemas as standard JSON schema objects:
Output:

Pydantic Model Schemas

Pydantic models provide a more Pythonic way to define schemas:
Output:

Alternative Schema Format

You can also define schemas using a simplified format:

Parsing JSON Responses

Convert JSON string responses to Python objects:

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 Optional fields 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