Skip to content

Commit d265086

Browse files
fix(pydantic-ai): preserve lineage on iter traces
> 🤖 **This text was AI-generated.** Step persistence needs the current agent span to attach lineage. Pass the span from direct iteration so the trace opens and receives lineage metadata. Replay the background tool scenario through iter as well to cover the child agent nesting under its tool span. Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com> Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 954fdbd commit d265086

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎py/src/braintrust/integrations/pydantic_ai/harness/test_background_tools.py‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@
1212
from pydantic_ai.usage import UsageLimits
1313

1414

15-
@pytest.mark.vcr
1615
@pytest.mark.asyncio
17-
@pytest.mark.parametrize("fails", [False, True], ids=["success", "failure"])
18-
async def test_harness_background_tool_execution(with_memory_logger, fails):
16+
@pytest.mark.parametrize(
17+
("fails", "use_iter"),
18+
[
19+
pytest.param(False, False, id="success", marks=pytest.mark.vcr),
20+
pytest.param(True, False, id="failure", marks=pytest.mark.vcr),
21+
pytest.param(
22+
False,
23+
True,
24+
id="iter",
25+
marks=pytest.mark.vcr(cassette_name="test_harness_background_tool_execution[success]"),
26+
),
27+
],
28+
)
29+
async def test_harness_background_tool_execution(with_memory_logger, fails, use_iter):
1930
background_tools = import_module("pydantic_ai_harness.background_tools")
2031
project = "test-pydantic-ai-harness"
2132
assert setup_pydantic_ai(project_name=project)
@@ -37,12 +48,17 @@ async def slow(query: str) -> str:
3748
raise ToolFailed(f"failed: {query}")
3849
return (await child.run(f"Reply with exactly '{query} complete' and nothing else.")).output
3950

40-
await parent.run(
41-
"Call slow exactly once with query 'lookup'. Then answer 'done' without calling any more tools.",
42-
usage_limits=UsageLimits(request_limit=6),
43-
)
51+
prompt = "Call slow exactly once with query 'lookup'. Then answer 'done' without calling any more tools."
52+
limits = UsageLimits(request_limit=6)
53+
if use_iter:
54+
async with parent.iter(prompt, usage_limits=limits) as run:
55+
async for _ in run:
56+
pass
57+
else:
58+
await parent.run(prompt, usage_limits=limits)
4459
spans = with_memory_logger.pop()
4560
parent_span = find_span_by_name(spans, "agent_run [background_parent]")
61+
assert not parent_span.get("span_parents")
4662
tool_spans = find_spans_by_type(spans, SpanTypeAttribute.TOOL)
4763
assert [span["span_attributes"]["name"] for span in tool_spans] == ["slow"]
4864
(execution,) = tool_spans

‎py/src/braintrust/integrations/pydantic_ai/harness/test_iter.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def echo(text: str) -> str:
4343
assert [span for span in spans if not span.get("span_parents")] == [run]
4444
assert run["input"]["user_prompt"] == prompt
4545
assert run["output"]["output"] == agent_run.result.output
46+
assert run["metadata"]["step_persistence"] == {"run_id": run_id}
4647

4748
llm_spans = find_spans_by_type(spans, SpanTypeAttribute.LLM)
4849
assert len(llm_spans) == 2

‎py/src/braintrust/integrations/pydantic_ai/tracing.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ async def traced_iter():
241241
input=input_data if input_data else None,
242242
metadata=metadata,
243243
) as agent_span:
244-
tool_trace_token = _start_tool_trace_capture()
244+
tool_trace_token = _start_tool_trace_capture(agent_span)
245245
start_time = time.time()
246246
run = None
247247
try:

0 commit comments

Comments
 (0)