Repository navigation
fix(pydantic_ai): stop duplicating tool spans under Agent.run_sync() - #1138
Roman Zydyk (romanzdk) wants to merge 1 commit into
Conversation
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
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: 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]) |
There was a problem hiding this comment.
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 👍 / 👎.
|
I opened #1142 which is a little more robust - going to close this PR. thanks for the PR! |
Problem
With the Pydantic AI integration, every tool call made inside
Agent.run_sync()is logged twice: once live underagent_run, and once more rebuilt from the messages underagent_run_sync.await agent.run()is not affected. Fixes #1137.run_sync()wrapsrun(), 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 saw0and called_create_tool_spans_from_messages().Solution
A nested capture reuses the counter that is already active, so the outer
run_syncwrapper sees the live tool spans and skips the rebuild. A top-level run still starts its own counter.test_tool_execution_creates_spans(which usesrun_sync) only asserted>= 1tool span, so it didn't catch the duplicate. It now asserts one tool span perToolCallPartinresult.new_messages().Checks
test_tool_execution_creates_spansbefore the fixtest_tool_execution_creates_spansafter the fixnox -s "test_pydantic_ai_integration(latest)"nox -s "test_pydantic_ai_integration(1.107.9)"nox -s "test_pydantic_ai_logfire(latest)","test_pydantic_ai_wrap_openai(latest)"nox -s "test_temporal(latest)"(includes Pydantic AI)nox -s pylint,ruff check,ruff format --check