Skip to content

fix(pydantic_ai): stop duplicating tool spans under Agent.run_sync() - #1138

Closed
Roman Zydyk (romanzdk) wants to merge 1 commit into
braintrustdata:mainfrom
romanzdk:fix/pydantic-ai-run-sync-duplicate-tool-spans
Closed

Roman Zydyk (romanzdk) wants to merge 1 commit into
braintrustdata:mainfrom
romanzdk:fix/pydantic-ai-run-sync-duplicate-tool-spans

Conversation

@romanzdk

Copy link
Copy Markdown

Problem

With the Pydantic AI integration, every tool call made inside Agent.run_sync() is logged twice: once live under agent_run, and once more rebuilt from the messages under agent_run_sync. await agent.run() is not affected. Fixes #1137.

run_sync() wraps run(), and both wrappers call _start_tool_trace_capture(), which started a new counter each time. The live tool span only bumped the inner counter, so the outer wrapper saw 0 and called _create_tool_spans_from_messages().

Solution

A nested capture reuses the counter that is already active, so the outer run_sync wrapper sees the live tool spans and skips the rebuild. A top-level run still starts its own counter.

test_tool_execution_creates_spans (which uses run_sync) only asserted >= 1 tool span, so it didn't catch the duplicate. It now asserts one tool span per ToolCallPart in result.new_messages().

Checks

Check Result
test_tool_execution_creates_spans before the fix fails: 1 tool call, 2 tool spans
test_tool_execution_creates_spans after the fix passes
nox -s "test_pydantic_ai_integration(latest)" 64 passed
nox -s "test_pydantic_ai_integration(1.107.9)" 52 passed, 12 skipped
nox -s "test_pydantic_ai_logfire(latest)", "test_pydantic_ai_wrap_openai(latest)" passed
nox -s "test_temporal(latest)" (includes Pydantic AI) 27 passed
nox -s pylint, ruff check, ruff format --check clean
Real app (Pydantic AI 2.69, OpenRouter), traces in Braintrust one tool span per call, before: two

run_sync() wraps run(), and each wrapper started its own tool span
counter. Live tool spans only bumped the inner counter, so the outer
run_sync wrapper saw zero and rebuilt every tool span from the messages.
A nested capture now reuses the active counter.

Fixes braintrustdata#1137
@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-18T16:58:56.269738Z 9c4a218 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: 9c4a2189ec

ℹ️ 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".

return _tool_trace_state.set([0])
# Reuse an active counter: run_sync() wraps run(), and the outer wrapper must see
# the tool spans the inner one logged, or it rebuilds them from the messages.
return _tool_trace_state.set(_tool_trace_state.get() or [0])

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 Scope counter reuse to run_sync's delegated run

When a traced function tool invokes a second agent whose calls require message reconstruction (for example, a structured-output subagent's final_result), _trace_tool_execution() has already incremented the parent's list before the child starts. This unconditional reuse gives the child that nonzero counter, so its _maybe_create_tool_spans_from_messages() returns without creating any of the child's TOOL spans; with the prior per-run list, the child fallback ran. Scope sharing to the run_sync()→run() delegation (or track emitted calls per run) rather than every nested capture.

AGENTS.md reference: AGENTS.md:L138-L143

Useful? React with 👍 / 👎.

@AbhiPrasad

Copy link
Copy Markdown
Member

I opened #1142 which is a little more robust - going to close this PR. thanks for the PR!

@romanzdk
Roman Zydyk (romanzdk) deleted the fix/pydantic-ai-run-sync-duplicate-tool-spans branch November 18, 2026 17:22
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: Agent.run_sync() logs every tool call twice

2 participants