The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(ocr): return 400 for an unknown x-req-format header value
mateobfc52b94db91
diff --git a/litellm/proxy/ocr_endpoints/endpoints.py b/litellm/proxy/ocr_endpoints/endpoints.pyindex 5c12bc90a4f..00890662be5 100644--- a/litellm/proxy/ocr_endpoints/endpoints.py+++ b/litellm/proxy/ocr_endpoints/endpoints.py@@ -5,7 +5,7 @@ from collections.abc import Mappingfrom typing import Any, Final, castimport orjson-from fastapi import APIRouter, Depends, Request, Response, UploadFile+from fastapi import APIRouter, Depends, HTTPException, Request, Response, UploadFilefrom fastapi.responses import ORJSONResponsefrom litellm._logging import verbose_proxy_logger@@ -57,7 +57,11 @@ def _with_request_format(data: Mapping[str, Any], request: Request) -> Mapping[sheader_value: Final = request.headers.get(OCR_REQUEST_FORMAT_HEADER)if header_value is None or OCR_REQUEST_FORMAT_PARAM in data:return data- return {**data, OCR_REQUEST_FORMAT_PARAM: parse_ocr_request_format(header_value.strip().lower())}+ try:+ request_format: Final = parse_ocr_request_format(header_value.strip().lower())+ except ValueError as e:+ raise HTTPException(status_code=400, detail={"error": f"{e}"})+ return {**data, OCR_REQUEST_FORMAT_PARAM: request_format}def _native_response(response: object, fastapi_response: Response) -> Response | None:diff --git a/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.py b/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.pyindex b34c20f30b7..491011170e5 100644--- a/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.py+++ b/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.py@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, MagicMockimport orjsonimport pytest+from fastapi import HTTPExceptionfrom litellm.llms.base_llm.ocr.transformation import OCRPage, OCRResponsefrom litellm.proxy.ocr_endpoints.endpoints import _native_response, _parse_ocr_request@@ -79,9 +80,12 @@ async def test_should_reject_unknown_req_format_header():{"x-req-format": "azure"},)- with pytest.raises(ValueError, match="Invalid `req_format`"):+ with pytest.raises(HTTPException) as exc_info:await _parse_ocr_request(request)+ assert exc_info.value.status_code == 400+ assert "Invalid `req_format`" in f"{exc_info.value.detail}"+def test_should_return_native_payload_with_litellm_response_headers():fastapi_response = MagicMock()