@@ -607,30 +607,27 @@ def _prepare_event(
607607 event .get ("breadcrumbs" , []), {"len" : previous_total_breadcrumbs }
608608 )
609609
610- # Annotate truncated gen_ai messages in spans
611610 if scope is not None and scope ._gen_ai_messages_truncated :
612- spans = event .get ("spans" , [])
613- if isinstance (spans , AnnotatedValue ):
614- spans = spans .value
615-
611+ spans = [] # type: List[Dict[str, Any]]
616612 for span in spans :
617613 if isinstance (span , dict ):
618614 span_id = span .get ("span_id" )
619615 if span_id and span_id in scope ._gen_ai_messages_truncated :
620616 span_data = span .get ("data" , {})
621- original_count = span_data .pop (
622- "_gen_ai_messages_original_count" , None
623- )
624- if (
625- original_count is not None
626- and SPANDATA .GEN_AI_REQUEST_MESSAGES in span_data
627- ):
628- span_data [SPANDATA .GEN_AI_REQUEST_MESSAGES ] = (
629- AnnotatedValue (
630- span_data [SPANDATA .GEN_AI_REQUEST_MESSAGES ],
631- {"len" : original_count },
632- )
617+ if isinstance (span_data , dict ):
618+ original_count = span_data .pop (
619+ "_gen_ai_messages_original_count" , None
633620 )
621+ if (
622+ original_count is not None
623+ and SPANDATA .GEN_AI_REQUEST_MESSAGES in span_data
624+ ):
625+ span_data [SPANDATA .GEN_AI_REQUEST_MESSAGES ] = (
626+ AnnotatedValue (
627+ span_data [SPANDATA .GEN_AI_REQUEST_MESSAGES ],
628+ {"len" : original_count },
629+ )
630+ )
634631
635632 # Postprocess the event here so that annotated types do
636633 # generally not surface in before_send
0 commit comments