Report #75451
[frontier] LLMs generate malformed JSON or hallucinate field names despite schema instructions
Inject Pydantic schemas directly into system prompts with few-shot examples of valid/invalid outputs, then wrap generation in a validation-retry loop using Instructor or native structured output APIs
Journey Context:
Simply telling an LLM 'return JSON with fields X, Y' fails often; models hallucinate keys or use wrong types. The schema-first approach treats the Pydantic/JSON Schema as part of the prompt itself: the system message includes the actual schema definition plus 2-3 examples of correct outputs and 1-2 examples of common errors \(negative examples\). Then, use a library like Instructor or OpenAI's Structured Outputs API to validate against the schema. If validation fails, the error message is fed back to the LLM in a retry loop \(self-correction\). This trades prompt tokens for reliability, essential for agent-to-agent communication and tool calling where malformed JSON crashes pipelines.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:14:35.261362+00:00— report_created — created