@@ -288,7 +288,6 @@ def __init__(self):
288288 assert len (result ) == len (sample_messages )
289289 assert result == sample_messages
290290 assert span .span_id not in scope ._gen_ai_messages_truncated
291- assert "_gen_ai_messages_original_count" not in span .data
292291
293292 def test_truncation_sets_metadata_on_scope (self , large_messages ):
294293 class MockSpan :
@@ -316,9 +315,8 @@ def __init__(self):
316315 assert len (result ) < len (large_messages )
317316 n_removed = original_count - len (result )
318317 assert scope ._gen_ai_messages_truncated [span .span_id ] == n_removed
319- assert span .data ["_gen_ai_messages_original_count" ] == original_count
320318
321- def test_metadata_contains_original_count (self , large_messages ):
319+ def test_scope_tracks_removed_messages (self , large_messages ):
322320 class MockSpan :
323321 def __init__ (self ):
324322 self .span_id = "test_span_id"
@@ -340,9 +338,9 @@ def __init__(self):
340338 large_messages , span , scope , max_bytes = small_limit
341339 )
342340
343- assert span .data ["_gen_ai_messages_original_count" ] == original_count
344341 n_removed = original_count - len (result )
345342 assert scope ._gen_ai_messages_truncated [span .span_id ] == n_removed
343+ assert len (result ) + n_removed == original_count
346344
347345 def test_empty_messages_returns_none (self ):
348346 class MockSpan :
@@ -391,7 +389,7 @@ def __init__(self):
391389
392390class TestClientAnnotation :
393391 def test_client_wraps_truncated_messages_in_annotated_value (self , large_messages ):
394- """Test that client.py properly wraps truncated messages in AnnotatedValue"""
392+ """Test that client.py properly wraps truncated messages in AnnotatedValue using scope data """
395393 from sentry_sdk ._types import AnnotatedValue
396394 from sentry_sdk .consts import SPANDATA
397395
@@ -418,27 +416,34 @@ def __init__(self):
418416 )
419417 span .set_data (SPANDATA .GEN_AI_REQUEST_MESSAGES , truncated_messages )
420418
421- # Verify metadata was set on scope and span
419+ # Verify metadata was set on scope
422420 assert span .span_id in scope ._gen_ai_messages_truncated
423421 assert scope ._gen_ai_messages_truncated [span .span_id ] > 0
424- assert "_gen_ai_messages_original_count" in span .data
425422
426423 # Simulate what client.py does
427424 event = {"spans" : [{"span_id" : span .span_id , "data" : span .data .copy ()}]}
428425
429- # Mimic client.py logic
426+ # Mimic client.py logic - using scope to get the removed count
430427 for event_span in event ["spans" ]:
428+ span_id = event_span .get ("span_id" )
431429 span_data = event_span .get ("data" , {})
432- orig_count = span_data .pop ("_gen_ai_messages_original_count" , None )
433- if orig_count is not None and SPANDATA .GEN_AI_REQUEST_MESSAGES in span_data :
430+ if (
431+ span_id
432+ and span_id in scope ._gen_ai_messages_truncated
433+ and SPANDATA .GEN_AI_REQUEST_MESSAGES in span_data
434+ ):
435+ messages = span_data [SPANDATA .GEN_AI_REQUEST_MESSAGES ]
436+ n_removed = scope ._gen_ai_messages_truncated [span_id ]
437+ n_remaining = len (messages ) if isinstance (messages , list ) else 0
438+ original_count_calculated = n_removed + n_remaining
439+
434440 span_data [SPANDATA .GEN_AI_REQUEST_MESSAGES ] = AnnotatedValue (
435- safe_serialize (span_data [ SPANDATA . GEN_AI_REQUEST_MESSAGES ] ),
436- {"len" : orig_count },
441+ safe_serialize (messages ),
442+ {"len" : original_count_calculated },
437443 )
438444
439445 # Verify the annotation happened
440446 messages_value = event ["spans" ][0 ]["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
441447 assert isinstance (messages_value , AnnotatedValue )
442448 assert messages_value .metadata ["len" ] == original_count
443449 assert isinstance (messages_value .value , str )
444- assert "_gen_ai_messages_original_count" not in event ["spans" ][0 ]["data" ]
0 commit comments