Structured Output
Gemini JSON Mode API - Generate validated JSON with Gemini Pro. Schema-based structured output for reliable data extraction and type-safe AI responses.
Structured Output (JSON Mode)
Generate structured data output from Gemini models by defining a JSON schema. This is useful for extracting data, building APIs, or any scenario where you need consistent output format.
Supported Models: All Gemini 1.5+ models support structured output.
How It Works
Set responseMimeType to application/json and provide a responseSchema to define the expected output structure.
Basic Example
curl -X POST "https://api.uniterouter.ai/v1beta/models/gemini-2.0-flash:generateContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $UNITEROUTER_API_KEY" \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "List 3 popular programming languages with their use cases"}]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"useCase": {"type": "string"},
"difficulty": {"type": "string", "enum": ["easy", "medium", "hard"]}
},
"required": ["name", "useCase", "difficulty"]
}
}
}
}'import google.generativeai as genai
genai.configure(
api_key="your_uniterouter_api_key",
transport="rest",
client_options={"api_endpoint": "api.uniterouter.ai"}
)
model = genai.GenerativeModel("gemini-2.0-flash")
response = model.generate_content(
"List 3 popular programming languages with their use cases",
generation_config=genai.GenerationConfig(
response_mime_type="application/json",
response_schema={
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"useCase": {"type": "string"},
"difficulty": {"type": "string", "enum": ["easy", "medium", "hard"]}
},
"required": ["name", "useCase", "difficulty"]
}
}
)
)
import json
data = json.loads(response.text)
print(data)import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("your_uniterouter_api_key", {
baseUrl: "https://api.uniterouter.ai"
});
const model = genAI.getGenerativeModel({
model: "gemini-2.0-flash",
generationConfig: {
responseMimeType: "application/json",
responseSchema: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.OBJECT,
properties: {
name: { type: SchemaType.STRING },
useCase: { type: SchemaType.STRING },
difficulty: { type: SchemaType.STRING, enum: ["easy", "medium", "hard"] }
},
required: ["name", "useCase", "difficulty"]
}
}
}
});
const result = await model.generateContent(
"List 3 popular programming languages with their use cases"
);
const data = JSON.parse(result.response.text());
console.log(data);Response
[
{
"name": "Python",
"useCase": "Data science, machine learning, web development",
"difficulty": "easy"
},
{
"name": "JavaScript",
"useCase": "Web development, frontend and backend",
"difficulty": "medium"
},
{
"name": "Rust",
"useCase": "Systems programming, performance-critical applications",
"difficulty": "hard"
}
]GenerationConfig Parameters
| Parameter | Type | Description |
|---|---|---|
responseMimeType | string | Must be application/json for structured output |
responseSchema | object | OpenAPI-compatible JSON Schema |
responseJsonSchema | object | Alternative: full JSON Schema support |
Supported Schema Properties
| Property | Description |
|---|---|
type | object, array, string, number, integer, boolean |
properties | Object properties definition |
items | Array item schema |
required | Required property names |
enum | Allowed values for strings/numbers |
minimum / maximum | Number constraints |
minItems / maxItems | Array length constraints |
description | Field description (helps the model) |
Enum Mode
For simple classification tasks, use text/x.enum:
response = model.generate_content(
"What is the sentiment of: 'I love this product!'",
generation_config=genai.GenerationConfig(
response_mime_type="text/x.enum",
response_schema={
"type": "string",
"enum": ["positive", "negative", "neutral"]
}
)
)
print(response.text) # "positive"Best Practices
- Use descriptions - Add
descriptionto schema properties to guide the model - Keep schemas simple - Complex nested schemas may reduce accuracy
- Validate output - Always validate the JSON output matches your schema
- Use enums - For categorical data, use enums to constrain outputs
Related
- Generate Content - Basic text generation
- Function Calling - Tool use
How is this guide?
Image Generation (Nano Banana)
Gemini Image Generation API - Create and edit images with Imagen 3. Native multimodal image generation through UniterRouter, supports inpainting and outpainting.
Extended Thinking
Gemini Thinking API - Advanced reasoning with Gemini 2.0 Flash Thinking. Chain-of-thought reasoning for complex math, coding, and analysis problems.