@@ -439,18 +439,28 @@ def _translate_openai_content_to_anthropic(
439
439
elif choice .message .thinking_blocks is not None :
440
440
for thinking_block in choice .message .thinking_blocks :
441
441
if "thinking" in thinking_block and "signature" in thinking_block :
442
+ thinking = thinking_block .get ("thinking" )
443
+ signature = thinking_block .get ("signature" )
444
+
445
+ assert isinstance (thinking , str )
446
+ assert isinstance (signature , str ) or signature is None
447
+
442
448
new_content .append (
443
449
AnthropicResponseContentBlockThinking (
444
450
type = "thinking" ,
445
- thinking = thinking_block . get ( " thinking" ) or "" ,
446
- signature = thinking_block . get ( " signature" ) or "" ,
451
+ thinking = thinking ,
452
+ signature = signature ,
447
453
)
448
454
)
449
455
elif "data" in thinking_block :
456
+ data = thinking_block .get ("data" )
457
+
458
+ assert isinstance (data , str )
459
+
450
460
new_content .append (
451
461
AnthropicResponseContentBlockRedactedThinking (
452
462
type = "redacted_thinking" ,
453
- data = thinking_block . get ( " data" , "" ) ,
463
+ data = data ,
454
464
)
455
465
)
456
466
@@ -525,16 +535,22 @@ def _translate_streaming_openai_chunk_to_anthropic_content_block(
525
535
):
526
536
thinking_blocks = choice .delta .thinking_blocks or []
527
537
if len (thinking_blocks ) > 0 :
528
- thinking = thinking_blocks [0 ].get ("thinking" ) or ""
529
- signature = thinking_blocks [0 ].get ("signature" ) or ""
530
-
531
- if thinking and signature :
532
- raise ValueError ("Both `thinking` and `signature` in a single streaming chunk isn't supported." )
533
- return "thinking" , ChatCompletionThinkingBlock (
534
- type = "thinking" ,
535
- thinking = thinking ,
536
- signature = signature
537
- )
538
+ thinking_block = thinking_blocks [0 ]
539
+ if thinking_block ["type" ] == "thinking" :
540
+ thinking = thinking_block .get ("thinking" ) or ""
541
+ signature = thinking_block .get ("signature" ) or ""
542
+
543
+ assert isinstance (thinking , str )
544
+ assert isinstance (signature , str )
545
+
546
+ if thinking and signature :
547
+ raise ValueError ("Both `thinking` and `signature` in a single streaming chunk isn't supported." )
548
+
549
+ return "thinking" , ChatCompletionThinkingBlock (
550
+ type = "thinking" ,
551
+ thinking = thinking ,
552
+ signature = signature
553
+ )
538
554
539
555
540
556
return "text" , TextBlock (type = "text" , text = "" )
@@ -564,8 +580,16 @@ def _translate_streaming_openai_chunk_to_anthropic(
564
580
elif isinstance (choice , StreamingChoices ) and hasattr (choice .delta , "thinking_blocks" ):
565
581
thinking_blocks = choice .delta .thinking_blocks or []
566
582
if len (thinking_blocks ) > 0 :
567
- reasoning_content += thinking_blocks [0 ].get ("thinking" ) or ""
568
- reasoning_signature += thinking_blocks [0 ].get ("signature" ) or ""
583
+ for thinking_block in thinking_blocks :
584
+ if thinking_block ["type" ] == "thinking" :
585
+ thinking = thinking_block .get ("thinking" ) or ""
586
+ signature = thinking_block .get ("signature" ) or ""
587
+
588
+ assert isinstance (thinking , str )
589
+ assert isinstance (signature , str )
590
+
591
+ reasoning_content += thinking
592
+ reasoning_signature += signature
569
593
570
594
if reasoning_content and reasoning_signature :
571
595
raise ValueError ("Both `reasoning` and `signature` in a single streaming chunk isn't supported." )
0 commit comments