From fb4ff158554d5db56243ec20d671c125257c654a Mon Sep 17 00:00:00 2001 From: Vinicius Mello Date: Thu, 10 Jul 2025 11:30:33 -0300 Subject: [PATCH 1/2] feat(tracer): enhance OpenlayerTracerProcessor with dynamic base class and type hinting - Introduced a dynamic base class for `OpenlayerTracerProcessor` to handle the presence of the `agents` library, improving compatibility. - Added type hinting for tracing-related parameters and return types, enhancing code clarity and type safety. - Implemented an ImportError raise for better error handling when the `agents` library is not available. - Updated dictionary type annotations for improved type specificity. --- .../lib/integrations/openai_agents.py | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/openlayer/lib/integrations/openai_agents.py b/src/openlayer/lib/integrations/openai_agents.py index 17e713c1..e3c3cf38 100644 --- a/src/openlayer/lib/integrations/openai_agents.py +++ b/src/openlayer/lib/integrations/openai_agents.py @@ -4,16 +4,24 @@ import logging from pathlib import Path import time -from typing import Any, Dict, Optional, Union, List +from datetime import datetime +from typing import Any, Dict, Optional, Union, List, TYPE_CHECKING from ..tracing import tracer, steps, enums +if TYPE_CHECKING: + try: + from agents import tracing # type: ignore[import] + except ImportError: + # When agents isn't available, we'll use string literals for type annotations + pass + try: from agents import tracing # type: ignore[import] - HAVE_AGENTS = True except ImportError: HAVE_AGENTS = False + tracing = None # type: ignore[assignment] logger = logging.getLogger(__name__) @@ -582,7 +590,14 @@ def _configure_chat_completion_step( step.model_parameters = model_parameters or {} -class OpenlayerTracerProcessor(tracing.TracingProcessor): # type: ignore[no-redef] +# Dynamic base class to handle inheritance when agents is available +if HAVE_AGENTS: + _BaseProcessor = tracing.TracingProcessor # type: ignore[misc] +else: + _BaseProcessor = object # type: ignore[assignment,misc] + + +class OpenlayerTracerProcessor(_BaseProcessor): # type: ignore[misc] """Tracing processor for the `OpenAI Agents SDK `_. @@ -649,6 +664,12 @@ def __init__(self, **kwargs: Any) -> None: Args: **kwargs: Additional metadata to associate with all traces. """ + if not HAVE_AGENTS: + raise ImportError( + "The 'agents' library is required to use OpenlayerTracerProcessor. " + "Please install it with: pip install openai-agents" + ) + self.metadata: Dict[str, Any] = kwargs or {} self._active_traces: Dict[str, Dict[str, Any]] = {} self._active_steps: Dict[str, steps.Step] = {} @@ -676,7 +697,7 @@ def __init__(self, **kwargs: Any) -> None: global _active_openlayer_processor _active_openlayer_processor = self - def on_trace_start(self, trace: tracing.Trace) -> None: + def on_trace_start(self, trace: "tracing.Trace") -> None: """Handle the start of a trace (root agent workflow).""" try: # Get trace information @@ -693,7 +714,7 @@ def on_trace_start(self, trace: tracing.Trace) -> None: except Exception as e: logger.error(f"Failed to handle trace start: {e}") - def on_trace_end(self, trace: tracing.Trace) -> None: + def on_trace_end(self, trace: "tracing.Trace") -> None: """Handle the end of a trace (root agent workflow).""" try: trace_data = self._active_traces.pop(trace.trace_id, None) @@ -786,7 +807,7 @@ def on_trace_end(self, trace: tracing.Trace) -> None: except Exception as e: logger.error(f"Failed to handle trace end: {e}") - def on_span_start(self, span: tracing.Span) -> None: + def on_span_start(self, span: "tracing.Span") -> None: """Handle the start of a span (individual agent step).""" try: # Extract span attributes using helper function @@ -840,7 +861,7 @@ def on_span_start(self, span: tracing.Span) -> None: except Exception as e: logger.error(f"Failed to handle span start: {e}") - def on_span_end(self, span: tracing.Span) -> None: + def on_span_end(self, span: "tracing.Span") -> None: """Handle the end of a span (individual agent step).""" try: # Extract span attributes using helper function @@ -912,7 +933,7 @@ def on_span_end(self, span: tracing.Span) -> None: logger.error(f"Failed to handle span end: {e}") def _create_step_for_span( - self, span: tracing.Span, span_data: Any + self, span: "tracing.Span", span_data: Any ) -> Optional[steps.Step]: """Create the appropriate Openlayer step for a span.""" try: @@ -1315,7 +1336,7 @@ def _create_generic_step( step.start_time = start_time return step - def _extract_usage_from_response(self, response: Any, field: str = None) -> int: + def _extract_usage_from_response(self, response: Any, field: Optional[str] = None) -> Union[int, Dict[str, int]]: """Extract usage information from response object.""" if not response: return 0 @@ -1339,7 +1360,7 @@ def _extract_usage_from_response(self, response: Any, field: str = None) -> int: } def _update_step_with_span_data( - self, step: steps.Step, span: tracing.Span, span_data: Any + self, step: steps.Step, span: "tracing.Span", span_data: Any ) -> None: """Update step with final span data.""" try: @@ -1650,7 +1671,7 @@ def _extract_actual_llm_output(self, span_data: Any) -> Optional[str]: except Exception: return None - def _cleanup_dict_with_warning(self, dict_obj: Dict, name: str) -> None: + def _cleanup_dict_with_warning(self, dict_obj: Dict[str, Any], name: str) -> None: """Helper to clean up dictionaries with warning logging.""" if dict_obj: dict_obj.clear() From 458b20f81af5b028432e5344e11fa6f73c6ca361 Mon Sep 17 00:00:00 2001 From: Vinicius Mello Date: Thu, 10 Jul 2025 11:31:39 -0300 Subject: [PATCH 2/2] fix(tracer): update dictionary type annotation in OpenlayerTracerProcessor - Changed the type annotation of `dict_obj` in `_cleanup_dict_with_warning` from `Dict[str, Any]` to `Dict` for improved type specificity and clarity. --- src/openlayer/lib/integrations/openai_agents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openlayer/lib/integrations/openai_agents.py b/src/openlayer/lib/integrations/openai_agents.py index e3c3cf38..c4e5e040 100644 --- a/src/openlayer/lib/integrations/openai_agents.py +++ b/src/openlayer/lib/integrations/openai_agents.py @@ -1671,7 +1671,7 @@ def _extract_actual_llm_output(self, span_data: Any) -> Optional[str]: except Exception: return None - def _cleanup_dict_with_warning(self, dict_obj: Dict[str, Any], name: str) -> None: + def _cleanup_dict_with_warning(self, dict_obj: Dict, name: str) -> None: """Helper to clean up dictionaries with warning logging.""" if dict_obj: dict_obj.clear() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy