Repository navigation
fix(pydantic-ai): infer unnamed agent names from the caller, not the wrapper - #1141
Roman Zydyk (romanzdk) wants to merge 1 commit into
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 45ce72f779
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
|
|
||
|
|
||
| async def _agent_run_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any): | ||
| _infer_agent_name(instance, kwargs) |
There was a problem hiding this comment.
Infer async agent names before task scheduling
When callers pass agent.run(...) to asyncio.create_task(), asyncio.gather(), or asyncio.wait_for(), this async wrapper is not entered until the event loop drives the coroutine, so the user's variable-defining frame is no longer on the stack. _infer_agent_name then inspects an asyncio frame, and the wrapped Pydantic run subsequently sees _agent_run_wrapper and infers instance, leaving these traces as agent_run [instance]; capture/infer the caller synchronously when wrapt invokes the method, before returning the coroutine.
Useful? React with 👍 / 👎.
|
thanks for the PR Roman Zydyk (@romanzdk)! opened #1143 so closing this one |
> 🤖 **This text was AI-generated.**
Pydantic AI infers an unnamed agent's name from its caller. Braintrust's
wrappers could cause that lookup to find an internal variable named
`instance`, while the outer span still had a generic name.
Run Pydantic AI's lookup in the caller's scope before starting the span.
Keep explicit names and `infer_name=False`, and defer inference for
`run_stream()` until the caller enters the stream.
For `my_agent.run_sync("hi")`, the outer and inner span names become:
```text
Before
agent_run_sync -> agent_run [instance]
After
agent_run_sync [my_agent] -> agent_run [my_agent]
```
Fixes #1139
Refs: #1141
Fixes #1139.
Pydantic AI names an unnamed agent after the variable that holds it in the caller's frame. Under the integration that caller is a Braintrust wrapper, so agents were named
instance(or got no name withrun_stream).Each agent wrapper now infers the name first, from the first frame outside the wrapper modules (
tracing.pyandintegrations/base.py). It does nothing when the agent already has a name or the caller passedinfer_name=False. Because the name is set before the span starts, the outer span is named too. Forrun(), the name is found whenagent.run(...)is called, not when the coroutine starts, soasyncio.create_task(agent.run(...)),gatherandwait_forwork as well.Before / after (agent created as
agent = Agent(MODEL)):Checks
run,run_sync,run_stream,run_stream_sync,run_stream_eventstests, run before the fixcreate_taskcase intest_agent_run_async, run before the second commitagent_runinstead ofagent_run [agent]test_pydantic_ai_integration(latest)test_pydantic_ai_integration(1.107.9)test_pydantic_ai_logfire/test_pydantic_ai_wrap_openai(both versions)test_temporal(latest)(includes Temporal + Pydantic AI)nox -s pylintrun,run_sync,run_stream,run_stream_sync,run_stream_eventson an unnamed agent, with and without instrumentationcreate_task,gather,wait_forNone, before this fix:instance)No new cassettes: the tests reuse the existing ones.