Ensure 100% Reliable Data Formats with Gemini JSON Schemas
Force Gemini to output perfectly formatted JSON using strict schemas.
Use Gemini's Controlled Generation feature to define a JSON schema that the model must follow, ensuring data is ready for programmatic use without manual cleaning.
The Scenario
You are building a tool that needs to extract product names and categories from a raw list of text and pipe them directly into a database or spreadsheet without manual data entry.
Before & after
Previously, you would prompt the model and hope for JSON, then spend 10–15 minutes manually fixing syntax errors or unmasking 'hallucinated' fields.
Using Gemini's controlled generation, you define a JSON schema to force the output format. The model generates valid data in 1–2 minutes, eliminating manual cleanup.
The Prompt
Produce a list of [NUMBER] [ITEMS] in valid JSON format. Follow this schema strictly:
{"type": "object", "properties": {"items": {"type": "array", "items": {"type": "object", "properties": {"name": {"type": "string"}, "category": {"type": "string"}}}}}}Gemini 1.5 Pro and Flash now support 'Controlled Generation,' which allows developers to provide a JSON schema. This ensures the model's output strictly follows the structure you define, making it ready for immediate use in code or spreadsheets.
Source
Release notes | Gemini API | Google AI for Developers"Introduced controlled generation which allows forcing the model to output specific formats like JSON via schema."
