Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions litellm/llms/anthropic/chat/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,17 +702,7 @@ def _content_block_delta_helper(

signature: Final = content_block["delta"].get("signature")
if isinstance(signature, str) and signature:
thinking_blocks = [
ChatCompletionThinkingBlock(
type="thinking",
thinking="".join(
cast(str, block["delta"].get("thinking"))
for block in self.content_blocks
if isinstance(block["delta"].get("thinking"), str)
),
signature=signature,
)
]
thinking_blocks = [ChatCompletionThinkingBlock(type="thinking", thinking="", signature=signature)]
provider_specific_fields["thinking_blocks"] = thinking_blocks
if reasoning_content is None:
reasoning_content = ""
Expand Down
76 changes: 69 additions & 7 deletions tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import litellm
from litellm._uuid import uuid
from litellm.constants import RESPONSE_FORMAT_TOOL_NAME
from litellm.litellm_core_utils.prompt_templates.factory import anthropic_messages_pt
from litellm.llms.anthropic.chat.handler import ModelResponseIterator, make_call
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.types.llms.openai import (
Expand Down Expand Up @@ -206,22 +207,83 @@ def test_streaming_thinking_blocks_are_replayable_after_signature_delta():
{"type": "thinking", "thinking": "Step 1. "},
{"type": "thinking", "thinking": "Step 2."},
)
expected_thinking_block = {
"type": "thinking",
"thinking": "Step 1. Step 2.",
"signature": "sig-final",
}
expected_signature_block = {"type": "thinking", "thinking": "", "signature": "sig-final"}

assert reasoning_content == "Step 1. Step 2."
assert thinking_blocks == (*expected_delta_blocks, expected_thinking_block)
assert thinking_blocks == (*expected_delta_blocks, expected_signature_block)
assert "".join(block.get("thinking") or "" for block in thinking_blocks) == reasoning_content
assert parsed_chunks[1].choices[0].delta.provider_specific_fields == {
"thinking_blocks": [expected_delta_blocks[0]]
}
assert parsed_chunks[-1].choices[0].delta.provider_specific_fields == {
"thinking_blocks": [expected_thinking_block]
"thinking_blocks": [expected_signature_block]
}


def test_streamed_signed_thinking_round_trips_to_the_next_turn_once():
iterator: Final = ModelResponseIterator(streaming_response=MagicMock(), sync_stream=True, json_mode=False)
thinking_parts: Final = ("Paris needs both tools. ", "Call weather first.")
thinking_text: Final = "".join(thinking_parts)
events: Final = (
{
"type": "message_start",
"message": {
"id": "msg_paris",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4-5",
"content": [],
"stop_reason": None,
"usage": {"input_tokens": 20, "output_tokens": 1},
},
},
{"type": "content_block_start", "index": 0, "content_block": {"type": "thinking", "thinking": ""}},
{"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": thinking_parts[0]}},
{"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": thinking_parts[1]}},
{"type": "content_block_delta", "index": 0, "delta": {"type": "signature_delta", "signature": "sig-paris"}},
{"type": "content_block_stop", "index": 0},
{
"type": "content_block_start",
"index": 1,
"content_block": {"type": "tool_use", "id": "toolu_paris", "name": "get_weather", "input": {}},
},
{
"type": "content_block_delta",
"index": 1,
"delta": {"type": "input_json_delta", "partial_json": '{"city": "Paris"}'},
},
{"type": "content_block_stop", "index": 1},
{"type": "message_delta", "delta": {"stop_reason": "tool_use", "stop_sequence": None}, "usage": {"output_tokens": 30}},
{"type": "message_stop"},
)
user_message: Final = {"role": "user", "content": "What's the weather in Paris?"}

streamed: Final = litellm.stream_chunk_builder(
chunks=[iterator.chunk_parser(event) for event in events], messages=[user_message]
)
assistant: Final = streamed.choices[0].message

assert assistant.reasoning_content == thinking_text
assert assistant.thinking_blocks == [{"type": "thinking", "thinking": thinking_text, "signature": "sig-paris"}]
assert [call.id for call in assistant.tool_calls] == ["toolu_paris"]

saved_history: Final = json.loads(
json.dumps(
[
user_message,
assistant.model_dump(),
{"role": "tool", "tool_call_id": "toolu_paris", "content": "22C and sunny"},
]
)
)
replayed: Final = anthropic_messages_pt(messages=saved_history, model="claude-sonnet-4-5", llm_provider="anthropic")

assert replayed[1]["content"][0] == {"type": "thinking", "thinking": thinking_text, "signature": "sig-paris"}
replayed_tool_use_ids: Final = [block["id"] for block in replayed[1]["content"] if block["type"] == "tool_use"]
assert replayed_tool_use_ids == ["toolu_paris"]
assert replayed[2]["content"][0]["type"] == "tool_result"


def test_streaming_unsigned_thinking_deltas_keep_reasoning_content():
model_response_iterator = ModelResponseIterator(
streaming_response=MagicMock(), sync_stream=True, json_mode=False
Expand Down
Loading