Skip to content

fix(pydantic_ai): support pydantic-ai 2.0#538

Merged
Abhijeet Prasad (AbhiPrasad) merged 2 commits into
mainfrom
pydantic-2.0-support
Jun 30, 2026
Merged

fix(pydantic_ai): support pydantic-ai 2.0#538
Abhijeet Prasad (AbhiPrasad) merged 2 commits into
mainfrom
pydantic-2.0-support

Conversation

@AbhiPrasad

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Jun 29, 2026

Copy link
Copy Markdown
Member

resolves #536

AI Summary:

This makes Braintrust tracing work with pydantic-ai 2.0. Streamed agent events are traced again instead of failing because pydantic-ai changed that API.

What users will see in Braintrust: stream-event spans continue to appear, completed direct streaming spans include the final model response, and errors during stream-event consumption are recorded on the span.

Traces also include additional pydantic-ai 2.0 response details when pydantic-ai returns them, such as finish reason, provider URL/name, run id, and conversation id. This also removes an internal deprecation warning that could appear during normal setup_pydantic_ai() tracing.

This makes Braintrust tracing work with pydantic-ai 2.0. Streamed agent events are traced again instead of failing because pydantic-ai changed that API.

What users will see in Braintrust: stream-event spans continue to appear, completed direct streaming spans include the final model response, and errors during stream-event consumption are recorded on the span.

Traces also include additional pydantic-ai 2.0 response details when pydantic-ai returns them, such as finish reason, provider URL/name, run id, and conversation id. This also removes an internal deprecation warning that could appear during normal setup_pydantic_ai() tracing.

event = await self._iterator.__anext__()
self._wrapper._event_count += 1
if hasattr(event, "output"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, my large language friend says this is the wrong attribute to check. Not sure if it's legit:

In _AgentStreamEventsIteratorProxy.__anext__, the code checks hasattr(event, "output"), but the final event (AgentRunResultEvent) exposes .result, not .output — confirmed in both pydantic-ai 1.x and 2.0 source. As a result:
- _final_result stays None, so the span logs no output
- _maybe_create_tool_spans_from_messages is never called
Verified empirically: final_result=None, output=None. This is pre-existing (carried over verbatim from the old wrapper, not a regression), but it sits in the rewritten code and contradicts the PR's goal. To fix, the check likely needs getattr(event, "result", None) and should shape event.result. Existing tests pass only because they assert event count / span presence, not the span's output.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch llm! will fix.

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) merged commit 1181d8e into main Jun 30, 2026
82 checks passed
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) deleted the pydantic-2.0-support branch June 30, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support pydantic-ai 2.0 in Python SDK integration

2 participants