fix(anthropic): don't drop text_delta when backend sends empty tool_calls

Some OpenAI-compatible backends (e.g. mlx_lm.server) always populate
tool_calls:[] in every streaming delta, even for plain-text responses.
The check `if choice.delta.tool_calls is not None` treats an empty
list as truthy, entering the tool_calls branch and overwriting
partial_json to "", which drops the actual text content.

Add a length check so empty tool_calls lists are ignored, allowing
text_delta events to flow through correctly.

Fixes BerriAI/litellm#25172

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
rudra717 2026-04-06 10:03:34 -07:00
parent 39c1042258
commit 573f678690

View file

@ -1418,7 +1418,7 @@ class LiteLLMAnthropicMessagesAdapter:
for choice in choices:
if choice.delta.content is not None and len(choice.delta.content) > 0:
text += choice.delta.content
if choice.delta.tool_calls is not None:
if choice.delta.tool_calls is not None and len(choice.delta.tool_calls) > 0:
partial_json = ""
for tool in choice.delta.tool_calls:
if (