mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-05 07:37:50 +00:00
Merge f7ea88f3da into de42b67252
This commit is contained in:
commit
b711946a4d
3 changed files with 41 additions and 1 deletions
|
|
@ -103,6 +103,8 @@ class ExceptionCheckers:
|
|||
"exceeds the available context size", # llama.cpp/Lemonade
|
||||
"exceeds the maximum number of tokens allowed", # Gemini
|
||||
'"code": "context_length_exceeded"',
|
||||
"exceeds the context window of this model", # OpenAI
|
||||
"exceeded model token limit", # Moonshot
|
||||
]
|
||||
for substring in known_exception_substrings:
|
||||
if substring in _error_str_lowercase:
|
||||
|
|
|
|||
|
|
@ -242,6 +242,13 @@ context_window_test_cases = [
|
|||
"Input should be 'error' or 'truncate', field: 'context_length_exceeded_behavior', value: 'foo'",
|
||||
False,
|
||||
),
|
||||
# OpenAI's current wording (code context_length_exceeded)
|
||||
(
|
||||
"Your input exceeds the context window of this model. Please adjust your input and try again.",
|
||||
True,
|
||||
),
|
||||
# Moonshot
|
||||
("Invalid request: Your request exceeded model token limit: 262144", True),
|
||||
# Negative cases (should return False)
|
||||
("A generic API error occurred.", False),
|
||||
("Invalid API Key provided.", False),
|
||||
|
|
@ -439,6 +446,37 @@ def test_gemini_context_window_error_mapping(error_message, should_raise_context
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"custom_llm_provider, model, error_message",
|
||||
[
|
||||
(
|
||||
"openai",
|
||||
"gpt-5",
|
||||
"Error code: 400 - {'error': {'message': 'Your input exceeds the context window of this model. "
|
||||
"Please adjust your input and try again.', 'type': 'invalid_request_error', 'param': 'input', "
|
||||
"'code': 'context_length_exceeded'}}",
|
||||
),
|
||||
(
|
||||
"moonshot",
|
||||
"kimi-k2",
|
||||
"Error code: 400 - {'error': {'message': 'Invalid request: Your request exceeded model token limit: "
|
||||
"262144', 'type': 'invalid_request_error', 'param': None, 'code': None}}",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_openai_compatible_context_window_error_mapping(custom_llm_provider, model, error_message):
|
||||
"""These 400s must map to ContextWindowExceededError so context_window_fallbacks fire."""
|
||||
with pytest.raises(litellm.ContextWindowExceededError) as excinfo:
|
||||
exception_type(
|
||||
model=model,
|
||||
original_exception=OpenAIError(status_code=400, message=error_message),
|
||||
custom_llm_provider=custom_llm_provider,
|
||||
)
|
||||
|
||||
assert excinfo.value.status_code == 400
|
||||
assert excinfo.value.llm_provider == custom_llm_provider
|
||||
|
||||
|
||||
def test_lemonade_context_window_error_mapping():
|
||||
"""Lemonade's llama.cpp backend should map context overflows to LiteLLM's standard error."""
|
||||
|
||||
|
|
|
|||
|
|
@ -596,7 +596,7 @@ async def test_responses_stream_error_event_raises_bad_request() -> None:
|
|||
stream: Final = await litellm.aresponses(
|
||||
model="openai/gpt-5-mini", api_key="sk-test", input="oversized prompt", stream=True
|
||||
)
|
||||
with pytest.raises(litellm.BadRequestError) as exc_info:
|
||||
with pytest.raises(litellm.ContextWindowExceededError) as exc_info:
|
||||
_ = [event async for event in stream]
|
||||
requests: Final = tuple(route.calls)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue