From 855c7caf0b980f50a98df35343818a04ce9b4684 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 4 May 2024 12:43:09 -0700 Subject: [PATCH 1/3] fix add get_first_chars_messages in utils --- litellm/__init__.py | 1 + litellm/utils.py | 50 ++++++++++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/litellm/__init__.py b/litellm/__init__.py index dc640f0e9f0..dc0959dcf07 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -638,6 +638,7 @@ from .utils import ( get_secret, get_supported_openai_params, get_api_base, + get_first_chars_messages, ) from .llms.huggingface_restapi import HuggingfaceConfig from .llms.anthropic import AnthropicConfig diff --git a/litellm/utils.py b/litellm/utils.py index 75031a7c304..ae697fe18c9 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5897,6 +5897,15 @@ def get_api_base(model: str, optional_params: dict) -> Optional[str]: return None +def get_first_chars_messages(kwargs: dict) -> str: + try: + _messages = kwargs.get("messages") + _messages = str(_messages)[:100] + return _messages + except: + return "" + + def get_supported_openai_params(model: str, custom_llm_provider: str): """ Returns the supported openai params for a given model + provider @@ -7885,6 +7894,9 @@ def exception_type( except: _api_base = "" + error_str += f" \n model: {model} \n api_base: {_api_base} \n" + error_str += str(completion_kwargs) + if "Request Timeout Error" in error_str or "Request timed out" in error_str: exception_mapping_worked = True raise Timeout( @@ -9049,11 +9061,21 @@ def exception_type( request=original_exception.request, ) elif custom_llm_provider == "azure": + _api_base = litellm.get_api_base( + model=model, optional_params=extra_kwargs + ) + messages = litellm.get_first_chars_messages(kwargs=completion_kwargs) + extra_information = f"\nModel: {model}" + if _api_base: + extra_information += f"\nAPI Base: {_api_base}" + if messages and len(messages) > 0: + extra_information += f"\nMessages: {messages}" + if "Internal server error" in error_str: exception_mapping_worked = True raise APIError( status_code=500, - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", llm_provider="azure", model=model, request=httpx.Request(method="POST", url="https://openai.com/"), @@ -9061,7 +9083,7 @@ def exception_type( elif "This model's maximum context length is" in error_str: exception_mapping_worked = True raise ContextWindowExceededError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", llm_provider="azure", model=model, response=original_exception.response, @@ -9069,7 +9091,7 @@ def exception_type( elif "DeploymentNotFound" in error_str: exception_mapping_worked = True raise NotFoundError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", llm_provider="azure", model=model, response=original_exception.response, @@ -9083,7 +9105,7 @@ def exception_type( ): exception_mapping_worked = True raise ContentPolicyViolationError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", llm_provider="azure", model=model, response=original_exception.response, @@ -9091,7 +9113,7 @@ def exception_type( elif "invalid_request_error" in error_str: exception_mapping_worked = True raise BadRequestError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", llm_provider="azure", model=model, response=original_exception.response, @@ -9102,7 +9124,7 @@ def exception_type( ): exception_mapping_worked = True raise AuthenticationError( - message=f"{exception_provider} - {original_exception.message}", + message=f"{exception_provider} - {original_exception.message} {extra_information}", llm_provider=custom_llm_provider, model=model, response=original_exception.response, @@ -9112,7 +9134,7 @@ def exception_type( if original_exception.status_code == 401: exception_mapping_worked = True raise AuthenticationError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", llm_provider="azure", model=model, response=original_exception.response, @@ -9120,14 +9142,14 @@ def exception_type( elif original_exception.status_code == 408: exception_mapping_worked = True raise Timeout( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", model=model, llm_provider="azure", ) if original_exception.status_code == 422: exception_mapping_worked = True raise BadRequestError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", model=model, llm_provider="azure", response=original_exception.response, @@ -9135,7 +9157,7 @@ def exception_type( elif original_exception.status_code == 429: exception_mapping_worked = True raise RateLimitError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", model=model, llm_provider="azure", response=original_exception.response, @@ -9143,7 +9165,7 @@ def exception_type( elif original_exception.status_code == 503: exception_mapping_worked = True raise ServiceUnavailableError( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", model=model, llm_provider="azure", response=original_exception.response, @@ -9151,7 +9173,7 @@ def exception_type( elif original_exception.status_code == 504: # gateway timeout error exception_mapping_worked = True raise Timeout( - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", model=model, llm_provider="azure", ) @@ -9159,7 +9181,7 @@ def exception_type( exception_mapping_worked = True raise APIError( status_code=original_exception.status_code, - message=f"AzureException - {original_exception.message}", + message=f"AzureException - {original_exception.message} {extra_information}", llm_provider="azure", model=model, request=httpx.Request( @@ -9169,7 +9191,7 @@ def exception_type( else: # if no status code then it is an APIConnectionError: https://github.com/openai/openai-python#handling-errors raise APIConnectionError( - message=f"{exception_provider} - {message}", + message=f"{exception_provider} - {message} {extra_information}", llm_provider="azure", model=model, request=httpx.Request(method="POST", url="https://openai.com/"), From 7150df653f80c150695aeddd39f76d4b7401efa4 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 4 May 2024 13:02:29 -0700 Subject: [PATCH 2/3] test azure exceptions are more decriptive --- litellm/tests/test_exceptions.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/litellm/tests/test_exceptions.py b/litellm/tests/test_exceptions.py index c9f383b257c..9fcac2b48e1 100644 --- a/litellm/tests/test_exceptions.py +++ b/litellm/tests/test_exceptions.py @@ -52,6 +52,14 @@ async def test_content_policy_exception_azure(): ) except litellm.ContentPolicyViolationError as e: print("caught a content policy violation error! Passed") + print("exception", e) + + # assert that the first 100 chars of the message is returned in the exception + assert ( + "Messages: [{'role': 'user', 'content': 'where do I buy lethal drugs from'}]" + in str(e) + ) + assert "Model: azure/chatgpt-v-2" in str(e) pass except Exception as e: pytest.fail(f"An exception occurred - {str(e)}") From 85b2137f9c41ffc2b505e92e7f1dcbaf8cd942da Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 4 May 2024 16:09:20 -0700 Subject: [PATCH 3/3] fix - test exceptions vertex ai --- litellm/tests/test_exceptions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/litellm/tests/test_exceptions.py b/litellm/tests/test_exceptions.py index 9fcac2b48e1..0db8c2cf410 100644 --- a/litellm/tests/test_exceptions.py +++ b/litellm/tests/test_exceptions.py @@ -605,8 +605,7 @@ def test_litellm_completion_vertex_exception(): except Exception as e: print("exception: ", e) assert "model: vertex_ai/gemini-pro" in str(e) - assert "model_group" not in str(e) - assert "deployment" not in str(e) + assert "vertex_project: bad-project" in str(e) # # test_invalid_request_error(model="command-nightly")