Skip to content

Commit f344ced

Browse files
committed
auto_instrument openai agents sdk
1 parent 183dbb0 commit f344ced

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

py/src/braintrust/auto.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def auto_instrument(
3535
google_genai: bool = True,
3636
agno: bool = True,
3737
claude_agent_sdk: bool = True,
38+
openai_agent_sdk: bool = True,
3839
dspy: bool = True,
3940
adk: bool = True,
4041
) -> dict[str, bool]:
@@ -55,6 +56,7 @@ def auto_instrument(
5556
google_genai: Enable Google GenAI instrumentation (default: True)
5657
agno: Enable Agno instrumentation (default: True)
5758
claude_agent_sdk: Enable Claude Agent SDK instrumentation (default: True)
59+
openai_agent_sdk: Enable OpenAI Agent SDK instrumentation (default: True)
5860
dspy: Enable DSPy instrumentation (default: True)
5961
adk: Enable Google ADK instrumentation (default: True)
6062
@@ -118,6 +120,8 @@ def auto_instrument(
118120
results["agno"] = _instrument_integration(AgnoIntegration)
119121
if claude_agent_sdk:
120122
results["claude_agent_sdk"] = _instrument_integration(ClaudeAgentSDKIntegration)
123+
if openai_agent_sdk:
124+
results["openai_agent_sdk"] = _instrument_openai_agent_sdk()
121125
if dspy:
122126
results["dspy"] = _instrument_dspy()
123127
if adk:
@@ -134,6 +138,16 @@ def _instrument_openai() -> bool:
134138
return False
135139

136140

141+
def _instrument_openai_agent_sdk() -> bool:
142+
with _try_patch():
143+
from agents import set_trace_processors
144+
from braintrust.wrappers.openai import BraintrustTracingProcessor
145+
146+
set_trace_processors([BraintrustTracingProcessor()])
147+
return True
148+
return False
149+
150+
137151
def _instrument_integration(integration) -> bool:
138152
with _try_patch():
139153
return integration.setup()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
"""Test auto_instrument for OpenAI."""
22

33
import openai
4+
from agents.tracing import get_trace_provider
45
from braintrust.auto import auto_instrument
6+
from braintrust.wrappers.openai import BraintrustTracingProcessor
57
from braintrust.wrappers.test_utils import autoinstrument_test_context
68

79

810
# 1. Verify not patched initially
911
assert not getattr(openai, "__braintrust_wrapped__", False)
1012

13+
1114
# 2. Instrument
1215
results = auto_instrument()
1316
assert results.get("openai") == True
1417
assert getattr(openai, "__braintrust_wrapped__", False)
1518

19+
assert results.get("openai_agent_sdk") == True
20+
bt_processor = get_trace_provider()._multi_processor._processors[0]
21+
assert isinstance(bt_processor, BraintrustTracingProcessor)
22+
1623
# 3. Idempotent
1724
results2 = auto_instrument()
1825
assert results2.get("openai") == True
26+
assert results2.get("openai_agent_sdk") == True
1927

2028
# 4. Make API call and verify span
2129
with autoinstrument_test_context("test_auto_openai") as memory_logger:

0 commit comments

Comments
 (0)