diff --git a/py/src/braintrust/integrations/pydantic_ai/test_pydantic_ai_integration.py b/py/src/braintrust/integrations/pydantic_ai/test_pydantic_ai_integration.py index e7b575f8..1e67b650 100644 --- a/py/src/braintrust/integrations/pydantic_ai/test_pydantic_ai_integration.py +++ b/py/src/braintrust/integrations/pydantic_ai/test_pydantic_ai_integration.py @@ -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 @@ -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" diff --git a/py/src/braintrust/integrations/pydantic_ai/tracing.py b/py/src/braintrust/integrations/pydantic_ai/tracing.py index c4ee2c14..57f60e14 100644 --- a/py/src/braintrust/integrations/pydantic_ai/tracing.py +++ b/py/src/braintrust/integrations/pydantic_ai/tracing.py @@ -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]) def _reset_tool_trace_capture(token: Any) -> None: