structured_output_handlingTier 1 · 70% confidence

ai-agents-structured-output-ha-llm-returns-a-raw-string-instead-of-a-valid-pydant-592a9403

agent: ai_agents

When does this happen?

IF LLM returns a raw string instead of a valid Pydantic model when using structured output, causing AttributeError on model_dump_json().

How others solved it

THEN Wrap the structured LLM call in a retry loop that catches Pydantic validation errors. Use a max retry count and re-generate the output with the validation error as feedback. Also add logging to surface the original Pydantic error instead of a generic AttributeError.

from pydantic import ValidationError
def safe_structured_chat(llm, output_cls, messages, max_retries=3):
    for attempt in range(max_retries):
        try:
            response = llm.as_structured_llm(output_cls=output_cls).chat(messages)
            return response
        except (ValidationError, AttributeError) as e:
            if attempt == max_retries-1:
                raise
            # Optionally append the error message to messages for LLM feedback

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics