From 26f7a6b7a256686f5dec2677c98cbdbf9e6c9ec9 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 14 Nov 2024 13:12:39 -0800 Subject: [PATCH] unit testing for test_convert_tool_response_to_message_no_arguments --- .../test_anthropic_completion.py | 92 ++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/tests/llm_translation/test_anthropic_completion.py b/tests/llm_translation/test_anthropic_completion.py index c399c3a47..8a788e0fb 100644 --- a/tests/llm_translation/test_anthropic_completion.py +++ b/tests/llm_translation/test_anthropic_completion.py @@ -33,8 +33,10 @@ from litellm import ( ) from litellm.adapters.anthropic_adapter import anthropic_adapter from litellm.types.llms.anthropic import AnthropicResponse - +from litellm.types.utils import GenericStreamingChunk, ChatCompletionToolCallChunk +from litellm.types.llms.openai import ChatCompletionToolCallFunctionChunk from litellm.llms.anthropic.common_utils import process_anthropic_headers +from litellm.llms.anthropic.chat.handler import AnthropicChatCompletion from httpx import Headers from base_llm_unit_tests import BaseLLMChatTest @@ -694,3 +696,91 @@ class TestAnthropicCompletion(BaseLLMChatTest): assert _document_validation["type"] == "document" assert _document_validation["source"]["media_type"] == "application/pdf" assert _document_validation["source"]["type"] == "base64" + + +def test_convert_tool_response_to_message_with_values(): + """Test converting a tool response with 'values' key to a message""" + tool_calls = [ + ChatCompletionToolCallChunk( + id="test_id", + type="function", + function=ChatCompletionToolCallFunctionChunk( + name="json_tool_call", + arguments='{"values": {"name": "John", "age": 30}}', + ), + index=0, + ) + ] + + message = AnthropicChatCompletion._convert_tool_response_to_message( + tool_calls=tool_calls + ) + + assert message is not None + assert message.content == '{"name": "John", "age": 30}' + + +def test_convert_tool_response_to_message_without_values(): + """ + Test converting a tool response without 'values' key to a message + + Anthropic API returns the JSON schema in the tool call, OpenAI Spec expects it in the message. This test ensures that the tool call is converted to a message correctly. + + Relevant issue: https://github.com/BerriAI/litellm/issues/6741 + """ + tool_calls = [ + ChatCompletionToolCallChunk( + id="test_id", + type="function", + function=ChatCompletionToolCallFunctionChunk( + name="json_tool_call", arguments='{"name": "John", "age": 30}' + ), + index=0, + ) + ] + + message = AnthropicChatCompletion._convert_tool_response_to_message( + tool_calls=tool_calls + ) + + assert message is not None + assert message.content == '{"name": "John", "age": 30}' + + +def test_convert_tool_response_to_message_invalid_json(): + """Test converting a tool response with invalid JSON""" + tool_calls = [ + ChatCompletionToolCallChunk( + id="test_id", + type="function", + function=ChatCompletionToolCallFunctionChunk( + name="json_tool_call", arguments="invalid json" + ), + index=0, + ) + ] + + message = AnthropicChatCompletion._convert_tool_response_to_message( + tool_calls=tool_calls + ) + + assert message is not None + assert message.content == "invalid json" + + +def test_convert_tool_response_to_message_no_arguments(): + """Test converting a tool response with no arguments""" + tool_calls = [ + ChatCompletionToolCallChunk( + id="test_id", + type="function", + function=ChatCompletionToolCallFunctionChunk(name="json_tool_call"), + index=0, + ) + ] + + message = AnthropicChatCompletion._convert_tool_response_to_message( + tool_calls=tool_calls + ) + + assert message is None