Skip to content

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]

fix(ocr): validate body req_format in the proxy endpoint and run its tests in CI

mateo1e1a2b63a436
diff --git a/.github/workflows/test-unit-proxy-endpoints.yml b/.github/workflows/test-unit-proxy-endpoints.yml
index 64b92f7d847..3d1d0fcd6c3 100644
--- a/.github/workflows/test-unit-proxy-endpoints.yml
+++ b/.github/workflows/test-unit-proxy-endpoints.yml
@@ -43,6 +43,7 @@ jobs:
tests/test_litellm/proxy/video_endpoints
tests/test_litellm/proxy/response_api_endpoints
tests/test_litellm/proxy/image_endpoints
+ tests/test_litellm/proxy/ocr_endpoints
tests/test_litellm/proxy/vector_store_endpoints
tests/test_litellm/proxy/agent_endpoints
tests/test_litellm/proxy/a2a
diff --git a/litellm/proxy/ocr_endpoints/endpoints.py b/litellm/proxy/ocr_endpoints/endpoints.py
index 00890662be5..173fe6851a2 100644
--- a/litellm/proxy/ocr_endpoints/endpoints.py
+++ b/litellm/proxy/ocr_endpoints/endpoints.py
@@ -50,15 +50,19 @@ def _build_document_from_upload(
def _with_request_format(data: Mapping[str, Any], request: Request) -> Mapping[str, Any]:
"""
- Resolve the requested response format from the `x-req-format` header.
+ Resolve the requested response format from the body or the `x-req-format` header.
An explicit `req_format` in the body wins over the header.
"""
+ body_value: Final = data.get(OCR_REQUEST_FORMAT_PARAM)
header_value: Final = request.headers.get(OCR_REQUEST_FORMAT_HEADER)
- if header_value is None or OCR_REQUEST_FORMAT_PARAM in data:
+ raw_value: Final = body_value if body_value is not None else header_value
+ if raw_value is None:
return data
try:
- request_format: Final = parse_ocr_request_format(header_value.strip().lower())
+ request_format: Final = parse_ocr_request_format(
+ raw_value.strip().lower() if isinstance(raw_value, str) else raw_value
+ )
except ValueError as e:
raise HTTPException(status_code=400, detail={"error": f"{e}"})
return {**data, OCR_REQUEST_FORMAT_PARAM: request_format}
diff --git a/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.py b/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.py
index 491011170e5..153e8c36eda 100644
--- a/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.py
+++ b/tests/test_litellm/proxy/ocr_endpoints/test_endpoints.py
@@ -74,10 +74,19 @@ async def test_should_omit_req_format_when_header_absent():
@pytest.mark.asyncio
-async def test_should_reject_unknown_req_format_header():
+ "body_format, headers",
+ [
+ (None, {"x-req-format": "azure"}),
+ ("azure", {}),
+ ("azure", {"x-req-format": "native"}),
+ ],
+)
+async def test_should_reject_unknown_req_format(body_format, headers):
+ body = {"model": "azure-prebuilt-layout", "document": {"type": "document_url", "document_url": "https://x/y.pdf"}}
request = _json_request(
- {"model": "azure-prebuilt-layout", "document": {"type": "document_url", "document_url": "https://x/y.pdf"}},
- {"x-req-format": "azure"},
+ body if body_format is None else {**body, "req_format": body_format},
+ headers,
)
with pytest.raises(HTTPException) as exc_info: