strategies for forcing Gemini to output valid structured data (JSON/XML). Use this for data extraction, API integrations, and creating machine-readable responses.
Generate reliable, machine-readable output (JSON/XML) for application integration, minimizing parsing errors and hallucinations.
"Classify the following review. Return valid JSON only: { 'sentiment': 'POSITIVE' | 'NEGATIVE', 'score': number }".
release_date), data types (e.g., string, float), and descriptions, helping the model understand relationships between data points.{
"type": "object",
"properties": {
"name": { "type": "string", "description": "Product name" },
"price": { "type": "number", "format": "float" }
}
}
max_tokens setting is high enough for the data structure.json-repair in your application pipeline to automatically close broken tags and fix malformed objects.| Task Type | Recommended Format |
|---|---|
| Creative Writing | Plain Text / Markdown |
| Data Extraction | JSON |
| Categorization/Ranking | JSON |
| Code Generation | Markdown Code Blocks |