|
1 | 1 | import json |
2 | 2 | from typing import Any, Callable, List |
3 | 3 |
|
4 | | -from agents.exceptions import (InputGuardrailTripwireTriggered, |
5 | | - OutputGuardrailTripwireTriggered) |
6 | | -from agents.run import Runner |
7 | 4 | from importlib_metadata import version as v |
8 | 5 | from langtrace.trace_attributes import FrameworkSpanAttributes, SpanAttributes |
9 | 6 | from opentelemetry import baggage, trace |
|
18 | 15 | set_usage_attributes) |
19 | 16 |
|
20 | 17 |
|
| 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 | + |
21 | 41 | def extract_agent_details(agent_or_handoff): |
22 | 42 | """Extract relevant details from an agent/handoff and its handoffs.""" |
23 | 43 | try: |
@@ -70,6 +90,10 @@ def extract_handoff_details(handoff): |
70 | 90 |
|
71 | 91 | def get_handoffs(version: str, tracer: Tracer) -> Callable: |
72 | 92 | """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 |
73 | 97 |
|
74 | 98 | def traced_method( |
75 | 99 | wrapped: Callable, |
@@ -117,7 +141,8 @@ def traced_method( |
117 | 141 | attributes = FrameworkSpanAttributes(**span_attributes) |
118 | 142 |
|
119 | 143 | 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 |
121 | 146 | ) as span: |
122 | 147 | try: |
123 | 148 | set_span_attributes(span, attributes) |
@@ -157,12 +182,11 @@ def traced_method( |
157 | 182 | pass # Silently fail if error recording fails |
158 | 183 | raise # Re-raise the original error since it's from the wrapped function |
159 | 184 |
|
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: |
162 | 186 | try: |
163 | 187 | return wrapped(*args, **kwargs) |
164 | 188 | except Exception as wrapped_err: |
165 | | - raise wrapped_err # Only raise errors from the wrapped function |
| 189 | + raise wrapped_err |
166 | 190 |
|
167 | 191 | return traced_method |
168 | 192 |
|
@@ -328,6 +352,10 @@ def extract_run_config(config): |
328 | 352 |
|
329 | 353 | def get_new_response(version: str, tracer: Tracer) -> Callable: |
330 | 354 | """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 |
331 | 359 |
|
332 | 360 | async def traced_method( |
333 | 361 | wrapped: Callable, |
@@ -524,7 +552,7 @@ async def traced_method( |
524 | 552 |
|
525 | 553 | raise |
526 | 554 |
|
527 | | - except Exception as outer_err: |
| 555 | + except Exception: # Remove outer_err since it's unused |
528 | 556 | try: |
529 | 557 | return await wrapped(*args, **kwargs) |
530 | 558 | except Exception as wrapped_err: |
|
0 commit comments