From ca04244a0ab76291a819f0f9a475f5e0706d0808 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 28 Jun 2024 21:50:21 -0700 Subject: [PATCH] fix(utils.py): correctly raise openrouter error --- litellm/tests/test_utils.py | 54 +++++++++++++++++++++++++++++++++++++ litellm/utils.py | 21 +++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/litellm/tests/test_utils.py b/litellm/tests/test_utils.py index 09715e6c166..78b64270c6f 100644 --- a/litellm/tests/test_utils.py +++ b/litellm/tests/test_utils.py @@ -609,3 +609,57 @@ def test_logging_trace_id(langfuse_trace_id, langfuse_existing_trace_id): litellm_logging_obj._get_trace_id(service_name="langfuse") == litellm_call_id ) + + +def test_convert_model_response_object(): + """ + Unit test to ensure model response object correctly handles openrouter errors. + """ + args = { + "response_object": { + "id": None, + "choices": None, + "created": None, + "model": None, + "object": None, + "service_tier": None, + "system_fingerprint": None, + "usage": None, + "error": { + "message": '{"type":"error","error":{"type":"invalid_request_error","message":"Output blocked by content filtering policy"}}', + "code": 400, + }, + }, + "model_response_object": litellm.ModelResponse( + id="chatcmpl-b88ce43a-7bfc-437c-b8cc-e90d59372cfb", + choices=[ + litellm.Choices( + finish_reason="stop", + index=0, + message=litellm.Message(content="default", role="assistant"), + ) + ], + created=1719376241, + model="openrouter/anthropic/claude-3.5-sonnet", + object="chat.completion", + system_fingerprint=None, + usage=litellm.Usage(), + ), + "response_type": "completion", + "stream": False, + "start_time": None, + "end_time": None, + "hidden_params": None, + } + + try: + litellm.convert_to_model_response_object(**args) + pytest.fail("Expected this to fail") + except Exception as e: + assert hasattr(e, "status_code") + assert e.status_code == 400 + assert hasattr(e, "message") + assert ( + e.message + == '{"type":"error","error":{"type":"invalid_request_error","message":"Output blocked by content filtering policy"}}' + ) diff --git a/litellm/utils.py b/litellm/utils.py index 0eedd259c0e..70d07706325 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5273,6 +5273,27 @@ def convert_to_model_response_object( hidden_params: Optional[dict] = None, ): received_args = locals() + ### CHECK IF ERROR IN RESPONSE ### - openrouter returns these in the dictionary + if ( + response_object is not None + and "error" in response_object + and response_object["error"] is not None + ): + error_args = {"status_code": 422, "message": "Error in response object"} + if isinstance(response_object["error"], dict): + if "code" in response_object["error"]: + error_args["status_code"] = response_object["error"]["code"] + if "message" in response_object["error"]: + if isinstance(response_object["error"]["message"], dict): + message_str = json.dumps(response_object["error"]["message"]) + else: + message_str = str(response_object["error"]["message"]) + error_args["message"] = message_str + raised_exception = Exception() + setattr(raised_exception, "status_code", error_args["status_code"]) + setattr(raised_exception, "message", error_args["message"]) + raise raised_exception + try: if response_type == "completion" and ( model_response_object is None