11import inspect
2- from typing import Optional , Any , Tuple , Dict , Generator , AsyncGenerator
2+ from typing import Optional , Any , Tuple , Generator , AsyncGenerator
33import uuid
44
55from langfuse .client import (
@@ -39,16 +39,10 @@ class LangfuseSpan(BaseSpan):
3939
4040
4141class LlamaIndexSpanHandler (BaseSpanHandler [LangfuseSpan ], extra = "allow" ):
42- def __init__ (
43- self ,
44- * ,
45- langfuse_client : Langfuse ,
46- observation_updates : Dict [str , Dict [str , Any ]],
47- ):
42+ def __init__ (self , * , langfuse_client : Langfuse ):
4843 super ().__init__ ()
4944
5045 self ._langfuse_client = langfuse_client
51- self ._observation_updates = observation_updates
5246 self ._context = InstrumentorContext ()
5347
5448 def new_span (
@@ -109,9 +103,6 @@ def new_span(
109103 metadata = kwargs ,
110104 )
111105
112- # Initialize observation update for the span to be populated by event handler
113- self ._observation_updates [id_ ] = {}
114-
115106 def prepare_to_exit_span (
116107 self ,
117108 id_ : str ,
@@ -122,7 +113,6 @@ def prepare_to_exit_span(
122113 ) -> Optional [LangfuseSpan ]:
123114 logger .debug (f"Exiting span { instance .__class__ .__name__ } with ID { id_ } " )
124115
125- observation_updates = self ._observation_updates .pop (id_ , {})
126116 output , metadata = self ._parse_output_metadata (instance , result )
127117
128118 # Reset the context root if the span is the root span
@@ -138,15 +128,13 @@ def prepare_to_exit_span(
138128 if self ._is_generation (id_ , instance ):
139129 generationClient = self ._get_generation_client (id_ )
140130 generationClient .end (
141- ** observation_updates ,
142131 output = output ,
143132 metadata = metadata ,
144133 )
145134
146135 else :
147136 spanClient = self ._get_span_client (id_ )
148137 spanClient .end (
149- ** observation_updates ,
150138 output = output ,
151139 metadata = metadata ,
152140 )
@@ -161,8 +149,6 @@ def prepare_to_drop_span(
161149 ) -> Optional [LangfuseSpan ]:
162150 logger .debug (f"Dropping span { instance .__class__ .__name__ } with ID { id_ } " )
163151
164- observation_updates = self ._observation_updates .pop (id_ , {})
165-
166152 # Reset the context root if the span is the root span
167153 if id_ == self ._context .root_llama_index_span_id :
168154 if self ._context .update_parent :
@@ -177,15 +163,13 @@ def prepare_to_drop_span(
177163 if self ._is_generation (id_ , instance ):
178164 generationClient = self ._get_generation_client (id_ )
179165 generationClient .end (
180- ** observation_updates ,
181166 level = "ERROR" ,
182167 status_message = str (err ),
183168 )
184169
185170 else :
186171 spanClient = self ._get_span_client (id_ )
187172 spanClient .end (
188- ** observation_updates ,
189173 level = "ERROR" ,
190174 status_message = str (err ),
191175 )
@@ -217,7 +201,10 @@ def _is_generation(self, id_: str, instance: Optional[Any] = None) -> bool:
217201 def _get_generation_client (self , id : str ) -> StatefulGenerationClient :
218202 trace_id = self ._context .trace_id
219203 if trace_id is None :
220- raise ValueError ("Trace ID is not set" )
204+ logger .warning (
205+ "Trace ID is not set. Creating generation client with new trace id."
206+ )
207+ trace_id = str (uuid .uuid4 ())
221208
222209 return StatefulGenerationClient (
223210 client = self ._langfuse_client .client ,
@@ -230,7 +217,10 @@ def _get_generation_client(self, id: str) -> StatefulGenerationClient:
230217 def _get_span_client (self , id : str ) -> StatefulSpanClient :
231218 trace_id = self ._context .trace_id
232219 if trace_id is None :
233- raise ValueError ("Trace ID is not set" )
220+ logger .warning (
221+ "Trace ID is not set. Creating generation client with new trace id."
222+ )
223+ trace_id = str (uuid .uuid4 ())
234224
235225 return StatefulSpanClient (
236226 client = self ._langfuse_client .client ,
0 commit comments