|
| 1 | +from importlib import import_module |
| 2 | + |
| 3 | +import pytest |
| 4 | +from braintrust.integrations.pydantic_ai._test_harness import OFFLINE_ANSWER, call_tool_once, make_harness_agent |
| 5 | +from braintrust.span_types import SpanTypeAttribute |
| 6 | +from braintrust.test_helpers import find_span_by_name, find_spans_by_type |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_harness_run_traces_model_and_tool(harness_spans): |
| 11 | + persistence = import_module("pydantic_ai_harness.step_persistence") |
| 12 | + store = persistence.InMemoryStepStore() |
| 13 | + agent = make_harness_agent( |
| 14 | + "harness_smoke", |
| 15 | + call_tool_once("echo", {"text": "hello"}), |
| 16 | + capabilities=[persistence.StepPersistence(store=store)], |
| 17 | + ) |
| 18 | + |
| 19 | + @agent.tool_plain |
| 20 | + def echo(text: str) -> str: |
| 21 | + return text |
| 22 | + |
| 23 | + result = await agent.run("Echo hello.", run_id="harness-smoke") |
| 24 | + |
| 25 | + assert result.output == OFFLINE_ANSWER |
| 26 | + assert await store.list_events(run_id="harness-smoke") |
| 27 | + |
| 28 | + spans = harness_spans.pop() |
| 29 | + run = find_span_by_name(spans, "agent_run [harness_smoke]") |
| 30 | + assert run["span_attributes"]["type"] == SpanTypeAttribute.TASK |
| 31 | + assert not run.get("span_parents") |
| 32 | + |
| 33 | + llm_spans = find_spans_by_type(spans, SpanTypeAttribute.LLM) |
| 34 | + assert len(llm_spans) == 2 |
| 35 | + for llm in llm_spans: |
| 36 | + assert llm["span_parents"] == [run["span_id"]] |
| 37 | + |
| 38 | + tool = find_span_by_name(spans, "echo") |
| 39 | + assert tool["span_attributes"]["type"] == SpanTypeAttribute.TOOL |
| 40 | + assert tool["span_parents"] == [run["span_id"]] |
| 41 | + assert tool["input"] == {"text": "hello"} |
| 42 | + assert tool["output"] == "hello" |
0 commit comments