fix(exceptions): map OpenAI-family invalid_request_error 404s to NotFoundError

Move the contributor's test to the mapped unit test file, assert the raised
class, status code and message, and cover the 400 and status-less cases that
must still raise BadRequestError

Add a regression test for the flow the bug was found on. Retrieving a missing
OpenAI vector store, sync or async, now raises NotFoundError with status 404

The router test that pinned an OpenAI 404 typed invalid_request_error as
BadRequestError now expects NotFoundError
This commit is contained in:
mateo-berri 2026-10-28 21:52:11 -07:00
parent c051f52538
commit 7de25afa2e
3 changed files with 88 additions and 2 deletions

View file

@ -1015,6 +1015,49 @@ def test_azure_404_with_invalid_request_error_type_maps_to_not_found():
assert "Response with id 'resp_abc' not found." in excinfo.value.message
@pytest.mark.parametrize("provider", ["openai", "groq", "deepseek"])
@pytest.mark.parametrize(
"status_code, message, expected_exception",
[
(400, "Invalid 'temperature'", litellm.BadRequestError),
(401, "Invalid API key", litellm.AuthenticationError),
(404, "The model does not exist", litellm.NotFoundError),
],
)
def test_openai_invalid_request_error_respects_status_code(
provider: str, status_code: int, message: str, expected_exception: type[openai.APIStatusError]
) -> None:
error_message: Final = json.dumps({"error": {"message": message, "type": "invalid_request_error"}})
original_exception: Final = OpenAIError(status_code=status_code, message=error_message, headers={})
with pytest.raises(openai.APIError) as excinfo:
exception_type(
model="some-model",
original_exception=original_exception,
custom_llm_provider=provider,
)
assert type(excinfo.value) is expected_exception
assert excinfo.value.status_code == status_code
assert message in excinfo.value.message
@pytest.mark.parametrize("provider", ["openai", "groq", "deepseek"])
def test_openai_invalid_request_error_without_a_status_code_maps_to_bad_request(provider: str) -> None:
message: Final = "Missing required parameter: 'input'."
original_exception: Final = Exception(json.dumps({"error": {"message": message, "type": "invalid_request_error"}}))
with pytest.raises(openai.APIError) as excinfo:
exception_type(
model="some-model",
original_exception=original_exception,
custom_llm_provider=provider,
)
assert type(excinfo.value) is litellm.BadRequestError
assert message in excinfo.value.message
class _UpstreamHTTPError(Exception):
def __init__(self, status_code: int) -> None:
super().__init__("upstream failure")

View file

@ -21477,7 +21477,7 @@ async def test_router_retry_policy_controls_upstream_attempt_count(
"error_body,error_type",
[
({"message": "model is down", "type": "server_error"}, litellm.NotFoundError),
({"message": "Response with id 'resp_x' not found.", "type": "invalid_request_error"}, litellm.BadRequestError),
({"message": "Response with id 'resp_x' not found.", "type": "invalid_request_error"}, litellm.NotFoundError),
],
)
@pytest.mark.parametrize(

View file

@ -15,12 +15,13 @@ from unittest.mock import MagicMock, patch
import httpx
import pytest
import litellm
import litellm.vector_stores.main as vector_stores_main
from litellm.llms.base_llm.vector_store.transformation import (
RouterVectorStoreEmbeddingExecutor,
)
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
from litellm.vector_stores.main import acreate, aupdate, search
from litellm.vector_stores.main import acreate, aretrieve, aupdate, retrieve, search
MOCK_SEARCH_RESPONSE = {
"object": "vector_store.search_results.page",
@ -200,3 +201,45 @@ async def test_direct_sdk_vector_store_writes_send_the_callers_metadata_unchange
sent_metadata: Final = await _metadata_sent_to_provider(operation, callers_metadata)
assert sent_metadata == [callers_metadata]
def _vector_store_not_found(request: httpx.Request) -> httpx.Response:
return httpx.Response(
404,
json={
"error": {
"message": "No vector store found with id 'vs_missing'.",
"type": "invalid_request_error",
"param": "vector_store_id",
"code": None,
}
},
request=request,
)
async def _retrieve_missing_openai_vector_store(is_async: bool) -> object:
transport: Final = httpx.MockTransport(_vector_store_not_found)
if is_async:
return await aretrieve(
vector_store_id="vs_missing",
custom_llm_provider="openai",
api_key="sk-test",
client=AsyncHTTPHandler(transport=transport),
)
return retrieve(
vector_store_id="vs_missing",
custom_llm_provider="openai",
api_key="sk-test",
client=HTTPHandler(client=httpx.Client(transport=transport)),
)
@pytest.mark.asyncio
@pytest.mark.parametrize("is_async", [True, False])
async def test_retrieving_a_missing_openai_vector_store_raises_not_found(is_async: bool):
with pytest.raises(litellm.NotFoundError) as caught:
await _retrieve_missing_openai_vector_store(is_async)
assert caught.value.status_code == 404
assert "No vector store found with id 'vs_missing'." in caught.value.message