4
4
from typing import Any
5
5
from typing import Dict
6
6
from typing import List
7
- from typing import Set
8
7
from typing import Optional
8
+ from typing import Set
9
9
from typing import Union
10
10
from weakref import WeakKeyDictionary
11
11
@@ -227,11 +227,16 @@ def _set_input_links(self, instance: Any, span: Span, parent_span: Union[Span, N
227
227
links = []
228
228
229
229
if not is_step :
230
- self ._set_span_links (span , [{
231
- "trace_id" : "{:x}" .format (span .trace_id ),
232
- "span_id" : str (invoker_spans [0 ].span_id ),
233
- "attributes" : invoker_links_attributes [0 ],
234
- }])
230
+ self ._set_span_links (
231
+ span ,
232
+ [
233
+ {
234
+ "trace_id" : "{:x}" .format (span .trace_id ),
235
+ "span_id" : str (invoker_spans [0 ].span_id ),
236
+ "attributes" : invoker_links_attributes [0 ],
237
+ }
238
+ ],
239
+ )
235
240
236
241
return step_idx
237
242
@@ -250,7 +255,7 @@ def _set_input_links(self, instance: Any, span: Span, parent_span: Union[Span, N
250
255
invoker_span = self ._spans [id (step )]
251
256
invoker_link_attributes = {"from" : "output" , "to" : "input" }
252
257
break
253
- if isinstance (step , list ): # parallel steps in the list
258
+ if isinstance (step , list ): # parallel steps in the list
254
259
for parallel_step in step :
255
260
if id (parallel_step ) in self ._spans :
256
261
if not has_parallel_steps :
@@ -277,16 +282,16 @@ def _set_input_links(self, instance: Any, span: Span, parent_span: Union[Span, N
277
282
self ._set_span_links (span , links )
278
283
279
284
return step_idx
280
-
285
+
281
286
def _set_output_links (self , span : Span , parent_span : Union [Span , None ], step_idx : int ) -> None :
282
287
"""
283
288
Sets the output links for the parent span of the given span (to: output)
284
289
This is done by removing repeated span links from steps in a chain.
285
- We add output->output span links at every step
290
+ We add output->output span links at every step
286
291
"""
287
292
if parent_span is None :
288
293
return
289
-
294
+
290
295
parent_links = parent_span ._get_ctx_item (SPAN_LINKS ) or []
291
296
pop_indecies = self ._get_popped_span_link_indecies (parent_span , parent_links , step_idx )
292
297
parent_links = [link for i , link in enumerate (parent_links ) if i not in pop_indecies ]
@@ -303,7 +308,9 @@ def _set_output_links(self, span: Span, parent_span: Union[Span, None], step_idx
303
308
],
304
309
)
305
310
306
- def _get_popped_span_link_indecies (self , parent_span : Span , parent_links : List [Dict [str , Any ]], step_idx : int ) -> List [int ]:
311
+ def _get_popped_span_link_indecies (
312
+ self , parent_span : Span , parent_links : List [Dict [str , Any ]], step_idx : int
313
+ ) -> List [int ]:
307
314
"""
308
315
Returns a list of indecies to pop from the parent span links list
309
316
This is determined by if the parent span represents a chain, and if there are steps before the step
@@ -316,11 +323,11 @@ def _get_popped_span_link_indecies(self, parent_span: Span, parent_links: List[D
316
323
parent_instance = self ._instances .get (parent_span )
317
324
if not parent_instance :
318
325
return pop_indecies
319
-
326
+
320
327
parent_instance = _extract_bound (parent_instance )
321
328
if not hasattr (parent_instance , "steps" ): # chain instance
322
329
return pop_indecies
323
-
330
+
324
331
steps = getattr (parent_instance , "steps" , [])
325
332
flatmap_chain_steps = _flattened_chain_steps (steps )
326
333
for i in range (step_idx - 1 , - 1 , - 1 ):
@@ -338,19 +345,15 @@ def _get_popped_span_link_indecies(self, parent_span: Span, parent_links: List[D
338
345
if id (parallel_step ) in self ._spans :
339
346
invoker_span_id = self ._spans [id (parallel_step )].span_id
340
347
link_idx = next (
341
- (
342
- i
343
- for i , link in enumerate (parent_links )
344
- if link ["span_id" ] == str (invoker_span_id )
345
- ),
348
+ (i for i , link in enumerate (parent_links ) if link ["span_id" ] == str (invoker_span_id )),
346
349
None ,
347
350
)
348
351
if link_idx is not None :
349
352
pop_indecies .append (link_idx )
350
353
break
351
354
352
355
return pop_indecies
353
-
356
+
354
357
def _set_span_links (self , span : Span , links : List [Dict [str , Any ]]) -> None :
355
358
"""Sets the span links on the given span along with the existing links."""
356
359
existing_links = span ._get_ctx_item (SPAN_LINKS ) or []
@@ -452,7 +455,7 @@ def _llmobs_set_tags_from_chat_model(
452
455
content = (
453
456
message .get ("content" , "" ) if isinstance (message , dict ) else getattr (message , "content" , "" )
454
457
)
455
- role = getattr (message , "role" , ROLE_MAPPING .get (message . type , "" ))
458
+ role = getattr (message , "role" , ROLE_MAPPING .get (getattr ( message , " type" , None ) , "" ))
456
459
input_messages .append ({"content" : str (content ), "role" : str (role )})
457
460
span ._set_ctx_item (input_tag_key , input_messages )
458
461
0 commit comments