fix(completion): keep the alert and failure-count exclusions to the role error

Raise MissingMessageRoleError, a MalformedRequestFieldError subclass, so a malformed tools 400 alerts and counts as on main, and fill assistant whenever a role-less turn has a tool_calls or function_call key
This commit is contained in:
mateo-berri 2026-10-29 22:17:16 -07:00
parent c39f2a383f
commit 64097cfea8
8 changed files with 46 additions and 10 deletions

View file

@ -1467,6 +1467,7 @@ from .exceptions import (
BadRequestError,
ImageFetchError,
MalformedRequestFieldError as MalformedRequestFieldError,
MissingMessageRoleError as MissingMessageRoleError,
VectorStoreSearchError,
NotFoundError,
PermissionDeniedError,

View file

@ -319,6 +319,10 @@ class MalformedRequestFieldError(BadRequestError):
pass
class MissingMessageRoleError(MalformedRequestFieldError):
pass
class UnsupportedEndpointError(BadRequestError):
pass

View file

@ -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

View file

@ -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))
)

View file

@ -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}),

View file

@ -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

View file

@ -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():

View file

@ -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]):