fix(invoke_handler.py): fix converse chunk parsing to only return empty dict on tool use

Fixes https://github.com/BerriAI/litellm/issues/9127
This commit is contained in:
Krrish Dholakia 2025-03-11 22:04:17 -07:00
parent 1051478e95
commit 92d85555fe
2 changed files with 9 additions and 3 deletions

View file

@ -1231,7 +1231,9 @@ class AWSEventStreamDecoder:
if len(self.content_blocks) == 0: if len(self.content_blocks) == 0:
return False return False
if "text" in self.content_blocks[0]: if (
"toolUse" not in self.content_blocks[0]
): # be explicit - only do this if tool use block, as this is to prevent json decoding errors
return False return False
for block in self.content_blocks: for block in self.content_blocks:

View file

@ -992,8 +992,8 @@ def test_anthropic_thinking_output(model):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"model", "model",
[ [
"anthropic/claude-3-7-sonnet-20250219", # "anthropic/claude-3-7-sonnet-20250219",
# "bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0", "bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0",
# "bedrock/invoke/us.anthropic.claude-3-7-sonnet-20250219-v1:0", # "bedrock/invoke/us.anthropic.claude-3-7-sonnet-20250219-v1:0",
], ],
) )
@ -1011,8 +1011,11 @@ def test_anthropic_thinking_output_stream(model):
reasoning_content_exists = False reasoning_content_exists = False
signature_block_exists = False signature_block_exists = False
tool_call_exists = False
for chunk in resp: for chunk in resp:
print(f"chunk 2: {chunk}") print(f"chunk 2: {chunk}")
if chunk.choices[0].delta.tool_calls:
tool_call_exists = True
if ( if (
hasattr(chunk.choices[0].delta, "thinking_blocks") hasattr(chunk.choices[0].delta, "thinking_blocks")
and chunk.choices[0].delta.thinking_blocks is not None and chunk.choices[0].delta.thinking_blocks is not None
@ -1025,6 +1028,7 @@ def test_anthropic_thinking_output_stream(model):
print(chunk.choices[0].delta.thinking_blocks[0]) print(chunk.choices[0].delta.thinking_blocks[0])
if chunk.choices[0].delta.thinking_blocks[0].get("signature"): if chunk.choices[0].delta.thinking_blocks[0].get("signature"):
signature_block_exists = True signature_block_exists = True
assert not tool_call_exists
assert reasoning_content_exists assert reasoning_content_exists
assert signature_block_exists assert signature_block_exists
except litellm.Timeout: except litellm.Timeout: