fix(utils.py): Fix anthropic tool calling exception mapping

Fixes https://github.com/BerriAI/litellm/issues/4348
This commit is contained in:
Krrish Dholakia 2024-06-21 21:20:49 -07:00
parent 44bd9548d9
commit 5e893ed13e
2 changed files with 19 additions and 8 deletions

View file

@ -724,11 +724,15 @@ def test_anthropic_tool_calling_exception():
"function": { "function": {
"name": "get_current_weather", "name": "get_current_weather",
"description": "Get the current weather in a given location", "description": "Get the current weather in a given location",
"parameters": {},
}, },
} }
] ]
litellm.completion( try:
model="claude-3.5", litellm.completion(
messages=[{"role": "user", "content": "Hey, how's it going?"}], model="claude-3-5-sonnet-20240620",
tools=tools, messages=[{"role": "user", "content": "Hey, how's it going?"}],
) tools=tools,
)
except litellm.BadRequestError:
pass

View file

@ -5922,21 +5922,28 @@ def exception_type(
if "prompt is too long" in error_str or "prompt: length" in error_str: if "prompt is too long" in error_str or "prompt: length" in error_str:
exception_mapping_worked = True exception_mapping_worked = True
raise ContextWindowExceededError( raise ContextWindowExceededError(
message=error_str, message="AnthropicError - {}".format(error_str),
model=model, model=model,
llm_provider="anthropic", llm_provider="anthropic",
) )
if "Invalid API Key" in error_str: if "Invalid API Key" in error_str:
exception_mapping_worked = True exception_mapping_worked = True
raise AuthenticationError( raise AuthenticationError(
message=error_str, message="AnthropicError - {}".format(error_str),
model=model, model=model,
llm_provider="anthropic", llm_provider="anthropic",
) )
if "content filtering policy" in error_str: if "content filtering policy" in error_str:
exception_mapping_worked = True exception_mapping_worked = True
raise ContentPolicyViolationError( raise ContentPolicyViolationError(
message=error_str, message="AnthropicError - {}".format(error_str),
model=model,
llm_provider="anthropic",
)
if "Client error '400 Bad Request'" in error_str:
exception_mapping_worked = True
raise BadRequestError(
message="AnthropicError - {}".format(error_str),
model=model, model=model,
llm_provider="anthropic", llm_provider="anthropic",
) )