Commit 710f6be
fix(litellm): preserve signature-only blocks for Anthropic streaming thinking aggregation
Key changes:
* **Streaming Signature Preservation:** When Anthropic thinking mode is enabled and responses are streamed via LiteLLM/OpenAI format, Anthropic splits thinking blocks across deltas: text-only chunks first (`signature: None`), followed by a final signature-only chunk at `block_stop` with empty text (`thinking: ""`). This change ensures signature-only deltas are preserved rather than discarded.
* **Signal Detection & Part Conversion:** Updated `_has_meaningful_signal` to recognize `thinking_blocks` so signature-only deltas are not dropped as empty noise. Updated `_convert_reasoning_value_to_parts` to preserve blocks when `signature` is present, storing the cryptographic signature on `Part.thought_signature`.
* **Outbound Thought Aggregation:** Added `_aggregate_streaming_thought_parts` helper in `lite_llm.py` and integrated it into `_content_to_message_param` (Anthropic branch). It joins fragmented text chunks and attaches the cryptographic signature from whichever part carries it, emitting a clean `thinking_blocks` entry for multi-turn session history.
* **Evidence & Verification:** Added unit test `test_aggregate_streaming_thought_parts` and updated `test_content_to_message_param_anthropic_aggregates_streaming_split_thinking` to verify that streaming deltas (split text chunks + signature chunk `ErEDClsIDBACGAIfull`) are re-joined into a valid Anthropic `thinking_block` with base64 signature (`RXJFRENsc0lEQkFDR0FJZnVsbA==`). Verified 321 unit tests and 957 internal TAP targets pass 100%.
Note for callers:
This fixes continuity of Anthropic extended thinking across streaming responses that include tool use or multi-turn chats. Previously, after a tool call or first streaming turn, thinking blocks were rejected by Anthropic API with a 400 Bad Request error (`missing thinking signature`) because the signature was discarded during streaming.
Fixes: #4801 Closes: #5437
Co-authored-by: Yi Liu <yiliuly@google.com>
COPYBARA_INTEGRATE_REVIEW=#5437 from gurjot-05:fix/anthropic-thinking-streaming-continuity 0ce04f1
PiperOrigin-RevId: 9443059121 parent 757ef22 commit 710f6be
2 files changed
Lines changed: 184 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
543 | 543 | | |
544 | 544 | | |
545 | 545 | | |
546 | | - | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
547 | 550 | | |
548 | | - | |
549 | 551 | | |
550 | 552 | | |
551 | 553 | | |
| |||
565 | 567 | | |
566 | 568 | | |
567 | 569 | | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
568 | 607 | | |
569 | 608 | | |
570 | 609 | | |
| |||
1023 | 1062 | | |
1024 | 1063 | | |
1025 | 1064 | | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
1026 | 1069 | | |
| 1070 | + | |
1027 | 1071 | | |
1028 | | - | |
| 1072 | + | |
1029 | 1073 | | |
1030 | 1074 | | |
1031 | 1075 | | |
| |||
1877 | 1921 | | |
1878 | 1922 | | |
1879 | 1923 | | |
| 1924 | + | |
1880 | 1925 | | |
1881 | 1926 | | |
1882 | 1927 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
5396 | 5397 | | |
5397 | 5398 | | |
5398 | 5399 | | |
5399 | | - | |
5400 | | - | |
| 5400 | + | |
| 5401 | + | |
| 5402 | + | |
| 5403 | + | |
| 5404 | + | |
| 5405 | + | |
| 5406 | + | |
5401 | 5407 | | |
5402 | 5408 | | |
5403 | 5409 | | |
| 5410 | + | |
| 5411 | + | |
| 5412 | + | |
| 5413 | + | |
| 5414 | + | |
5404 | 5415 | | |
5405 | 5416 | | |
5406 | | - | |
5407 | | - | |
| 5417 | + | |
| 5418 | + | |
| 5419 | + | |
| 5420 | + | |
| 5421 | + | |
| 5422 | + | |
| 5423 | + | |
| 5424 | + | |
| 5425 | + | |
| 5426 | + | |
| 5427 | + | |
| 5428 | + | |
| 5429 | + | |
| 5430 | + | |
| 5431 | + | |
| 5432 | + | |
| 5433 | + | |
| 5434 | + | |
| 5435 | + | |
| 5436 | + | |
| 5437 | + | |
| 5438 | + | |
| 5439 | + | |
| 5440 | + | |
| 5441 | + | |
5408 | 5442 | | |
5409 | 5443 | | |
5410 | 5444 | | |
| |||
5478 | 5512 | | |
5479 | 5513 | | |
5480 | 5514 | | |
| 5515 | + | |
| 5516 | + | |
| 5517 | + | |
| 5518 | + | |
| 5519 | + | |
| 5520 | + | |
| 5521 | + | |
| 5522 | + | |
| 5523 | + | |
| 5524 | + | |
| 5525 | + | |
| 5526 | + | |
| 5527 | + | |
| 5528 | + | |
| 5529 | + | |
| 5530 | + | |
| 5531 | + | |
| 5532 | + | |
| 5533 | + | |
| 5534 | + | |
| 5535 | + | |
| 5536 | + | |
| 5537 | + | |
| 5538 | + | |
| 5539 | + | |
| 5540 | + | |
| 5541 | + | |
5481 | 5542 | | |
5482 | 5543 | | |
5483 | 5544 | | |
| |||
5577 | 5638 | | |
5578 | 5639 | | |
5579 | 5640 | | |
5580 | | - | |
| 5641 | + | |
5581 | 5642 | | |
5582 | 5643 | | |
5583 | 5644 | | |
| |||
5678 | 5739 | | |
5679 | 5740 | | |
5680 | 5741 | | |
| 5742 | + | |
| 5743 | + | |
| 5744 | + | |
| 5745 | + | |
| 5746 | + | |
| 5747 | + | |
| 5748 | + | |
| 5749 | + | |
| 5750 | + | |
| 5751 | + | |
| 5752 | + | |
| 5753 | + | |
| 5754 | + | |
| 5755 | + | |
| 5756 | + | |
| 5757 | + | |
| 5758 | + | |
| 5759 | + | |
| 5760 | + | |
| 5761 | + | |
| 5762 | + | |
| 5763 | + | |
| 5764 | + | |
| 5765 | + | |
| 5766 | + | |
| 5767 | + | |
| 5768 | + | |
| 5769 | + | |
| 5770 | + | |
| 5771 | + | |
| 5772 | + | |
| 5773 | + | |
| 5774 | + | |
| 5775 | + | |
| 5776 | + | |
| 5777 | + | |
| 5778 | + | |
| 5779 | + | |
| 5780 | + | |
| 5781 | + | |
| 5782 | + | |
| 5783 | + | |
| 5784 | + | |
| 5785 | + | |
| 5786 | + | |
| 5787 | + | |
| 5788 | + | |
| 5789 | + | |
| 5790 | + | |
| 5791 | + | |
| 5792 | + | |
| 5793 | + | |
| 5794 | + | |
| 5795 | + | |
| 5796 | + | |
| 5797 | + | |
| 5798 | + | |
| 5799 | + | |
| 5800 | + | |
| 5801 | + | |
| 5802 | + | |
| 5803 | + | |
| 5804 | + | |
| 5805 | + | |
| 5806 | + | |
| 5807 | + | |
| 5808 | + | |
| 5809 | + | |
| 5810 | + | |
| 5811 | + | |
5681 | 5812 | | |
5682 | 5813 | | |
5683 | 5814 | | |
| |||
0 commit comments