refactor(ocr): delegate connection resolution to adapters

This commit is contained in:
Yujong Lee 2026-09-09 18:47:21 -07:00
parent f12d88856a
commit 0411680340
4 changed files with 102 additions and 23 deletions

View file

@ -92,6 +92,19 @@ class AzureDocumentIntelligenceOCRConfig(BaseOCRConfig):
def get_api_key_env_var(self) -> str | None:
return AZURE_DOCUMENT_INTELLIGENCE_API_KEY_ENV_VAR
def resolve_connection_params(
self,
*,
api_key: str | None,
api_base: str | None,
dynamic_api_key: str | None,
dynamic_api_base: str | None,
) -> tuple[str | None, str | None]:
return (
(dynamic_api_key or api_key) if api_key is not None else None,
(dynamic_api_base or api_base) if api_base is not None else None,
)
def get_supported_ocr_params(self, model: str) -> list:
"""
Get supported OCR parameters for Azure Document Intelligence.

View file

@ -144,6 +144,16 @@ class BaseOCRConfig:
"""
return None
def resolve_connection_params(
self,
*,
api_key: str | None,
api_base: str | None,
dynamic_api_key: str | None,
dynamic_api_base: str | None,
) -> tuple[str | None, str | None]:
return dynamic_api_key or api_key, dynamic_api_base or api_base
def supports_rust_bridge(self) -> bool:
"""Whether the Rust OCR bridge may serve this config when it is enabled for the provider."""
return True

View file

@ -94,9 +94,6 @@ 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
(
model,
custom_llm_provider,
@ -109,21 +106,6 @@ def _prepare_ocr_request(
api_key=api_key,
)
suppress_dynamic_api_base: Final = (
not caller_supplied_api_base
and custom_llm_provider == "azure_ai"
and is_azure_document_intelligence_model(model)
)
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
ocr_provider_config: Final = ProviderConfigManager.get_provider_ocr_config(
model=model,
provider=litellm.LlmProviders(custom_llm_provider),
@ -132,6 +114,13 @@ def _prepare_ocr_request(
if ocr_provider_config is None:
raise ValueError(f"OCR is not supported for provider: {custom_llm_provider}")
resolved_api_key, resolved_api_base = ocr_provider_config.resolve_connection_params(
api_key=api_key,
api_base=api_base,
dynamic_api_key=dynamic_api_key,
dynamic_api_base=dynamic_api_base,
)
verbose_logger.debug("OCR call - model: %s, provider: %s", model, custom_llm_provider)
litellm_params: Final = GenericLiteLLMParams.model_validate(kwargs)
@ -176,7 +165,7 @@ def _prepare_ocr_request(
optional_params=optional_params,
litellm_params={
"litellm_call_id": litellm_call_id,
"api_base": api_base,
"api_base": resolved_api_base,
},
custom_llm_provider=custom_llm_provider,
)
@ -184,8 +173,8 @@ def _prepare_ocr_request(
return _PreparedOCRRequest(
model=model,
document=document,
api_key=api_key,
api_base=api_base,
api_key=resolved_api_key,
api_base=resolved_api_base,
custom_llm_provider=custom_llm_provider,
extra_headers=extra_headers,
provider_config=ocr_provider_config,

View file

@ -1,16 +1,19 @@
"""
Regression tests for Azure Document Intelligence api_base resolution in OCR.
Regression tests for Azure Document Intelligence connection resolution in OCR.
`azure_ai` exposes two OCR services on one provider; the `doc-intelligence`
sub-route must resolve to `AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT`, not to the
generic `AZURE_AI_API_BASE` fallback that `get_llm_provider` injects. These tests
pin that routing and guard the backwards-compatibility contract that an explicitly
supplied api_base is always honoured.
supplied connection parameters are always honoured.
"""
import pytest
from litellm.llms.azure_ai.ocr.common_utils import (
is_azure_document_intelligence_model,
)
from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler
from litellm.ocr.main import _prepare_ocr_request, _rust_bridge_api_base
_DOC = {"type": "document_url", "document_url": "https://example.com/doc.pdf"}
@ -24,6 +27,9 @@ class _FakeLogging:
def update_from_kwargs(self, **kwargs: object) -> None:
return None
def pre_call(self, **kwargs: object) -> None:
return None
def _resolve_secret(name: str) -> str | None:
return {
@ -112,9 +118,70 @@ class TestDocIntelligenceApiKeyResolution:
assert prepared.api_key == "explicit-key"
def test_explicit_secret_references_are_preserved(self, monkeypatch):
monkeypatch.setenv("AZURE_AI_API_KEY", _AZURE_AI_API_KEY)
monkeypatch.setenv("AZURE_AI_API_BASE", _AZURE_AI_API_BASE)
prepared = _prepare(
"azure_ai/doc-intelligence/prebuilt-layout",
"os.environ/AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT",
api_key="os.environ/AZURE_DOCUMENT_INTELLIGENCE_API_KEY",
)
assert prepared.api_key == "os.environ/AZURE_DOCUMENT_INTELLIGENCE_API_KEY"
assert prepared.api_base == "os.environ/AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT"
def test_generic_azure_credentials_are_not_forwarded_to_doc_intelligence(self, monkeypatch):
monkeypatch.setenv("AZURE_AI_API_KEY", _AZURE_AI_API_KEY)
monkeypatch.setenv("AZURE_AI_API_BASE", _AZURE_AI_API_BASE)
monkeypatch.delenv("AZURE_DOCUMENT_INTELLIGENCE_API_KEY", raising=False)
monkeypatch.delenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT", raising=False)
prepared = _prepare("azure_ai/doc-intelligence/prebuilt-layout", None, api_key=None)
assert prepared.api_key is None
assert prepared.api_base is None
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
@pytest.mark.asyncio
async def test_sync_and_async_request_headers_use_document_intelligence_key(monkeypatch):
monkeypatch.setenv("AZURE_AI_API_KEY", _AZURE_AI_API_KEY)
monkeypatch.setenv("AZURE_AI_API_BASE", _AZURE_AI_API_BASE)
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)
logging = _FakeLogging()
handler = BaseLLMHTTPHandler()
sync_headers, _, _, _ = handler._prepare_ocr_request(
model=prepared.model,
document=prepared.document,
optional_params=prepared.optional_params,
logging_obj=logging,
api_key=prepared.api_key,
api_base=prepared.api_base,
headers=None,
provider_config=prepared.provider_config,
litellm_params=prepared.litellm_params,
)
async_headers, _, _, _ = await handler._async_prepare_ocr_request(
model=prepared.model,
document=prepared.document,
optional_params=prepared.optional_params,
logging_obj=logging,
api_key=prepared.api_key,
api_base=prepared.api_base,
headers=None,
provider_config=prepared.provider_config,
litellm_params=prepared.litellm_params,
)
assert sync_headers["Ocp-Apim-Subscription-Key"] == _DOC_INTELLIGENCE_API_KEY
assert async_headers["Ocp-Apim-Subscription-Key"] == _DOC_INTELLIGENCE_API_KEY