@@ -524,13 +524,15 @@ def __init__(
524
524
stream_done_callback = None ,
525
525
):
526
526
super ().__init__ (response )
527
-
528
527
self ._stream_done_callback = stream_done_callback
529
528
self ._accumulating_body = {"generation" : "" }
529
+ self .last_chunk = None
530
530
531
531
def __iter__ (self ):
532
532
for event in self .__wrapped__ :
533
+ # Process the event
533
534
self ._process_event (event )
535
+ # Yield the original event immediately
534
536
yield event
535
537
536
538
def _process_event (self , event ):
@@ -545,7 +547,11 @@ def _process_event(self, event):
545
547
self ._stream_done_callback (decoded_chunk )
546
548
return
547
549
if "generation" in decoded_chunk :
548
- self ._accumulating_body ["generation" ] += decoded_chunk .get ("generation" )
550
+ generation = decoded_chunk .get ("generation" )
551
+ if self .last_chunk == generation :
552
+ return
553
+ self .last_chunk = generation
554
+ self ._accumulating_body ["generation" ] += generation
549
555
550
556
if type == "message_start" :
551
557
self ._accumulating_body = decoded_chunk .get ("message" )
@@ -554,9 +560,11 @@ def _process_event(self, event):
554
560
decoded_chunk .get ("content_block" )
555
561
)
556
562
elif type == "content_block_delta" :
557
- self ._accumulating_body ["content" ][- 1 ]["text" ] += decoded_chunk .get (
558
- "delta"
559
- ).get ("text" )
563
+ text = decoded_chunk .get ("delta" ).get ("text" )
564
+ if self .last_chunk == text :
565
+ return
566
+ self .last_chunk = text
567
+ self ._accumulating_body ["content" ][- 1 ]["text" ] += text
560
568
561
569
elif self .has_finished (type , decoded_chunk ):
562
570
self ._accumulating_body ["invocation_metrics" ] = decoded_chunk .get (
0 commit comments