11import asyncio
2+ from importlib import import_module
23
34import pytest
4- from braintrust import setup_pydantic_ai
5+ from braintrust . integrations . pydantic_ai . _test_harness import OFFLINE_ANSWER , make_harness_agent
56from braintrust .span_types import SpanTypeAttribute
6- from braintrust .test_helpers import init_test_logger
7- from braintrust .test_helpers import with_memory_logger as with_memory_logger
8- from pydantic_ai import Agent
7+ from braintrust .test_helpers import find_span_by_name , find_spans_by_type
98from pydantic_ai .messages import ModelResponse , TextPart , ToolCallPart , ToolReturnPart
109from pydantic_ai .models .function import FunctionModel
11- from pydantic_ai .models .test import TestModel
1210
1311
1412@pytest .mark .asyncio
1513@pytest .mark .parametrize ("fails" , [False , True ], ids = ["success" , "failure" ])
16- async def test_harness_background_tool_execution (with_memory_logger , fails ):
17- background_tools = pytest .importorskip ("pydantic_ai_harness.background_tools" , exc_type = ModuleNotFoundError )
14+ async def test_harness_background_tool_execution (harness_spans , fails ):
1815 from pydantic_ai .exceptions import ToolFailed
1916
20- project = "test-pydantic-ai-harness"
21- assert setup_pydantic_ai (project_name = project )
22- init_test_logger (project )
23-
17+ background_tools = import_module ("pydantic_ai_harness.background_tools" )
2418 call_id = "background-call"
2519 receipt_seen = asyncio .Event ()
26- child = Agent ( TestModel ( custom_output_text = "offline answer" ), name = "background_child" )
20+ child = make_harness_agent ( "background_child" )
2721
2822 def respond (messages , _info ):
2923 for message in messages :
@@ -34,7 +28,9 @@ def respond(messages, _info):
3428 return ModelResponse (parts = [TextPart ("done" )])
3529 return ModelResponse (parts = [ToolCallPart ("slow" , {"query" : "lookup" }, tool_call_id = call_id )])
3630
37- parent = Agent (FunctionModel (respond ), name = "background_parent" , capabilities = [background_tools .BackgroundTools ()])
31+ parent = make_harness_agent (
32+ "background_parent" , FunctionModel (respond ), capabilities = [background_tools .BackgroundTools ()]
33+ )
3834
3935 @parent .tool_plain (metadata = {"background" : True })
4036 async def slow (query : str ) -> str :
@@ -44,9 +40,9 @@ async def slow(query: str) -> str:
4440 return (await child .run (query )).output
4541
4642 assert (await parent .run ("dispatch background tool" )).output == "done"
47- spans = with_memory_logger .pop ()
48- parent_span = next ( s for s in spans if s [ "span_attributes" ][ "name" ] == "agent_run [background_parent]" )
49- tool_spans = [ s for s in spans if s [ "span_attributes" ][ "type" ] == SpanTypeAttribute .TOOL ]
43+ spans = harness_spans .pop ()
44+ parent_span = find_span_by_name ( spans , "agent_run [background_parent]" )
45+ tool_spans = find_spans_by_type ( spans , SpanTypeAttribute .TOOL )
5046 assert [span ["span_attributes" ]["name" ] for span in tool_spans ] == ["slow" , "slow" ]
5147 dispatch = next (s for s in tool_spans if s ["span_parents" ] == [parent_span ["span_id" ]])
5248 execution = next (s for s in tool_spans if s ["span_parents" ] == [dispatch ["span_id" ]])
@@ -59,7 +55,7 @@ async def slow(query: str) -> str:
5955 assert "failed: lookup" in execution ["error" ]
6056 assert execution .get ("output" ) is None
6157 else :
62- assert execution ["output" ] == "offline answer"
58+ assert execution ["output" ] == OFFLINE_ANSWER
6359 assert execution .get ("error" ) is None
64- child_span = next ( s for s in spans if s [ "span_attributes" ][ "name" ] == "agent_run [background_child]" )
60+ child_span = find_span_by_name ( spans , "agent_run [background_child]" )
6561 assert child_span ["span_parents" ] == [execution ["span_id" ]]
0 commit comments