tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Guardrails and Evaluation > LLM Output Schema Validation - Enforcing Structured Responses with Pydantic

LLM Output Schema Validation - Enforcing Structured Responses with Pydantic

Author: Venkata Sudhakar

ShopMax India's order processing agent needs to return structured data - product names, prices, and delivery dates - in a consistent format. Without output validation, LLMs occasionally return free-text prose instead of the expected JSON, causing downstream parsing failures. Combining Pydantic with retry logic enforces schema compliance and automatically corrects invalid responses.

The approach defines a Pydantic BaseModel for the expected output schema. After the LLM responds, the raw output is parsed against the model. If parsing fails, the system retries with an error correction prompt that includes the validation error message, asking the LLM to fix the structure. After a configurable number of retries, the request fails with a clear exception.

The example below shows ShopMax India's product recommendation agent enforcing a structured response. The Pydantic model requires product_name, price_rs, category, available, and delivery_days fields with automatic retry on schema violations.


It gives the following output,

Product  : Lenovo IdeaPad Slim 3 (Intel i3, 8GB RAM)
Price    : Rs 37999
Category : Laptops
Available: True
Delivery : 3 days

In production, set retries to 3 and log each retry attempt with the attempt number and error to your observability platform. Use model.model_validate_json() instead of model_validate() when the LLM returns a raw JSON string. Track retry rates by model and prompt version - a high retry rate signals the prompt needs clearer output format instructions. Store validation failures in a monitoring table to feed into your LLM quality dashboard.


 
  


  
bl  br