Skip to content

Commit 19afe35

Browse files
fix(pydantic-ai): trace step persistence lineage on agent iter (#1198)
## Summary Follow-up to #1194 and #1192. The direct `Agent.iter()` root span added in #1192 was not passed into the Step Persistence lineage capture added in #1194. With Harness installed, `iter()` raised `TypeError: _start_tool_trace_capture() missing 1 required positional argument: 'agent_span'`. `tracing.py` now passes the `iter()` task span into the existing capture, so `metadata.step_persistence.{run_id, parent_run_id}` gets recorded on `iter()` runs the same way as on `run()` / `run_stream()`. Source and fork runs stay in separate traces. The new test case in `harness/test_step_persistence.py` replays a real OpenAI Responses cassette (`test_step_persistence_lineage[fork_run-iter].yaml`). `run_stream()` lineage already has VCR coverage on main from #1194. Link to Devin session: https://braintrust.devinenterprise.com/sessions/b253a31c21104ab8baef1857d38a2cf0 Open in Devin Desktop: https://braintrust.devinenterprise.com/desktop/session/b253a31c21104ab8baef1857d38a2cf0?variant=devin Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 879d456 commit 19afe35

2 files changed

Lines changed: 250 additions & 1 deletion

File tree

‎py/src/braintrust/integrations/pydantic_ai/harness/cassettes/latest/test_step_persistence_lineage[fork_run-iter].yaml‎

Lines changed: 241 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
pytest.param("fork_run", False, "run", id="fork_run-False"),
1919
pytest.param("fork_run", True, "run", id="fork_run-True"),
2020
pytest.param("fork_run", False, "run_stream", id="fork_run-run_stream"),
21+
pytest.param("fork_run", False, "iter", id="fork_run-iter"),
2122
],
2223
)
2324
async def test_step_persistence_lineage(
@@ -55,14 +56,21 @@ async def test_step_persistence_lineage(
5556
if entrypoint == "run_stream":
5657
async with branch.run_stream(prompt, run_id="branch-run", message_history=history) as result:
5758
output = "".join([chunk async for chunk in result.stream_text(delta=True)])
59+
elif entrypoint == "iter":
60+
async with branch.iter(prompt, run_id="branch-run", message_history=history) as run:
61+
async for _ in run:
62+
pass
63+
assert run.result is not None
64+
output = run.result.output
5865
else:
5966
output = (await branch.run(prompt, run_id="branch-run", message_history=history)).output
6067
spans = with_memory_logger.pop()
6168

6269
source_record = await store.get_run(run_id="source-run")
6370
branch_record = await store.get_run(run_id="branch-run")
6471
source_span = find_span_by_name(spans, "agent_run [source]")
65-
branch_span = find_span_by_name(spans, f"agent_{entrypoint} [branch]")
72+
branch_span_name = "agent_run_stream" if entrypoint == "run_stream" else "agent_run"
73+
branch_span = find_span_by_name(spans, f"{branch_span_name} [branch]")
6674
assert branch_span["span_attributes"]["type"] == "task"
6775
assert branch_span["output"]["response"]["parts"][0]["content"] == output
6876
if entrypoint == "run_stream":

0 commit comments

Comments
 (0)