Skip to content

Conversation

@hula-la
Copy link
Contributor

@hula-la hula-la commented Nov 23, 2025

Title

Convert TypeError to BadRequestError for missing/invalid parameters

Relevant issues

Fixes #16993

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

Problem

When calling LiteLLM API functions with missing or invalid parameters, error handling is inconsistent:

  1. Core Library: TypeError raised instead of BadRequestError when parameters completely omitted
  2. Proxy Server: HTTP 500 returned instead of HTTP 400 when wrong parameter names used

Solution

Added TypeError to BadRequestError conversion at two layers:

1. Core Library (litellm/utils.py)

  • Added TypeError handling in @client decorator (both sync and async)
  • Extracts missing parameter name using regex: r"missing (\d+ )?required positional argument[s]?:? '?([^']+)'?"
  • Fixed KeyError when wrong parameter names used (kwargs.get instead of kwargs[])
  • Raises BadRequestError with HTTP 400 status code and clear error message

2. Proxy Layer (litellm/proxy/route_llm_request.py)

  • Added TypeError handling in route_request function
  • Wraps _route_request_impl in try-except block
  • Prevents TypeError from bubbling up as HTTP 500 error
  • Converts to BadRequestError with HTTP 400 status code

3. Test Coverage

Added comprehensive test suite:

  • Core library tests (tests/test_litellm/test_parameter_validation_error.py): 5 test cases

    • Missing required parameters (model, input)
    • Wrong parameter names (messages vs input)
    • HTTP 400 status code validation
  • Proxy tests (tests/proxy_unit_tests/test_proxy_parameter_validation.py): 6 test cases

    • End-to-end testing through proxy server
    • Missing model parameter
    • Wrong parameter names causing HTTP 500
    • Error response format validation

Test Results

Core Library Tests - AFTER Implementation:

tests/test_litellm/test_parameter_validation_error.py::test_bad_request_error_has_400_status_code PASSED
tests/test_litellm/test_parameter_validation_error.py::test_missing_required_parameter_aresponses PASSED
tests/test_litellm/test_parameter_validation_error.py::test_missing_required_parameter_async PASSED
tests/test_litellm/test_parameter_validation_error.py::test_missing_required_parameter_sync PASSED
tests/test_litellm/test_parameter_validation_error.py::test_responses_api_wrong_parameter PASSED

======================== 5 passed, 1 warning in 0.09s =========================

Proxy Tests - AFTER Implementation:

tests/proxy_unit_tests/test_proxy_parameter_validation.py::test_proxy_acompletion_missing_model PASSED
tests/proxy_unit_tests/test_proxy_parameter_validation.py::test_proxy_chat_completion_missing_messages PASSED
tests/proxy_unit_tests/test_proxy_parameter_validation.py::test_proxy_chat_completion_missing_model PASSED
tests/proxy_unit_tests/test_proxy_parameter_validation.py::test_proxy_error_response_format PASSED
tests/proxy_unit_tests/test_proxy_parameter_validation.py::test_proxy_responses_api_missing_model PASSED
tests/proxy_unit_tests/test_proxy_parameter_validation.py::test_proxy_responses_api_wrong_parameter PASSED

======================== 6 passed, 3 warnings in 5.48s =========================

Impact

  • ✅ API consumers can distinguish between client (4xx) and server (5xx) errors
  • ✅ Follows HTTP status code conventions
  • ✅ Clear error messages indicating missing parameters
  • ✅ Prevents confusing HTTP 500 errors for user mistakes
  • ✅ No breaking changes - only improves error handling

Files Changed

  • litellm/utils.py: Added TypeError handling in @client decorator
  • litellm/proxy/route_llm_request.py: Added TypeError handling in route_request
  • tests/test_litellm/test_parameter_validation_error.py: New test file (5 tests)
  • tests/proxy_unit_tests/test_proxy_parameter_validation.py: New test file (6 tests)

- Add TypeError to BadRequestError conversion in @client decorator
- Extract missing parameter name using regex pattern
- Fix KeyError when wrong parameter names are used
- Add TypeError handling in route_request for proxy layer
- Add comprehensive test coverage for parameter validation

Test Results:
- Core library: 5/5 tests pass (were failing with TypeError)
- Proxy: 6/6 tests pass (2 were failing with HTTP 500)
- All parameter validation errors now return HTTP 400
@vercel
Copy link

vercel bot commented Nov 23, 2025

@hula-la is attempting to deploy a commit to the CLERKIEAI Team on Vercel.

A member of the Team first needs to authorize it.

"""
try:
return await _route_request_impl(data, llm_router, user_model, route_type)
except TypeError as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is already fixed in utils.py why have it again here?

Copy link
Contributor Author

@hula-la hula-la Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krrishdholakia
Due to the code below, certain requests(ex. aresponses), wrap client method (ex. litellm.aresponses), so the @client logic is executed.

# router.py
        self.aresponses = self.factory_function(
            litellm.aresponses, call_type="aresponses"
        )

In contrast, requests like acompletion go directly to methods implemented as shown below, so they do not go through the validation performed by the @client wrapping.

# router.py
    async def acompletion(
        self,
        model: str,
        messages: List[AllMessageValues],
        stream: bool = False,
        **kwargs,
    ):
        try:
         ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TypeError instead of BadRequestError for missing/invalid parameters (HTTP 500 in proxy)

2 participants