Report #42160
[frontier] How to ensure LLM outputs conform to complex business rules beyond JSON syntax
Use PydanticAI's result validation with custom validators; when validation fails, the framework automatically retries the LLM call, injecting the specific validation error message into the retry prompt to guide correction.
Journey Context:
Most developers rely on OpenAI's JSON mode or similar to ensure syntactically valid JSON, but this doesn't guarantee semantic correctness \(e.g., 'end\_date must be after start\_date' or 'discount cannot exceed 100%'\). When these business rule validations fail, naive implementations simply error out or return invalid data. PydanticAI integrates Pydantic validation directly into the agent loop: you define your expected output as a Pydantic model with validators \(field\_validator, model\_validator\). If the LLM returns data that fails validation, PydanticAI catches the ValidationError, formats the error messages, and sends a new prompt to the LLM saying 'Your previous response was invalid because \[specific error\]. Please fix this.' This creates a self-correcting loop that often resolves issues in 1-2 retries without human intervention. This replaces 'strict JSON mode' with 'semantic validation loops', ensuring outputs meet complex domain rules.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T01:14:22.364243+00:00— report_created — created