gemini-docs-dbfiles / docstore /0300c86f-a947-4a28-b0cf-f90d70d7eda8
arshjaved's picture
Upload all project files and folders
2e9b585 verified
Structured output | Gemini API | Google AI for Developers Skip to main content / English Deutsch Español – América Latina Français Indonesia Italiano Polski Português – Brasil Shqip Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 中文 – 繁體 日本語 한국어 Sign in Introducing Batch Mode, with higher rate limits and a 50% token discount. Learn more Home Gemini API Models Send feedback Structured output You can configure Gemini for structured output instead of unstructured text, allowing precise extraction and standardization of information for further processing. For example, you can use structured output to extract information from resumes, standardize them to build a structured database. Gemini can generate either JSON or enum values as structured output. Generating JSON There are two ways to generate JSON using the Gemini API: Configure a schema on the model Provide a schema in a text prompt Configuring a schema on the model is the recommended way to generate JSON, because it constrains the model to output JSON. Configuring a schema (recommended) To constrain the model to generate JSON, configure a responseSchema . The model will then respond to any prompt with JSON-formatted output. Python from google import genai from pydantic import BaseModel class Recipe ( BaseModel ): recipe_name : str ingredients : list [ str ] client = genai . Client () response = client . models . generate_content ( model = "gemini-2.5-flash" , contents = "List a few popular cookie recipes, and include the amounts of ingredients." , config = { "response_mime_type" : "application/json" , "response_schema" : list [ Recipe ], }, ) # Use the response as a JSON string. print ( response . text ) # Use instantiated objects. my_recipes : list [ Recipe ] = response . parsed Note: Pydantic validators are not yet supported. If a pydantic.ValidationError occurs, it is suppressed, and .parsed may be empty/null. JavaScript import { GoogleGenAI , Type } from "@google/genai" ; const ai =