ValidationError
pydantic.error_wrappers.ValidationError
Stack trace
pydantic.error_wrappers.ValidationError: 1 validation error for ModelName field_name value is not a valid dict (type=type_error.dict) During handling of the above exception, retry validation also failed, raising this error.
Why it happens
This error happens because the AI-generated output does not match the Pydantic model schema expected by the parser. Even after retrying with corrected prompts or format instructions, the output remains invalid or malformed, causing validation to fail repeatedly.
Detection
Catch pydantic.error_wrappers.ValidationError exceptions during parsing and log the raw LLM output to detect schema mismatches before the retry limit is reached.
Causes & fixes
LLM output includes unexpected markdown fences or extra text around JSON
Modify the prompt to instruct the model to return raw JSON only, or use a parser like JsonOutputParser that strips markdown fences automatically.
Pydantic model field names do not exactly match the keys in the LLM output
Ensure the Pydantic model fields are named exactly as the output keys, including case sensitivity.
Using a base LLM model that ignores output format instructions
Switch to an instruction-tuned model such as gpt-4o-mini or claude-3-5-haiku-20241022 that reliably follows output format instructions.
Prompt does not clearly specify the output format or schema
Enhance the prompt with explicit instructions and examples to enforce the expected output structure.
Code: broken vs fixed
from pydantic_ai import PydanticAI
model = MyPydanticModel()
ai = PydanticAI(model=model)
response = ai.invoke("Generate data") # Raises ValidationError after retries
print(response) import os
from pydantic_ai import PydanticAI, JsonOutputParser
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY') # Use env var for API key
model = MyPydanticModel()
parser = JsonOutputParser().with_retry(n=3) # Added parser with retry
ai = PydanticAI(model=model, output_parser=parser)
response = ai.invoke("Generate data with raw JSON only") # Fixed: retries handle malformed output
print(response) Workaround
Wrap the parsing call in try/except ValidationError, then extract JSON manually from the raw LLM output using regex and parse it with json.loads() as a fallback.
Prevention
Use structured output features provided by the LLM API or enforce strict output schemas via response_format parameters to guarantee schema-valid responses and avoid parser retries.