Skip to content

Improve error trace for user errors that happen inside capture_run_messages #2218

@alonme

Description

@alonme

Description

When using the capture_run_messages context manager - user errors also show an internal error from the library.

Exceptions occur inside an internal exception handler in pydanticAI and will include something like..

...
LookupError: <ContextVar name='var' at 0x10635a020>

During handling of the above exception, another exception occurred:
...
<user exception>

Which is confusing and unnecessary

example for reproduction

from pydantic_ai import (
    Agent,
    capture_run_messages,
)

agent = Agent('openai:gpt-4o')

with capture_run_messages() as messages:  
    result = agent.run_sync('How are you?')
    1 / 0

    print(messages)

References

From the docs - https://ai.pydantic.dev/agents/#model-errors

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions