Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from packaging.version import Version
from pydantic import BaseModel
from pydantic_ai import Agent, ModelSettings
from pydantic_ai.messages import ModelRequest, ModelResponse, UserPromptPart
from pydantic_ai.messages import ModelRequest, ModelResponse, ToolCallPart, UserPromptPart
from pydantic_ai.usage import UsageLimits


Expand Down Expand Up @@ -1612,8 +1612,12 @@ def calculate(operation: str, a: float, b: float) -> float:
tool_names = [t["name"] for t in tools if isinstance(t, dict)]
assert "calculate" in tool_names, f"calculate tool should be in toolset, got: {tool_names}"

# run_sync() wraps run(): the live tool spans must not be rebuilt again from the messages.
tool_calls = [p for m in result.new_messages() for p in m.parts if isinstance(p, ToolCallPart)]
tool_spans = [s for s in spans if s["span_attributes"].get("type") == SpanTypeAttribute.TOOL]
assert len(tool_spans) >= 1, f"Expected at least 1 TOOL span, got {len(tool_spans)}"
assert len(tool_spans) == len(tool_calls) >= 1, (
f"Expected one TOOL span per tool call ({len(tool_calls)}), got {len(tool_spans)}"
)

calc_tool_span = next((s for s in tool_spans if s["span_attributes"]["name"] == "calculate"), None)
assert calc_tool_span is not None, "calculate TOOL span not found"
Expand Down
4 changes: 3 additions & 1 deletion py/src/braintrust/integrations/pydantic_ai/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def _direct_prepare_model_wrapper(wrapped: Any, instance: Any, args: Any, kwargs


def _start_tool_trace_capture() -> Any:
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 👍 / 👎.



def _reset_tool_trace_capture(token: Any) -> None:
Expand Down