From f12d88856abaceb96140cab5fe2d3ea0be3aa27e Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Wed, 9 Sep 2026 17:55:33 -0700 Subject: [PATCH] fix(ocr): prefer document intelligence env key --- litellm/ocr/main.py | 8 +++- ...cr_azure_document_intelligence_api_base.py | 39 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/litellm/ocr/main.py b/litellm/ocr/main.py index df3f9d2096b..f553d55d175 100644 --- a/litellm/ocr/main.py +++ b/litellm/ocr/main.py @@ -94,6 +94,7 @@ def _prepare_ocr_request( if doc_type not in ["document_url", "image_url"]: raise ValueError(f"Invalid document type: {doc_type}. Must be 'document_url', 'image_url', or 'file'") + caller_supplied_api_key: Final = api_key is not None caller_supplied_api_base: Final = api_base is not None ( @@ -113,7 +114,12 @@ def _prepare_ocr_request( and custom_llm_provider == "azure_ai" and is_azure_document_intelligence_model(model) ) - if dynamic_api_key: + suppress_dynamic_api_key: Final = ( + not caller_supplied_api_key + and custom_llm_provider == "azure_ai" + and is_azure_document_intelligence_model(model) + ) + if dynamic_api_key and not suppress_dynamic_api_key: api_key = dynamic_api_key if dynamic_api_base and not suppress_dynamic_api_base: api_base = dynamic_api_base diff --git a/tests/test_litellm/ocr/test_ocr_azure_document_intelligence_api_base.py b/tests/test_litellm/ocr/test_ocr_azure_document_intelligence_api_base.py index 0c8b1cc2836..057b683abc9 100644 --- a/tests/test_litellm/ocr/test_ocr_azure_document_intelligence_api_base.py +++ b/tests/test_litellm/ocr/test_ocr_azure_document_intelligence_api_base.py @@ -16,6 +16,8 @@ from litellm.ocr.main import _prepare_ocr_request, _rust_bridge_api_base _DOC = {"type": "document_url", "document_url": "https://example.com/doc.pdf"} _DOC_INTELLIGENCE_ENDPOINT = "https://di.cognitiveservices.azure.com" _AZURE_AI_API_BASE = "https://generic-azure-ai.example.com" +_DOC_INTELLIGENCE_API_KEY = "document-intelligence-key" +_AZURE_AI_API_KEY = "generic-azure-ai-key" class _FakeLogging: @@ -30,11 +32,11 @@ def _resolve_secret(name: str) -> str | None: }.get(name) -def _prepare(model: str, api_base: str | None): +def _prepare(model: str, api_base: str | None, api_key: str | None = "test-key"): return _prepare_ocr_request( model=model, document=dict(_DOC), - api_key="test-key", + api_key=api_key, api_base=api_base, timeout=None, custom_llm_provider=None, @@ -83,3 +85,36 @@ class TestDocIntelligenceApiBaseResolution: prepared = _prepare("azure_ai/mistral-document-ai-2505", None) assert prepared.api_base == _AZURE_AI_API_BASE + + +class TestDocIntelligenceApiKeyResolution: + def test_dedicated_key_wins_over_generic_azure_ai_fallback(self, monkeypatch): + monkeypatch.setenv("AZURE_AI_API_KEY", _AZURE_AI_API_KEY) + monkeypatch.setenv("AZURE_DOCUMENT_INTELLIGENCE_API_KEY", _DOC_INTELLIGENCE_API_KEY) + monkeypatch.setenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT", _DOC_INTELLIGENCE_ENDPOINT) + + prepared = _prepare("azure_ai/doc-intelligence/prebuilt-layout", None, api_key=None) + headers = prepared.provider_config.validate_environment( + headers={}, + model=prepared.model, + api_key=prepared.api_key, + api_base=prepared.api_base, + litellm_params=prepared.litellm_params, + ) + + assert headers["Ocp-Apim-Subscription-Key"] == _DOC_INTELLIGENCE_API_KEY + + def test_explicit_key_is_honoured_for_doc_intelligence(self, monkeypatch): + monkeypatch.setenv("AZURE_AI_API_KEY", _AZURE_AI_API_KEY) + monkeypatch.setenv("AZURE_DOCUMENT_INTELLIGENCE_API_KEY", _DOC_INTELLIGENCE_API_KEY) + + prepared = _prepare("azure_ai/doc-intelligence/prebuilt-layout", None, api_key="explicit-key") + + assert prepared.api_key == "explicit-key" + + def test_generic_azure_ai_key_still_applies_to_mistral_ocr(self, monkeypatch): + monkeypatch.setenv("AZURE_AI_API_KEY", _AZURE_AI_API_KEY) + + prepared = _prepare("azure_ai/mistral-document-ai-2505", None, api_key=None) + + assert prepared.api_key == _AZURE_AI_API_KEY