Skip to content

Commit d1695ac

Browse files
committed
Merge branch 'development' into release
2 parents 0a3a4a6 + 73df116 commit d1695ac

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/langtrace_python_sdk/instrumentation/openai_agents/patch.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import json
22
from typing import Any, Callable, List
33

4-
from agents.exceptions import (InputGuardrailTripwireTriggered,
5-
OutputGuardrailTripwireTriggered)
6-
from agents.run import Runner
74
from importlib_metadata import version as v
85
from langtrace.trace_attributes import FrameworkSpanAttributes, SpanAttributes
96
from opentelemetry import baggage, trace
@@ -18,6 +15,29 @@
1815
set_usage_attributes)
1916

2017

18+
# Define dummy classes to use when imports fail
19+
class DummyRunner:
20+
pass
21+
22+
23+
class DummyException(Exception):
24+
pass
25+
26+
27+
# Try importing from openai-agents package
28+
try:
29+
from agents.exceptions import (InputGuardrailTripwireTriggered,
30+
OutputGuardrailTripwireTriggered)
31+
from agents.run import Runner
32+
OPENAI_AGENTS_AVAILABLE = True
33+
except ImportError:
34+
# Define dummy classes if imports fail
35+
InputGuardrailTripwireTriggered = DummyException
36+
OutputGuardrailTripwireTriggered = DummyException
37+
Runner = DummyRunner
38+
OPENAI_AGENTS_AVAILABLE = False
39+
40+
2141
def extract_agent_details(agent_or_handoff):
2242
"""Extract relevant details from an agent/handoff and its handoffs."""
2343
try:
@@ -70,6 +90,10 @@ def extract_handoff_details(handoff):
7090

7191
def get_handoffs(version: str, tracer: Tracer) -> Callable:
7292
"""Wrap the `prompt` method of the `TLM` class to trace it."""
93+
if not OPENAI_AGENTS_AVAILABLE:
94+
def noop_traced_method(wrapped: Callable, instance: Any, args: List[Any], kwargs: Any) -> Any:
95+
return wrapped(*args, **kwargs)
96+
return noop_traced_method
7397

7498
def traced_method(
7599
wrapped: Callable,
@@ -117,7 +141,8 @@ def traced_method(
117141
attributes = FrameworkSpanAttributes(**span_attributes)
118142

119143
with tracer.start_as_current_span(
120-
name=f"openai_agents.available_handoffs", kind=SpanKind.CLIENT
144+
name="openai_agents.available_handoffs",
145+
kind=SpanKind.CLIENT
121146
) as span:
122147
try:
123148
set_span_attributes(span, attributes)
@@ -157,12 +182,11 @@ def traced_method(
157182
pass # Silently fail if error recording fails
158183
raise # Re-raise the original error since it's from the wrapped function
159184

160-
except Exception as outer_err:
161-
# If anything fails in our instrumentation wrapper, catch it and return control to the wrapped function
185+
except Exception:
162186
try:
163187
return wrapped(*args, **kwargs)
164188
except Exception as wrapped_err:
165-
raise wrapped_err # Only raise errors from the wrapped function
189+
raise wrapped_err
166190

167191
return traced_method
168192

@@ -328,6 +352,10 @@ def extract_run_config(config):
328352

329353
def get_new_response(version: str, tracer: Tracer) -> Callable:
330354
"""Wrap the _get_new_response method to trace inputs and outputs."""
355+
if not OPENAI_AGENTS_AVAILABLE:
356+
async def noop_traced_method(wrapped: Callable, instance: Any, args: List[Any], kwargs: Any) -> Any:
357+
return await wrapped(*args, **kwargs)
358+
return noop_traced_method
331359

332360
async def traced_method(
333361
wrapped: Callable,
@@ -524,7 +552,7 @@ async def traced_method(
524552

525553
raise
526554

527-
except Exception as outer_err:
555+
except Exception: # Remove outer_err since it's unused
528556
try:
529557
return await wrapped(*args, **kwargs)
530558
except Exception as wrapped_err:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.8.5"
1+
__version__ = "3.8.6"

0 commit comments

Comments
 (0)