Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bedrock anthropic prompt too long detection #6975

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions litellm/litellm_core_utils/exception_mapping_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ def exception_type( # type: ignore # noqa: PLR0915
"too many tokens" in error_str
or "expected maxLength:" in error_str
or "Input is too long" in error_str
or "prompt is too long" in error_str
or "prompt: length: 1.." in error_str
or "Too many input tokens" in error_str
):
Expand Down
4 changes: 4 additions & 0 deletions tests/proxy_unit_tests/test_configs/test_bad_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ model_list:
model: azure/azure-embedding-model
api_base: os.environ/AZURE_API_BASE
api_key: bad-key
- model_name: anthropic/claude-3-sonnet
litellm_params:
model: anthropic/claude-3-sonnet
api_key: bad-key

63 changes: 63 additions & 0 deletions tests/proxy_unit_tests/test_proxy_exception_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,66 @@ def test_chat_completion_exception_azure_context_window(mock_acompletion, client

except Exception as e:
pytest.fail(f"LiteLLM Proxy test failed. Exception {str(e)}")


# Test for Anthropic "prompt is too long" error pattern
anthropic_context_length_exceeded_error_response_dict = {
"error": {
"message": "litellm.ContextWindowExceededError: AnthropicError - {\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"prompt is too long: 209303 tokens > 200000 maximum\"}}",
"type": None,
"param": None,
"code": 400,
},
}
anthropic_context_length_exceeded_error_response = Response(
status_code=400,
content=json.dumps(anthropic_context_length_exceeded_error_response_dict),
)

@mock.patch(
"litellm.proxy.proxy_server.llm_router.acompletion",
return_value=anthropic_context_length_exceeded_error_response,
)
def test_chat_completion_exception_anthropic_context_window(mock_acompletion, client):
try:
# Your test data
test_data = {
"model": "anthropic/claude-3-sonnet",
"messages": [
{"role": "user", "content": "hi" * 100000},
],
"max_tokens": 10,
}
response = None

response = client.post("/chat/completions", json=test_data)
print("got response from server", response)

mock_acompletion.assert_called_once_with(
**test_data,
litellm_call_id=mock.ANY,
litellm_logging_obj=mock.ANY,
request_timeout=mock.ANY,
metadata=mock.ANY,
proxy_server_request=mock.ANY,
)

json_response = response.json()

print("keys in json response", json_response.keys())

assert json_response.keys() == {"error"}

assert json_response == anthropic_context_length_exceeded_error_response_dict

# make an openai client to call _make_status_error_from_response
openai_client = openai.OpenAI(api_key="anything")
openai_exception = openai_client._make_status_error_from_response(
response=response
)
print("exception from proxy", openai_exception)
assert isinstance(openai_exception, openai.BadRequestError)
print("passed exception is of type BadRequestError")

except Exception as e:
pytest.fail(f"LiteLLM Proxy test failed. Exception {str(e)}")