Report #4450
[architecture] How do I force an LLM to return JSON that exactly matches my agent's expected schema?
Use native Structured Outputs \(json\_schema / text.format with strict: true\) instead of JSON mode or prompt-only JSON, then parse and validate with Pydantic/Zod. Treat schema conformance as the floor, not correctness.
Journey Context:
JSON mode guarantees valid JSON but not field presence, types, or enum values. Structured Outputs constrain token generation to the schema, eliminating the retry-and-parse dance. OpenAI recommends it over JSON mode whenever supported. The catch: strict mode requires additionalProperties: false and all fields required, and some JSON Schema features are unsupported. Also, a well-formed response can still contain wrong values, so always add semantic/business validation and monitor first-attempt success rate.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T19:30:35.735927+00:00— report_created — created