Skip to content

Commit 9b83e00

Browse files
committed
auto_instrument openai agents sdk
1 parent 7a4328e commit 9b83e00

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

py/src/braintrust/auto.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def auto_instrument(
4141
google_genai: bool = True,
4242
agno: bool = True,
4343
claude_agent_sdk: bool = True,
44+
openai_agent_sdk: bool = True,
4445
dspy: bool = True,
4546
adk: bool = True,
4647
) -> dict[str, bool]:
@@ -61,6 +62,7 @@ def auto_instrument(
6162
google_genai: Enable Google GenAI instrumentation (default: True)
6263
agno: Enable Agno instrumentation (default: True)
6364
claude_agent_sdk: Enable Claude Agent SDK instrumentation (default: True)
65+
openai_agent_sdk: Enable OpenAI Agent SDK instrumentation (default: True)
6466
dspy: Enable DSPy instrumentation (default: True)
6567
adk: Enable Google ADK instrumentation (default: True)
6668
@@ -124,6 +126,8 @@ def auto_instrument(
124126
results["agno"] = _instrument_integration(AgnoIntegration)
125127
if claude_agent_sdk:
126128
results["claude_agent_sdk"] = _instrument_integration(ClaudeAgentSDKIntegration)
129+
if openai_agent_sdk:
130+
results["openai_agent_sdk"] = _instrument_openai_agent_sdk()
127131
if dspy:
128132
results["dspy"] = _instrument_dspy()
129133
if adk:
@@ -140,6 +144,16 @@ def _instrument_openai() -> bool:
140144
return False
141145

142146

147+
def _instrument_openai_agent_sdk() -> bool:
148+
with _try_patch():
149+
from agents import set_trace_processors
150+
from braintrust.wrappers.openai import BraintrustTracingProcessor
151+
152+
set_trace_processors([BraintrustTracingProcessor()])
153+
return True
154+
return False
155+
156+
143157
def _instrument_integration(integration) -> bool:
144158
with _try_patch():
145159
return integration.setup()

py/src/braintrust/integrations/auto_test_scripts/test_auto_openai.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
# 1. Verify not patched initially
99
assert not getattr(openai, "__braintrust_wrapped__", False)
1010

11+
1112
# 2. Instrument
1213
results = auto_instrument()
1314
assert results.get("openai") == True
1415
assert getattr(openai, "__braintrust_wrapped__", False)
1516

17+
1618
# 3. Idempotent
1719
results2 = auto_instrument()
1820
assert results2.get("openai") == True
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Test auto_instrument for OpenAI Agents SDK."""
2+
3+
from agents.tracing import get_trace_provider
4+
from braintrust.auto import auto_instrument
5+
from braintrust.wrappers.openai import BraintrustTracingProcessor
6+
7+
8+
results = auto_instrument()
9+
assert results.get("openai_agent_sdk") == True
10+
bt_processor = get_trace_provider()._multi_processor._processors[0]
11+
assert isinstance(bt_processor, BraintrustTracingProcessor)
12+
13+
results2 = auto_instrument()
14+
assert results2.get("openai_agent_sdk") == True
15+
16+
# with autoinstrument_test_context("test_auto_openai_agents_sdk") as memory_logger:
17+
# agent = Agent(
18+
# name="Assistant",
19+
# instructions="You only respond in haikus.",
20+
# )
21+
22+
# result = Runner.run_sync(agent, "Tell me about recursion in programming.")
23+
24+
# span = memory_logger.pop()
25+
# assert len(spans) == 1, f"Expected 1 span, got {len(spans)}"
26+
# span = spans[0]

py/src/braintrust/wrappers/test_openai.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,10 @@ def test_auto_instrument_openai(self):
21722172
"""Test auto_instrument patches OpenAI, creates spans, and uninstrument works."""
21732173
verify_autoinstrument_script("test_auto_openai.py")
21742174

2175+
def test_auto_instrument_openai_agents_sdk(self):
2176+
"""Test auto_instrument patches OpenAI, creates spans, and uninstrument works."""
2177+
verify_autoinstrument_script("test_auto_openai_agents_sdk.py")
2178+
21752179

21762180
class TestZAICompatibleOpenAI:
21772181
"""Tests for validating some ZAI compatibility with OpenAI wrapper."""

0 commit comments

Comments
 (0)