Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structure Result of List throws exception #989

Open
1 task done
ebamberg opened this issue Feb 25, 2025 · 0 comments
Open
1 task done

Structure Result of List throws exception #989

ebamberg opened this issue Feb 25, 2025 · 0 comments

Comments

@ebamberg
Copy link

Initial Checks

  • I confirm that I'm using the latest version of Pydantic AI

Description

When my structured result is a single result everything works fine.
But when my system prompt returns a list of results - like "Ensure the output are 5 search queries matching the user input." the framework throws an exception:
│ /home/erik/miniconda3/envs/llm/lib/python3.12/site-packages/pydantic_ai/_agent_graph.py:81 in │
│ increment_retries │
│ │
│ 78 │ def increment_retries(self, max_result_retries: int) -> None: │
│ 79 │ │ self.retries += 1 │
│ 80 │ │ if self.retries > max_result_retries: │
│ ❱ 81 │ │ │ raise exceptions.UnexpectedModelBehavior( │
│ 82 │ │ │ │ f'Exceeded maximum retries ({max_result_retries}) for result validation' │
│ 83 │ │ │ ) │
│ 84 │

╰────────────────────────────────────────────────────────────────────────────────────────────────────╯
UnexpectedModelBehavior: Exceeded maximum retries (1) for result validation

It does not matter how I define the list for example:

result_type=list[Person]
or
@DataClass
Answer:
persons: List[Person]
result_type=Answer

Example Code

@dataclass
class Person :
    name: str
    age: int       


agent = Agent(  
            model=llm,
            result_type=list(Person),
            system_prompt='Extract name and age of all persons from the following text. \n\n###TEXT####\n',  
        )

        result=agent.run_sync("John Doe, 36 years old, is an carpenter. Jane Austin,46 years old, is an author.")

Python, Pydantic AI & LLM client version

ollama (=OpenAPI client)
python 3.12
pydantic 0.0.26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant