Skip to content

fix(pydantic-ai): infer unnamed agent names from the caller, not the wrapper - #1141

Closed
Roman Zydyk (romanzdk) wants to merge 1 commit into
braintrustdata:mainfrom
romanzdk:fix/pydantic-ai-agent-name-inference
Closed

Roman Zydyk (romanzdk) wants to merge 1 commit into
braintrustdata:mainfrom
romanzdk:fix/pydantic-ai-agent-name-inference

Conversation

@romanzdk

@romanzdk Roman Zydyk (romanzdk) commented Nov 18, 2026 •

Copy link
Copy Markdown

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 with run_stream).

Each agent wrapper now infers the name first, from the first frame outside the wrapper modules (tracing.py and integrations/base.py). It does nothing when the agent already has a name or the caller passed infer_name=False. Because the name is set before the span starts, the outer span is named too. For run(), the name is found when agent.run(...) is called, not when the coroutine starts, so asyncio.create_task(agent.run(...)), gather and wait_for work as well.

Before / after (agent created as agent = Agent(MODEL)):

before: agent_run_sync         -> agent_run [instance]
after:  agent_run_sync [agent] -> agent_run [agent]

Checks

Check Result
New span-name asserts in run, run_sync, run_stream, run_stream_sync, run_stream_events tests, run before the fix fail (red)
New create_task case in test_agent_run_async, run before the second commit fails: agent_run instead of agent_run [agent]
test_pydantic_ai_integration(latest) 65 passed
test_pydantic_ai_integration(1.107.9) 53 passed, 12 skipped
test_pydantic_ai_logfire / test_pydantic_ai_wrap_openai (both versions) passed
test_temporal(latest) (includes Temporal + Pydantic AI) 27 passed
pre-commit hooks (ruff, codespell, …) on changed files, nox -s pylint passed
Script calling run, run_sync, run_stream, run_stream_sync, run_stream_events on an unnamed agent, with and without instrumentation same names in both cases
Script with create_task, gather, wait_for variable name (without Braintrust: None, before this fix: instance)

No new cassettes: the tests reuse the existing ones.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T17:05:32.045319Z 45ce72f PR opened
ℹ️ About Codex in GitHub

Your 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@AbhiPrasad

Copy link
Copy Markdown
Member

thanks for the PR Roman Zydyk (@romanzdk)! opened #1143 so closing this one

Abhijeet Prasad (AbhiPrasad) added a commit that referenced this pull request Nov 18, 2026
> 🤖 **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
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.

Pydantic AI integration: unnamed agents are named "instance"

2 participants