diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py index 3c130aafcb..d50129bc7a 100644 --- a/litellm/llms/anthropic.py +++ b/litellm/llms/anthropic.py @@ -187,7 +187,7 @@ class AnthropicChatCompletion(BaseLLM): elif len(completion_response["content"]) == 0: raise AnthropicError( message="No content in response", - status_code=response.status_code, + status_code=500, ) else: text_content = "" diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py index c8a0918059..47056584b5 100644 --- a/litellm/tests/test_completion.py +++ b/litellm/tests/test_completion.py @@ -232,6 +232,73 @@ def test_completion_claude_3_function_call(): pytest.fail(f"Error occurred: {e}") +@pytest.mark.asyncio +async def test_anthropic_no_content_error(): + """ + https://github.com/BerriAI/litellm/discussions/3440#discussioncomment-9323402 + """ + try: + litellm.drop_params = True + response = await litellm.acompletion( + model="anthropic/claude-3-opus-20240229", + api_key=os.getenv("ANTHROPIC_API_KEY"), + messages=[ + { + "role": "system", + "content": "You will be given a list of fruits. Use the submitFruit function to submit a fruit. Don't say anything after.", + }, + {"role": "user", "content": "I like apples"}, + { + "content": "The most relevant tool for this request is the submitFruit function.", + "role": "assistant", + "tool_calls": [ + { + "function": { + "arguments": '{"name": "Apple"}', + "name": "submitFruit", + }, + "id": "toolu_012ZTYKWD4VqrXGXyE7kEnAK", + "type": "function", + } + ], + }, + { + "role": "tool", + "content": '{"success":true}', + "tool_call_id": "toolu_012ZTYKWD4VqrXGXyE7kEnAK", + }, + ], + max_tokens=2000, + temperature=1, + tools=[ + { + "type": "function", + "function": { + "name": "submitFruit", + "description": "Submits a fruit", + "parameters": { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "The name of the fruit", + } + }, + "required": ["name"], + }, + }, + } + ], + frequency_penalty=0.8, + ) + + pass + except litellm.APIError as e: + assert e.status_code == 500 + except Exception as e: + pytest.fail(f"An unexpected error occurred - {str(e)}") + + def test_completion_cohere_command_r_plus_function_call(): litellm.set_verbose = True tools = [ diff --git a/litellm/utils.py b/litellm/utils.py index 0565362e54..baeace6874 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -8154,27 +8154,9 @@ def exception_type( response=original_exception.response, ) elif original_exception.status_code == 500: - exception_mapping_worked = True - raise ServiceUnavailableError( - message=f"AnthropicException - {original_exception.message}", - llm_provider="anthropic", - model=model, - response=( - original_exception.response - if hasattr(original_exception, "response") - else httpx.Response( - status_code=500, - request=httpx.Request( - method="POST", - url="https://docs.anthropic.com/claude/reference/messages_post", - ), - ) - ), - ) - else: exception_mapping_worked = True raise APIError( - status_code=original_exception.status_code, + status_code=500, message=f"AnthropicException - {original_exception.message}. Handle with `litellm.APIError`.", llm_provider="anthropic", model=model,