Skip to content

Commit 9878cf7

Browse files
test(pydantic-ai): add offline harness coverage (#1191)
## Summary Adds Pydantic AI Harness to the test matrix so follow-up Harness tracing PRs have a place to land. - `[tool.braintrust.matrix]`: `pydantic_ai` latest → `2.75.0` (keeps `1.107.9`), new `pydantic_ai_harness` latest `0.75.0`; uv cutoff advanced just enough to admit the pins. - New nox session `test_pydantic_ai_harness(latest)` (Python 3.11+, auto-discovered by CI). - Shared offline helpers in `pydantic_ai/_test_harness.py` + `harness/conftest.py`: `make_harness_agent`, `call_tool_once` (TestModel/FunctionModel), `harness_spans` (in-memory logger). - Smoke test: a Harness agent run under `setup_pydantic_ai()` yields a run span with llm and tool children. Relies only on Braintrust patching — no native OTel/Logfire. No production instrumentation changes. Link to Devin session: https://braintrust.devinenterprise.com/sessions/647e916c73ff4ada83311199fd5a2ec3 Open in Devin Desktop: https://braintrust.devinenterprise.com/desktop/session/647e916c73ff4ada83311199fd5a2ec3?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 2190088 commit 9878cf7

7 files changed

Lines changed: 176 additions & 77 deletions

File tree

‎py/nox_session_constraints.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"test_google_adk": {"versions": {"latest": {"min_python": "3.11"}}}, # google-adk 2.12 requires 3.11
1919
"test_google_genai": {"versions": {"latest": {"min_python": "3.11"}}}, # google-genai 2.34 requires 3.11
2020
"test_pydantic_ai_integration": {"versions": {"latest": {"min_python": "3.11"}}},
21+
"test_pydantic_ai_harness": {"min_python": "3.11"},
2122
"test_pydantic_ai_logfire": {"versions": {"latest": {"min_python": "3.11"}}},
2223
"test_pydantic_ai_wrap_openai": {"versions": {"latest": {"min_python": "3.11"}}},
2324
"test_dspy": {"versions": {"latest": {"min_python": "3.11", "max_python_exclusive": "3.15"}}}, # fastuuid

‎py/noxfile.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ def test_autogen(session, version):
614614
# Test both the direct integration and wrap_openai with provider versions
615615
# that support patched OpenTelemetry releases.
616616
PYDANTIC_AI_INTEGRATION_VERSIONS = _get_matrix_versions("pydantic-ai-integration")
617+
PYDANTIC_AI_HARNESS_VERSIONS = _get_matrix_versions("pydantic-ai-harness")
617618
PYDANTIC_AI_WRAP_OPENAI_VERSIONS = _get_matrix_versions("pydantic-ai-wrap-openai")
618619

619620

@@ -626,6 +627,16 @@ def test_pydantic_ai_integration(session, version):
626627
_run_tests(session, f"{INTEGRATION_DIR}/pydantic_ai/test_pydantic_ai_integration.py", version=version)
627628

628629

630+
@nox.session()
631+
@nox.parametrize("version", PYDANTIC_AI_HARNESS_VERSIONS, ids=PYDANTIC_AI_HARNESS_VERSIONS)
632+
def test_pydantic_ai_harness(session, version):
633+
_skip_if_incompatible(session)
634+
_install_test_deps(session)
635+
_install_matrix_dep(session, "pydantic-ai-integration", LATEST)
636+
_install_matrix_dep(session, "pydantic-ai-harness", version)
637+
_run_tests(session, f"{INTEGRATION_DIR}/pydantic_ai/harness", version=version)
638+
639+
629640
@nox.session()
630641
@nox.parametrize("version", PYDANTIC_AI_INTEGRATION_VERSIONS, ids=PYDANTIC_AI_INTEGRATION_VERSIONS)
631642
def test_pydantic_ai_logfire(session, version):

‎py/pyproject.toml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ explicit = true
421421
# ---------------------------------------------------------------------------
422422

423423
[tool.uv]
424-
exclude-newer = "2026-11-23T06:01:28Z"
424+
exclude-newer = "2026-11-25T18:45:00Z"
425425
conflicts = [
426426
# Groups that pin or transitively require conflicting openai versions.
427427
[
@@ -565,11 +565,14 @@ latest = "strands-agents==1.65.0"
565565
"1.20.0" = "strands-agents==1.20.0"
566566

567567
[tool.braintrust.matrix.pydantic-ai-integration]
568-
latest = "pydantic-ai==2.71.0"
568+
latest = "pydantic-ai==2.75.0"
569569
"1.107.9" = "pydantic-ai==1.107.9"
570570

571+
[tool.braintrust.matrix.pydantic-ai-harness]
572+
latest = "pydantic-ai-harness==0.75.0"
573+
571574
[tool.braintrust.matrix.pydantic-ai-wrap-openai]
572-
latest = "pydantic-ai==2.71.0"
575+
latest = "pydantic-ai==2.75.0"
573576
"1.107.9" = "pydantic-ai==1.107.9"
574577

575578
[tool.braintrust.matrix.autoevals]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pydantic_ai import Agent
2+
from pydantic_ai.messages import ModelResponse, TextPart, ToolCallPart, ToolReturnPart
3+
from pydantic_ai.models.function import FunctionModel
4+
from pydantic_ai.models.test import TestModel as PydanticTestModel
5+
6+
7+
OFFLINE_ANSWER = "offline answer"
8+
9+
10+
def make_harness_agent(name, model=None, *, capabilities=(), **kwargs):
11+
if model is None:
12+
model = PydanticTestModel(custom_output_text=OFFLINE_ANSWER)
13+
return Agent(model, name=name, capabilities=list(capabilities), **kwargs)
14+
15+
16+
def call_tool_once(tool_name, args=None, *, tool_call_id="offline-call"):
17+
def respond(messages, _info):
18+
if any(
19+
isinstance(part, ToolReturnPart) and part.tool_call_id == tool_call_id
20+
for message in messages
21+
for part in message.parts
22+
):
23+
return ModelResponse(parts=[TextPart(OFFLINE_ANSWER)])
24+
return ModelResponse(parts=[ToolCallPart(tool_name, args or {}, tool_call_id=tool_call_id)])
25+
26+
return FunctionModel(respond, model_name=f"function:{tool_name}")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
from braintrust import setup_pydantic_ai
3+
from braintrust.test_helpers import init_test_logger
4+
from braintrust.test_helpers import with_memory_logger as with_memory_logger
5+
6+
7+
@pytest.fixture
8+
def harness_spans(with_memory_logger):
9+
pytest.importorskip("pydantic_ai_harness", exc_type=ModuleNotFoundError)
10+
project = "test-pydantic-ai-harness"
11+
assert setup_pydantic_ai(project_name=project)
12+
init_test_logger(project)
13+
return with_memory_logger
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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"

‎py/uv.lock‎

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

0 commit comments

Comments
 (0)