From 92d85555fe48715214e51e839db4dfc3c24d1dcd Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Tue, 11 Mar 2025 22:04:17 -0700 Subject: [PATCH] fix(invoke_handler.py): fix converse chunk parsing to only return empty dict on tool use Fixes https://github.com/BerriAI/litellm/issues/9127 --- litellm/llms/bedrock/chat/invoke_handler.py | 4 +++- tests/llm_translation/test_anthropic_completion.py | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/litellm/llms/bedrock/chat/invoke_handler.py b/litellm/llms/bedrock/chat/invoke_handler.py index 27289164f7..44e5b40380 100644 --- a/litellm/llms/bedrock/chat/invoke_handler.py +++ b/litellm/llms/bedrock/chat/invoke_handler.py @@ -1231,7 +1231,9 @@ class AWSEventStreamDecoder: if len(self.content_blocks) == 0: 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 for block in self.content_blocks: diff --git a/tests/llm_translation/test_anthropic_completion.py b/tests/llm_translation/test_anthropic_completion.py index ce7f8f95b5..da47e745e7 100644 --- a/tests/llm_translation/test_anthropic_completion.py +++ b/tests/llm_translation/test_anthropic_completion.py @@ -992,8 +992,8 @@ def test_anthropic_thinking_output(model): @pytest.mark.parametrize( "model", [ - "anthropic/claude-3-7-sonnet-20250219", - # "bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0", + # "anthropic/claude-3-7-sonnet-20250219", + "bedrock/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 signature_block_exists = False + tool_call_exists = False for chunk in resp: print(f"chunk 2: {chunk}") + if chunk.choices[0].delta.tool_calls: + tool_call_exists = True if ( hasattr(chunk.choices[0].delta, "thinking_blocks") 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]) if chunk.choices[0].delta.thinking_blocks[0].get("signature"): signature_block_exists = True + assert not tool_call_exists assert reasoning_content_exists assert signature_block_exists except litellm.Timeout: