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
feat(mistral): support Mistral OCR 4 (mistral-ocr-4-0) (#31353)
Mateo Wange0e920d80e47
diff --git a/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rs b/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rsindex 386457f8b84..1a33bc1e951 100644--- a/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rs+++ b/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rs@@ -15,6 +15,7 @@ const SUPPORTED_OCR_PARAMS: &[&str] = &["extract_footer","table_format","confidence_scores_granularity",+ "include_blocks","id",];@@ -193,6 +194,7 @@ mod tests {"extract_footer","table_format","confidence_scores_granularity",+ "include_blocks","id",]);diff --git a/litellm/llms/mistral/ocr/transformation.py b/litellm/llms/mistral/ocr/transformation.pyindex 3c0460cd51e..07a67815f6e 100644--- a/litellm/llms/mistral/ocr/transformation.py+++ b/litellm/llms/mistral/ocr/transformation.py@@ -44,6 +44,7 @@ class MistralOCRConfig(BaseOCRConfig):- extract_footer: Whether to extract document footer- table_format: Table output format ("markdown" or "html")- confidence_scores_granularity: Confidence score level ("word" or "page")+ - include_blocks: Whether to return paragraph-level bounding boxes and typed content blocks (OCR 4)- id: Request identifier"""return [@@ -58,6 +59,7 @@ class MistralOCRConfig(BaseOCRConfig):"extract_footer","table_format","confidence_scores_granularity",+ "include_blocks","id",]diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.jsonindex 296996d186e..1393c683860 100644--- a/litellm/model_prices_and_context_window_backup.json+++ b/litellm/model_prices_and_context_window_backup.json@@ -25563,8 +25563,18 @@},"mistral/mistral-ocr-latest": {"litellm_provider": "mistral",- "ocr_cost_per_page": 0.001,- "annotation_cost_per_page": 0.003,+ "ocr_cost_per_page": 0.004,+ "annotation_cost_per_page": 0.005,+ "mode": "ocr",+ "supported_endpoints": [+ "/v1/ocr"+ ],+ "source": "https://mistral.ai/pricing#api-pricing"+ },+ "mistral/mistral-ocr-4-0": {+ "litellm_provider": "mistral",+ "ocr_cost_per_page": 0.004,+ "annotation_cost_per_page": 0.005,"mode": "ocr","supported_endpoints": ["/v1/ocr"diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.jsonindex 64f23118275..9867874cb65 100644--- a/model_prices_and_context_window.json+++ b/model_prices_and_context_window.json@@ -25735,8 +25735,18 @@},"mistral/mistral-ocr-latest": {"litellm_provider": "mistral",- "ocr_cost_per_page": 0.001,- "annotation_cost_per_page": 0.003,+ "ocr_cost_per_page": 0.004,+ "annotation_cost_per_page": 0.005,+ "mode": "ocr",+ "supported_endpoints": [+ "/v1/ocr"+ ],+ "source": "https://mistral.ai/pricing#api-pricing"+ },+ "mistral/mistral-ocr-4-0": {+ "litellm_provider": "mistral",+ "ocr_cost_per_page": 0.004,+ "annotation_cost_per_page": 0.005,"mode": "ocr","supported_endpoints": ["/v1/ocr"diff --git a/tests/test_litellm/llms/mistral/ocr/test_mistral_ocr_cost.py b/tests/test_litellm/llms/mistral/ocr/test_mistral_ocr_cost.pynew file mode 100644index 00000000000..b98b0c22e50--- /dev/null+++ b/tests/test_litellm/llms/mistral/ocr/test_mistral_ocr_cost.py@@ -0,0 +1,40 @@+"""+Cost tests for Mistral OCR models against the real litellm cost map+(no monkeypatching of get_model_info). These regress the pricing entries+for mistral-ocr-4-0 and mistral-ocr-latest, which now both resolve to+OCR 4 at $4 / 1000 pages.+"""++import pytest++import litellm+from litellm.cost_calculator import completion_cost+from litellm.llms.base_llm.ocr.transformation import OCRPage, OCRResponse, OCRUsageInfo++OCR4_COST_PER_PAGE = 0.004+++def _ocr_response(model: str, pages_processed: int) -> OCRResponse:+ return OCRResponse(+ pages=[OCRPage(index=i, markdown=f"page {i}") for i in range(pages_processed)],+ model=model,+ usage_info=OCRUsageInfo(pages_processed=pages_processed),+ )++[email protected]("model", ["mistral-ocr-4-0", "mistral-ocr-latest"])+def test_model_info_ocr4_price(model: str) -> None:+ info = litellm.get_model_info(model=f"mistral/{model}", custom_llm_provider="mistral")+ assert info["ocr_cost_per_page"] == OCR4_COST_PER_PAGE++[email protected]("model", ["mistral-ocr-4-0", "mistral-ocr-latest"])[email protected]("pages_processed", [1, 3, 10])+def test_ocr4_cost_scales_with_pages(model: str, pages_processed: int) -> None:+ cost = completion_cost(+ completion_response=_ocr_response(model, pages_processed),+ model=f"mistral/{model}",+ custom_llm_provider="mistral",+ call_type="ocr",+ )+ assert cost == pytest.approx(OCR4_COST_PER_PAGE * pages_processed)diff --git a/tests/test_litellm/llms/mistral/ocr/test_mistral_ocr_transformation.py b/tests/test_litellm/llms/mistral/ocr/test_mistral_ocr_transformation.pyindex 97461561a05..8ce8b777f71 100644--- a/tests/test_litellm/llms/mistral/ocr/test_mistral_ocr_transformation.py+++ b/tests/test_litellm/llms/mistral/ocr/test_mistral_ocr_transformation.py@@ -5,6 +5,7 @@ Tests the supported OCR parameters and their mapping behaviour.No real API calls are made — all tests are fully mocked/local."""+import httpximport pytestfrom litellm.llms.mistral.ocr.transformation import MistralOCRConfig@@ -40,9 +41,7 @@ class TestGetSupportedOcrParams:"bbox_annotation_format","document_annotation_format",]:- assert (- param in supported- ), f"Previously supported param '{param}' is missing"+ assert param in supported, f"Previously supported param '{param}' is missing"class TestMapOcrParams:@@ -93,12 +92,11 @@ class TestNewSupportedParams:"table_format","confidence_scores_granularity","document_annotation_prompt",+ "include_blocks","id",],)- def test_new_param_in_supported_list(- self, config: MistralOCRConfig, param_name: str- ) -> None:+ def test_new_param_in_supported_list(self, config: MistralOCRConfig, param_name: str) -> None:supported = config.get_supported_ocr_params(model=MODEL)assert param_name in supported@@ -114,12 +112,11 @@ class TestNewParamsMapOcr:("confidence_scores_granularity", "word"),("confidence_scores_granularity", "page"),("document_annotation_prompt", "Extract all invoice line items"),+ ("include_blocks", True),("id", "req-123"),],)- def test_new_param_passed_through(- self, config: MistralOCRConfig, param_name: str, param_value: str- ) -> None:+ def test_new_param_passed_through(self, config: MistralOCRConfig, param_name: str, param_value: str) -> None:result = config.map_ocr_params(non_default_params={param_name: param_value},optional_params={},@@ -144,12 +141,11 @@ class TestTransformOcrRequest:("document_annotation_prompt", "Extract all invoice line items"),("id", "req-123"),("extract_header", True),+ ("include_blocks", True),("pages", [0, 1]),],)- def test_param_included_in_request_body(- self, config: MistralOCRConfig, param_name: str, param_value- ) -> None:+ def test_param_included_in_request_body(self, config: MistralOCRConfig, param_name: str, param_value) -> None:result = config.transform_ocr_request(model=MODEL,document=self.SAMPLE_DOCUMENT,@@ -176,3 +172,65 @@ class TestTransformOcrRequest:)for key, value in optional_params.items():assert result.data[key] == value+++class TestTransformOcrResponseOcr4Fields:+ """OCR 4 adds blocks, confidence_scores, tables, hyperlinks, header and footer+ to each page. These must survive transform_ocr_response so callers actually+ receive the new structured output rather than having it silently dropped."""++ def _response(self, page: dict) -> httpx.Response:+ return httpx.Response(+ 200,+ json={+ "pages": [page],+ "model": "mistral-ocr-4-0",+ "usage_info": {"pages_processed": 1},+ },+ )++ def test_blocks_and_confidence_scores_preserved(self, config: MistralOCRConfig) -> None:+ page = {+ "index": 0,+ "markdown": "# Invoice",+ "blocks": [+ {+ "type": "title",+ "top_left_x": 10,+ "top_left_y": 20,+ "bottom_right_x": 300,+ "bottom_right_y": 60,+ "content": "Invoice",+ }+ ],+ "confidence_scores": {"page": 0.98},+ }++ result = config.transform_ocr_response(+ model="mistral-ocr-4-0",+ raw_response=self._response(page),+ logging_obj=None,+ )++ assert result.pages[0].blocks == page["blocks"]+ assert result.pages[0].confidence_scores == page["confidence_scores"]++ def test_ocr4_fields_survive_model_dump(self, config: MistralOCRConfig) -> None:+ page = {+ "index": 0,+ "markdown": "table page",+ "tables": [{"rows": 2, "cols": 3}],+ "hyperlinks": ["https://example.com"],+ "header": "Acme Corp",+ "footer": "Page 1",+ }++ result = config.transform_ocr_response(+ model="mistral-ocr-4-0",+ raw_response=self._response(page),+ logging_obj=None,+ )++ dumped_page = result.model_dump()["pages"][0]+ for field in ("tables", "hyperlinks", "header", "footer"):+ assert dumped_page[field] == page[field]