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 @@ -255,8 +255,9 @@ async def test_agent_run_async(memory_logger, agent_kind):
chat_span = next(span for span in spans if span["span_attributes"]["type"] == SpanTypeAttribute.LLM)
assert agent_span is not None
assert agent_span["span_attributes"]["type"] == SpanTypeAttribute.TASK
if agent_kind == "wrapper":
assert agent_span["span_attributes"]["name"] == "agent_run [wrapped-agent]"
# Unnamed agents must get the caller's variable name, not the wrapper's `instance` argument.
expected_name = "wrapped-agent" if agent_kind == "wrapper" else "agent"
assert agent_span["span_attributes"]["name"] == f"agent_run [{expected_name}]"
assert agent_span["input"]["user_prompt"] == TEST_PROMPT
assert "4" in str(agent_span["output"])
assert chat_span["span_parents"] == [agent_span["span_id"]]
Expand Down Expand Up @@ -301,6 +302,8 @@ def test_agent_run_sync(memory_logger):
assert chat_span is not None, "chat span not found"

# Check agent span
assert agent.name == "agent"
assert agent_sync_span["span_attributes"]["name"] == "agent_run_sync [agent]"
assert agent_sync_span["span_attributes"]["type"] == SpanTypeAttribute.TASK
assert agent_sync_span["metadata"]["model"] == "gpt-6-luna"
assert agent_sync_span["metadata"]["provider"] == "openai"
Expand Down Expand Up @@ -494,6 +497,7 @@ async def test_agent_run_stream(memory_logger):

# Check agent span
assert agent_span["span_attributes"]["type"] == SpanTypeAttribute.TASK
assert agent_span["span_attributes"]["name"] == "agent_run_stream [agent]"
assert agent_span["metadata"]["model"] == "gpt-6-luna"
assert "Count from 1 to 5" in str(agent_span["input"])
_assert_metrics_are_valid(agent_span["metrics"], start, end)
Expand Down Expand Up @@ -937,6 +941,7 @@ def test_agent_run_stream_sync(memory_logger):

# Check agent span
assert agent_span["span_attributes"]["type"] == SpanTypeAttribute.TASK
assert agent_span["span_attributes"]["name"] == "agent_run_stream_sync [agent]"
assert agent_span["metadata"]["model"] == "gpt-6-luna"
assert "Count from 1 to 3" in str(agent_span["input"])
_assert_metrics_are_valid(agent_span["metrics"], start, end)
Expand Down Expand Up @@ -1011,6 +1016,7 @@ async def test_agent_run_stream_events(memory_logger):

# Check agent span has basic structure
assert agent_span["span_attributes"]["type"] == SpanTypeAttribute.TASK
assert agent_span["span_attributes"]["name"] == "agent_run_stream_events [agent]"
assert agent_span["metadata"]["model"] == "gpt-6-luna"
assert "5+5" in str(agent_span["input"]) or "What" in str(agent_span["input"])
assert agent_span["metrics"]["event_count"] == event_count
Expand Down
23 changes: 23 additions & 0 deletions py/src/braintrust/integrations/pydantic_ai/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,25 @@ def _log_agent_result(span: Any, input_data: dict[str, Any], result: Any, output
span.log(input=resolved_input, output=output, metrics=metrics, metadata=metadata)


_WRAPPER_MODULES = (__name__, "braintrust.integrations.base")


def _infer_agent_name(instance: Any, kwargs: Any) -> None:
# Pydantic AI names an unnamed agent after the variable that holds it in the
# caller's frame. Under these wrappers that caller would be a Braintrust frame,
# so the agent would be named "instance". Infer from the user's frame instead.
if getattr(instance, "name", "") is not None or not kwargs.get("infer_name", True):
return
if not hasattr(instance, "_infer_name"):
return
frame = inspect.currentframe().f_back
while frame.f_back is not None and frame.f_back.f_globals.get("__name__") in _WRAPPER_MODULES:
frame = frame.f_back
instance._infer_name(frame)


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

input_data, metadata = _build_agent_input_and_metadata(args, kwargs, instance)

with start_span(
Expand Down Expand Up @@ -147,6 +165,7 @@ async def _agent_run_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any


def _agent_run_sync_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
_infer_agent_name(instance, kwargs)
input_data, metadata = _build_agent_input_and_metadata(args, kwargs, instance)

with start_span(
Expand Down Expand Up @@ -176,6 +195,7 @@ def _agent_run_sync_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any)


def _agent_to_cli_sync_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
_infer_agent_name(instance, kwargs)
input_data, metadata = _build_agent_input_and_metadata(args, kwargs, instance)

with start_span(
Expand All @@ -194,6 +214,7 @@ def _agent_to_cli_sync_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: A


def _agent_run_stream_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
_infer_agent_name(instance, kwargs)
input_data, metadata = _build_agent_input_and_metadata(args, kwargs, instance)
agent_name = instance.name if hasattr(instance, "name") else None
span_name = f"agent_run_stream [{agent_name}]" if agent_name else "agent_run_stream"
Expand All @@ -207,6 +228,7 @@ def _agent_run_stream_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: An


def _agent_run_stream_sync_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
_infer_agent_name(instance, kwargs)
input_data, metadata = _build_agent_input_and_metadata(args, kwargs, instance)
agent_name = instance.name if hasattr(instance, "name") else None
span_name = f"agent_run_stream_sync [{agent_name}]" if agent_name else "agent_run_stream_sync"
Expand Down Expand Up @@ -241,6 +263,7 @@ def _agent_run_stream_sync_wrapper(wrapped: Any, instance: Any, args: Any, kwarg


def _agent_run_stream_events_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any):
_infer_agent_name(instance, kwargs)
input_data, metadata = _build_agent_input_and_metadata(args, kwargs, instance)

agent_name = instance.name if hasattr(instance, "name") else None
Expand Down