diff --git a/litellm/__init__.py b/litellm/__init__.py index 3e8cddac12c..4987387bb48 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -1467,6 +1467,7 @@ from .exceptions import ( BadRequestError, ImageFetchError, MalformedRequestFieldError as MalformedRequestFieldError, + MissingMessageRoleError as MissingMessageRoleError, VectorStoreSearchError, NotFoundError, PermissionDeniedError, diff --git a/litellm/exceptions.py b/litellm/exceptions.py index 28211b3dab2..9174ba22ed2 100644 --- a/litellm/exceptions.py +++ b/litellm/exceptions.py @@ -319,6 +319,10 @@ class MalformedRequestFieldError(BadRequestError): pass +class MissingMessageRoleError(MalformedRequestFieldError): + pass + + class UnsupportedEndpointError(BadRequestError): pass diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 75a261ee0d4..ee53605e117 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -140,7 +140,7 @@ from litellm.caching.dual_cache import LimitedSizeOrderedDict from litellm.caching.in_memory_cache import InMemoryCache from litellm.exceptions import ( GuardrailRaisedException, - MalformedRequestFieldError, + MissingMessageRoleError, RejectedRequestError, SensitiveDataRouteException, ) @@ -625,7 +625,7 @@ class _StreamIteratorHook(Protocol[_T]): def _is_client_error_exception(exc: Exception) -> bool: - if isinstance(exc, MalformedRequestFieldError): + if isinstance(exc, MissingMessageRoleError): return True if isinstance(exc, HTTPException): return exc.status_code < 500 diff --git a/litellm/router_utils/cooldown_handlers.py b/litellm/router_utils/cooldown_handlers.py index 845475d9f25..6b0180a203c 100644 --- a/litellm/router_utils/cooldown_handlers.py +++ b/litellm/router_utils/cooldown_handlers.py @@ -26,7 +26,7 @@ from litellm.constants import ( INTERNAL_CALL_ORIGIN_METADATA_KEY, SINGLE_DEPLOYMENT_TRAFFIC_FAILURE_THRESHOLD, ) -from litellm.exceptions import MidStreamFallbackError +from litellm.exceptions import MidStreamFallbackError, MissingMessageRoleError from litellm.litellm_core_utils.credential_accessor import CredentialAccessor from litellm.litellm_core_utils.litellm_logging import mask_api_base_credentials from litellm.router_utils.cooldown_cache import ROUTER_COOLDOWNS_TARGET, CooldownCacheValue @@ -87,7 +87,7 @@ def mark_deployment_selection_failure(exception: BaseException) -> None: def is_failure_outside_selected_deployment(exception: BaseException | None) -> bool: return ( is_advisor_orchestration_failure(exception) - or isinstance(exception, litellm.MalformedRequestFieldError) + or isinstance(exception, MissingMessageRoleError) or bool(getattr(exception, _DEPLOYMENT_SELECTION_FAILURE_ATTR, False)) ) diff --git a/litellm/utils.py b/litellm/utils.py index f741168f58a..04728764c1e 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -506,6 +506,7 @@ from .exceptions import ( ContentPolicyViolationError, ContextWindowExceededError, MalformedRequestFieldError, + MissingMessageRoleError, ModelNotMappedError, NotFoundError, OpenAIError, @@ -9248,9 +9249,7 @@ def validate_and_fix_openai_messages( ), ) cleaned_message = cleanup_none_field_in_message(message=convert_msg_to_dict) - if not cleaned_message.get("role") and ( - cleaned_message.get("tool_calls") or cleaned_message.get("function_call") - ): + if not cleaned_message.get("role") and ("tool_calls" in cleaned_message or "function_call" in cleaned_message): cleaned_message["role"] = "assistant" new_messages.append(cleaned_message) return validate_chat_completion_user_messages(messages=new_messages, model=model, request_kwargs=request_kwargs) @@ -9322,12 +9321,12 @@ def validate_chat_completion_user_messages( List[dict]: The validated messages Raises: - MalformedRequestFieldError: If a message has no role + MissingMessageRoleError: If a message has no role ValueError: If any message is invalid """ for idx, m in enumerate(messages): if "role" not in m or not m["role"]: - raise MalformedRequestFieldError( + raise MissingMessageRoleError( message=f"Missing required parameter: 'messages[{idx}].role'.", model=model, llm_provider=_deployment_provider({**(request_kwargs or {}), "model": model}), diff --git a/tests/unit/proxy/test_proxy_utils_model_creation_and_error_logging.py b/tests/unit/proxy/test_proxy_utils_model_creation_and_error_logging.py index 6c6f6140543..cc24ee6fef8 100644 --- a/tests/unit/proxy/test_proxy_utils_model_creation_and_error_logging.py +++ b/tests/unit/proxy/test_proxy_utils_model_creation_and_error_logging.py @@ -1812,6 +1812,13 @@ class TestPostCallFailureHookLLMExceptionAlerting: litellm.completion(model="openai/gpt-5.6", messages=[{"content": "hi"}], mock_response="ok") assert (await self._alerted(exc_info.value)).called is False + @pytest.mark.asyncio + async def test_malformed_tools_field_still_alerts(self): + exc = litellm.MalformedRequestFieldError( + message="tools: Input should be a valid list", model="openai/gpt-5.6", llm_provider="openai" + ) + assert (await self._alerted(exc)).called is True + @pytest.mark.asyncio async def test_genuine_llm_api_error_still_alerts(self): assert (await self._alerted(Exception("upstream 503"))).called is True diff --git a/tests/unit/router_utils/test_cooldown_handlers.py b/tests/unit/router_utils/test_cooldown_handlers.py index cb6d192dcd8..3b808753dc9 100644 --- a/tests/unit/router_utils/test_cooldown_handlers.py +++ b/tests/unit/router_utils/test_cooldown_handlers.py @@ -36,6 +36,7 @@ from litellm.router_utils.cooldown_handlers import ( async_get_cooldown_deployments, cast_exception_status_to_int, get_cooldown_deployments, + is_failure_outside_selected_deployment, mark_advisor_orchestration_failure, set_cooldown_deployments, should_cooldown_based_on_allowed_fails_policy, @@ -2157,6 +2158,29 @@ async def test_role_less_requests_do_not_count_toward_the_deployments_failure_ra assert _should_cooldown_deployment(router, "d1", 500, Exception("Test")) is False +@pytest.mark.parametrize( + ("exception", "outside_deployment"), + [ + ( + litellm.MissingMessageRoleError( + message="Missing required parameter: 'messages[0].role'.", model="m", llm_provider="openai" + ), + True, + ), + ( + litellm.MalformedRequestFieldError( + message="tools: Input should be a valid list", model="m", llm_provider="openai" + ), + False, + ), + ], +) +def test_only_a_role_less_request_is_a_failure_outside_the_deployment( + exception: Exception, outside_deployment: bool +) -> None: + assert is_failure_outside_selected_deployment(exception) is outside_deployment + + @pytest.mark.usefixtures("_vcr_outcome_gate", "setup_and_teardown") @pytest.mark.asyncio async def test_should_cooldown_deployment_allowed_fails_set_on_router(): diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0dc1b9ff259..f0bc04463eb 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -8652,7 +8652,7 @@ def test_validate_tool_choice_without_model_is_still_a_400(): def test_completion_message_without_role_is_a_400(messages: list[dict[str, str | None]], index: int): """A message with no role is the caller's mistake, rejected before any provider sees it, never sent as assistant.""" with pytest.raises( - litellm.MalformedRequestFieldError, match=re.escape(f"Missing required parameter: 'messages[{index}].role'.") + litellm.MissingMessageRoleError, match=re.escape(f"Missing required parameter: 'messages[{index}].role'.") ) as exc_info: litellm.completion(model=MODEL, messages=messages, mock_response="Hello there!") assert exc_info.value.status_code == 400 @@ -8673,6 +8673,7 @@ def test_completion_message_without_role_is_a_400(messages: list[dict[str, str | ], }, {"content": None, "function_call": {"name": "get_weather", "arguments": "{}"}}, + {"content": "ok", "tool_calls": []}, ], ) def test_completion_role_less_tool_call_turn_is_sent_as_assistant(call_turn: dict[str, object]):