4040from  logfire ._internal .utils  import  handle_internal_errors , log_internal_error , truncate_string 
4141
4242if  TYPE_CHECKING :  # pragma: no cover 
43-     from  agents .tracing . setup  import  TraceProvider 
43+     from  agents .tracing  import  TraceProvider 
4444    from  openai .types .responses  import  Response 
4545
4646    from  logfire  import  Logfire , LogfireSpan 
@@ -130,18 +130,28 @@ def __getattr__(self, item: Any) -> Any:
130130
131131    @classmethod  
132132    def  install (cls , logfire_instance : Logfire ) ->  None :
133-         name  =  'GLOBAL_TRACE_PROVIDER' 
134-         original  =  getattr (agents .tracing , name )
135-         if  isinstance (original , cls ):
136-             return 
137-         wrapper  =  cls (original , logfire_instance )
138-         for  module_name , mod  in  sys .modules .items ():
139-             if  module_name .startswith ('agents' ):
140-                 try :
141-                     if  getattr (mod , name , None ) is  original :
142-                         setattr (mod , name , wrapper )
143-                 except  Exception :  # pragma: no cover 
144-                     pass 
133+         try :
134+             from  agents .tracing  import  get_trace_provider , set_trace_provider 
135+         except  ImportError :  # pragma: no cover 
136+             # Handle older versions of agents where these functions are not available 
137+             name  =  'GLOBAL_TRACE_PROVIDER' 
138+             original  =  getattr (agents .tracing , name )
139+             if  isinstance (original , cls ):
140+                 return 
141+             wrapper  =  cls (original , logfire_instance )
142+             for  module_name , mod  in  sys .modules .items ():
143+                 if  module_name .startswith ('agents' ):
144+                     try :
145+                         if  getattr (mod , name , None ) is  original :
146+                             setattr (mod , name , wrapper )
147+                     except  Exception :  # pragma: no cover 
148+                         pass 
149+         else :
150+             original  =  get_trace_provider ()
151+             if  isinstance (original , cls ):
152+                 return 
153+             wrapper  =  cls (original , logfire_instance )
154+             set_trace_provider (wrapper )  # type: ignore 
145155
146156
147157@dataclass  
@@ -185,13 +195,13 @@ class LogfireWrapperBase(Generic[T]):
185195    span_helper : LogfireSpanHelper 
186196    token : contextvars .Token [T  |  None ] |  None  =  None 
187197
188-     def  start (self , mark_as_current : bool  =  False ):
198+     def  start (self , mark_as_current : bool  =  False )  ->   None :
189199        self .span_helper .start (mark_as_current )
190200        if  mark_as_current :
191201            self .attach ()
192202        return  self .wrapped .start ()
193203
194-     def  finish (self , reset_current : bool  =  False ):
204+     def  finish (self , reset_current : bool  =  False )  ->   None :
195205        self .on_ending ()
196206        self .span_helper .end (reset_current )
197207        if  reset_current :
@@ -204,7 +214,7 @@ def __enter__(self) -> Self:
204214        self .attach ()
205215        return  self 
206216
207-     def  __exit__ (self , exc_type : type [BaseException ], exc_val : BaseException , exc_tb : TracebackType ):
217+     def  __exit__ (self , exc_type : type [BaseException ], exc_val : BaseException , exc_tb : TracebackType )  ->   None :
208218        self .on_ending ()
209219        self .span_helper .__exit__ (exc_type , exc_val , exc_tb )
210220        self .wrapped .finish ()
0 commit comments