Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion litellm/llms/anthropic/chat/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ def chunk_parser(self, chunk: dict) -> ModelResponseStream: # noqa: PLR0915
"type"
]
if content_block_start["content_block"]["type"] == "text":
text = content_block_start["content_block"]["text"]
text = content_block_start["content_block"].get("text", "")
elif (
content_block_start["content_block"]["type"] == "tool_use"
or content_block_start["content_block"]["type"] == "server_tool_use"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,26 @@ def test_current_content_block_type_tracking():
assert iterator.current_content_block_type is None


def test_content_block_start_text_field_missing():
"""
Some Anthropic-compatible providers emit content_block_start for text blocks
without the optional "text" field: {"type": "text"} instead of {"type": "text", "text": ""}.
chunk_parser should not raise KeyError and should treat it as an empty string.
Regression test for https://github.com/BerriAI/litellm/issues/28067
"""
iterator = ModelResponseIterator(
streaming_response=MagicMock(), sync_stream=True, json_mode=False
)
chunk = {
"type": "content_block_start",
"index": 0,
"content_block": {"type": "text"}, # "text" field intentionally absent
}
# should not raise KeyError
response = iterator.chunk_parser(chunk=chunk)
assert response.choices[0].delta.content == ""


def test_web_search_tool_result_captured_in_provider_specific_fields():
"""
Test that web_search_tool_result content is captured in provider_specific_fields.
Expand Down
Loading