Structured outputs

You can configure Gemini models to generate responses that adhere to a provided JSON Schema. This ensures predictable, type-safe results and simplifies extracting structured data from unstructured text.

Using structured outputs is ideal for:

  • Data extraction: Pull specific information like names and dates from text.
  • Structured classification: Classify text into predefined categories.
  • Agentic workflows: Generate structured inputs for tools or APIs.

In addition to supporting JSON Schema in the REST API, the Google GenAI SDKs allow defining schemas using Pydantic (Python) and Zod (JavaScript).

Structured output examples

Recipe Extractor

This example demonstrates how to extract structured data from text using basic JSON Schema types like object, array, string, and integer.

Python

from google import genai
from pydantic import BaseModel, Field
from typing import List, Optional

class Ingredient(BaseModel):
    name: str = Field(description="Name of the ingredient.")
    quantity: str = Field(description="Quantity of the ingredient, including units.")

class Recipe(BaseModel):
    recipe_name: str = Field(description