mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
feat(azure.py): add azure bad request error support
This commit is contained in:
parent
f688fc8138
commit
bfbe26b91d
5 changed files with 49 additions and 34 deletions
|
@ -422,6 +422,7 @@ def exception_type( # type: ignore # noqa: PLR0915
|
||||||
llm_provider=custom_llm_provider,
|
llm_provider=custom_llm_provider,
|
||||||
response=getattr(original_exception, "response", None),
|
response=getattr(original_exception, "response", None),
|
||||||
litellm_debug_info=extra_information,
|
litellm_debug_info=extra_information,
|
||||||
|
body=getattr(original_exception, "body", None),
|
||||||
)
|
)
|
||||||
elif original_exception.status_code == 429:
|
elif original_exception.status_code == 429:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
|
@ -1961,6 +1962,7 @@ def exception_type( # type: ignore # noqa: PLR0915
|
||||||
model=model,
|
model=model,
|
||||||
litellm_debug_info=extra_information,
|
litellm_debug_info=extra_information,
|
||||||
response=getattr(original_exception, "response", None),
|
response=getattr(original_exception, "response", None),
|
||||||
|
body=getattr(original_exception, "body", None),
|
||||||
)
|
)
|
||||||
elif (
|
elif (
|
||||||
"The api_key client option must be set either by passing api_key to the client or by setting"
|
"The api_key client option must be set either by passing api_key to the client or by setting"
|
||||||
|
@ -1992,6 +1994,7 @@ def exception_type( # type: ignore # noqa: PLR0915
|
||||||
model=model,
|
model=model,
|
||||||
litellm_debug_info=extra_information,
|
litellm_debug_info=extra_information,
|
||||||
response=getattr(original_exception, "response", None),
|
response=getattr(original_exception, "response", None),
|
||||||
|
body=getattr(original_exception, "body", None),
|
||||||
)
|
)
|
||||||
elif original_exception.status_code == 401:
|
elif original_exception.status_code == 401:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
|
|
|
@ -540,10 +540,14 @@ class AzureChatCompletion(BaseLLM):
|
||||||
status_code = getattr(e, "status_code", 500)
|
status_code = getattr(e, "status_code", 500)
|
||||||
error_headers = getattr(e, "headers", None)
|
error_headers = getattr(e, "headers", None)
|
||||||
error_response = getattr(e, "response", None)
|
error_response = getattr(e, "response", None)
|
||||||
|
error_body = getattr(e, "body", None)
|
||||||
if error_headers is None and error_response:
|
if error_headers is None and error_response:
|
||||||
error_headers = getattr(error_response, "headers", None)
|
error_headers = getattr(error_response, "headers", None)
|
||||||
raise AzureOpenAIError(
|
raise AzureOpenAIError(
|
||||||
status_code=status_code, message=str(e), headers=error_headers
|
status_code=status_code,
|
||||||
|
message=str(e),
|
||||||
|
headers=error_headers,
|
||||||
|
body=error_body,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def acompletion(
|
async def acompletion(
|
||||||
|
@ -649,6 +653,7 @@ class AzureChatCompletion(BaseLLM):
|
||||||
raise AzureOpenAIError(status_code=500, message=str(e))
|
raise AzureOpenAIError(status_code=500, message=str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
message = getattr(e, "message", str(e))
|
message = getattr(e, "message", str(e))
|
||||||
|
body = getattr(e, "body", None)
|
||||||
## LOGGING
|
## LOGGING
|
||||||
logging_obj.post_call(
|
logging_obj.post_call(
|
||||||
input=data["messages"],
|
input=data["messages"],
|
||||||
|
@ -659,7 +664,7 @@ class AzureChatCompletion(BaseLLM):
|
||||||
if hasattr(e, "status_code"):
|
if hasattr(e, "status_code"):
|
||||||
raise e
|
raise e
|
||||||
else:
|
else:
|
||||||
raise AzureOpenAIError(status_code=500, message=message)
|
raise AzureOpenAIError(status_code=500, message=message, body=body)
|
||||||
|
|
||||||
def streaming(
|
def streaming(
|
||||||
self,
|
self,
|
||||||
|
@ -805,10 +810,14 @@ class AzureChatCompletion(BaseLLM):
|
||||||
error_headers = getattr(e, "headers", None)
|
error_headers = getattr(e, "headers", None)
|
||||||
error_response = getattr(e, "response", None)
|
error_response = getattr(e, "response", None)
|
||||||
message = getattr(e, "message", str(e))
|
message = getattr(e, "message", str(e))
|
||||||
|
error_body = getattr(e, "body", None)
|
||||||
if error_headers is None and error_response:
|
if error_headers is None and error_response:
|
||||||
error_headers = getattr(error_response, "headers", None)
|
error_headers = getattr(error_response, "headers", None)
|
||||||
raise AzureOpenAIError(
|
raise AzureOpenAIError(
|
||||||
status_code=status_code, message=message, headers=error_headers
|
status_code=status_code,
|
||||||
|
message=message,
|
||||||
|
headers=error_headers,
|
||||||
|
body=error_body,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def aembedding(
|
async def aembedding(
|
||||||
|
|
|
@ -17,6 +17,7 @@ class AzureOpenAIError(BaseLLMException):
|
||||||
request: Optional[httpx.Request] = None,
|
request: Optional[httpx.Request] = None,
|
||||||
response: Optional[httpx.Response] = None,
|
response: Optional[httpx.Response] = None,
|
||||||
headers: Optional[Union[httpx.Headers, dict]] = None,
|
headers: Optional[Union[httpx.Headers, dict]] = None,
|
||||||
|
body: Optional[dict] = None,
|
||||||
):
|
):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
status_code=status_code,
|
status_code=status_code,
|
||||||
|
@ -24,6 +25,7 @@ class AzureOpenAIError(BaseLLMException):
|
||||||
request=request,
|
request=request,
|
||||||
response=response,
|
response=response,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
|
body=body,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -391,34 +391,3 @@ def test_openai_chat_completion_streaming_handler_reasoning_content():
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.choices[0].delta.reasoning_content == "."
|
assert response.choices[0].delta.reasoning_content == "."
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
||||||
@pytest.mark.parametrize("stream_mode", [True, False])
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_exception_bubbling_up(sync_mode, stream_mode):
|
|
||||||
"""
|
|
||||||
make sure code, param, and type are bubbled up
|
|
||||||
"""
|
|
||||||
import litellm
|
|
||||||
|
|
||||||
litellm.set_verbose = True
|
|
||||||
with pytest.raises(Exception) as exc_info:
|
|
||||||
if sync_mode:
|
|
||||||
litellm.completion(
|
|
||||||
model="gpt-4o-mini",
|
|
||||||
messages=[{"role": "usera", "content": "hi"}],
|
|
||||||
stream=stream_mode,
|
|
||||||
sync_stream=sync_mode,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
await litellm.acompletion(
|
|
||||||
model="gpt-4o-mini",
|
|
||||||
messages=[{"role": "usera", "content": "hi"}],
|
|
||||||
stream=stream_mode,
|
|
||||||
sync_stream=sync_mode,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert exc_info.value.code == "invalid_value"
|
|
||||||
assert exc_info.value.param is not None
|
|
||||||
assert exc_info.value.type == "invalid_request_error"
|
|
||||||
|
|
|
@ -1205,3 +1205,35 @@ def test_context_window_exceeded_error_from_litellm_proxy():
|
||||||
}
|
}
|
||||||
with pytest.raises(litellm.ContextWindowExceededError):
|
with pytest.raises(litellm.ContextWindowExceededError):
|
||||||
extract_and_raise_litellm_exception(**args)
|
extract_and_raise_litellm_exception(**args)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
||||||
|
@pytest.mark.parametrize("stream_mode", [True, False])
|
||||||
|
@pytest.mark.parametrize("model", ["azure/gpt-4o"]) # "gpt-4o-mini",
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_exception_bubbling_up(sync_mode, stream_mode, model):
|
||||||
|
"""
|
||||||
|
make sure code, param, and type are bubbled up
|
||||||
|
"""
|
||||||
|
import litellm
|
||||||
|
|
||||||
|
litellm.set_verbose = True
|
||||||
|
with pytest.raises(Exception) as exc_info:
|
||||||
|
if sync_mode:
|
||||||
|
litellm.completion(
|
||||||
|
model=model,
|
||||||
|
messages=[{"role": "usera", "content": "hi"}],
|
||||||
|
stream=stream_mode,
|
||||||
|
sync_stream=sync_mode,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await litellm.acompletion(
|
||||||
|
model=model,
|
||||||
|
messages=[{"role": "usera", "content": "hi"}],
|
||||||
|
stream=stream_mode,
|
||||||
|
sync_stream=sync_mode,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert exc_info.value.code == "invalid_value"
|
||||||
|
assert exc_info.value.param is not None
|
||||||
|
assert exc_info.value.type == "invalid_request_error"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue