Gemini Controlled Generation (response_schema) Error
You're using response_mime_type + response_schema to force JSON output but hitting schema errors. Gemini's controlled generation shares constraints with function calling (OpenAPI 3.0 subset). Fix by using supported types, uppercase type names, and property_ordering for stable field order.
Set response_mime_type="application/json" AND provide response_schema — both together for guaranteed JSON matching schema. Or use response_mime_type="text/x.enum" + enum schema for single-value classification. Types must be uppercase. Use property_ordering for consistent output. Gemini's Pydantic → Schema helper handles most translation.
The Error Messages You're Seeing
HTTP/1.1 400 Bad Request
{
"error": {
"code": 400,
"message": "response_schema requires response_mime_type to
be application/json or text/x.enum.",
"status": "INVALID_ARGUMENT"
}
}
Variants:
"Invalid response_schema: field 'age.minimum' not supported"
"response_schema type must be OBJECT for JSON mode"
"text/x.enum requires schema type STRING with enum values"
"response_json_schema is deprecated; use response_schema"
"Cannot use both response_schema and tools in the same request"response_mime_type Modes
| MIME type | Schema needed | Use case |
|---|---|---|
application/json | OBJECT or ARRAY schema | Structured data extraction |
text/x.enum | STRING with enum | Classification (single value) |
text/plain (default) | None | Free text |
What Actually Causes This Error
Fixes That Work (Tested Nov 2026)
1JSON Mode with Schema
2Enum Classification Mode
text/x.enum bypasses full JSON generation — model emits a single token from the enum set. Ideal for classification, routing, sentiment. On Flash-Lite this can be 5-10× cheaper per call than JSON mode.
3Pydantic Integration
Preventing This Error Going Forward
- Always pair response_mime_type + response_schema. Neither alone enforces both.
- Uppercase types in raw dict schemas. STRING, OBJECT, INTEGER.
- Use property_ordering for consistent parsing. Especially for arrays.
- Don't combine tools + response_schema. Choose one per request.
- Prefer Pydantic for maintainability. SDK handles most translation.
- Use text/x.enum for single-label tasks. Faster, cheaper.
- Validate response.parsed exists before using. Falls back to text on schema mismatch.
Researcher · AI Error Hub
Frequently Asked Questions
Not in the same request. Choose: response_schema forces the model's final output shape, tools let the model call functions. If you need both patterns, do multi-turn: tools for gathering data, then a final call with response_schema for structured summary.
No — same token pricing. There's minor pre-compilation overhead on new schemas (~100-500ms first call) then cached. Overall usually FASTER because the model doesn't waste tokens on preamble/markdown.
Rare with controlled generation but can happen on very complex schemas or edge cases. Wrap json.loads in try/except; on failure, retry with same prompt (schema compilation improves consistency).
Only if you passed a Pydantic/dataclass as response_schema. With raw types.Schema, use response.text and parse manually. Check for None before accessing.
Yes but keep reasonable (5-6 levels max). Deeply nested schemas hurt accuracy and can hit token overhead limits. Consider flattening or using multi-step calls for complex hierarchies.
Related Errors
Structured Outputs with Gemini
Weekly deep dives on controlled generation, Pydantic, and cross-provider schemas. 12,000+ developers subscribe.
Subscribe Free →