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
refactor(batches): share the trusted-credentials helper across both call paths
Marty Sullivand7afc1797cf2
diff --git a/litellm/batches/main.py b/litellm/batches/main.pyindex bb04d495555..20d38bbb77f 100644--- a/litellm/batches/main.py+++ b/litellm/batches/main.py@@ -22,7 +22,7 @@ from openai.types.batch import BatchRequestCountsimport litellmfrom litellm._logging import verbose_logger-from litellm.files.main import _add_trusted_model_credentials_to_litellm_params+from litellm.litellm_core_utils.get_litellm_params import add_trusted_model_credentials_to_litellm_paramsfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObjfrom litellm.llms.anthropic.batches.handler import AnthropicBatchesHandlerfrom litellm.llms.azure.batches.handler import AzureBatchesAPI@@ -528,7 +528,7 @@ def retrieve_batch(custom_llm_provider=custom_llm_provider,**kwargs,)- _add_trusted_model_credentials_to_litellm_params(litellm_params, kwargs)+ add_trusted_model_credentials_to_litellm_params(litellm_params, kwargs)if litellm_logging_obj is not None:litellm_logging_obj.update_from_kwargs(kwargs=kwargs,diff --git a/litellm/files/main.py b/litellm/files/main.pyindex 34421d13761..9a64c78552b 100644--- a/litellm/files/main.py+++ b/litellm/files/main.py@@ -11,7 +11,6 @@ import timeimport uuid as uuid_modulefrom collections.abc import Coroutinefrom functools import partial-from types import MappingProxyTypefrom typing import Any, Final, Literal, castimport httpx@@ -34,6 +33,7 @@ import litellmfrom litellm import get_secret_strfrom litellm.files.streaming import FileContentStreamingResponsefrom litellm.files.types import FileContentProvider, FileContentStreamingResult+from litellm.litellm_core_utils.get_litellm_params import add_trusted_model_credentials_to_litellm_paramsfrom litellm.litellm_core_utils.get_llm_provider_logic import get_llm_providerfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObjfrom litellm.llms.azure.common_utils import get_azure_credentials@@ -85,14 +85,6 @@ bedrock_files_instance: Final = BedrockFilesHandler()#################################################-def _add_trusted_model_credentials_to_litellm_params(- litellm_params_dict: dict[str, Any], kwargs: dict[str, Any]-) -> None:- trusted_model_credentials: Final = kwargs.get("_litellm_internal_model_credentials")- if isinstance(trusted_model_credentials, type(MappingProxyType({}))):- litellm_params_dict["_litellm_internal_model_credentials"] = trusted_model_credentials--@clientasync def acreate_file(file: FileTypes,@@ -372,7 +364,7 @@ def file_retrieve()if provider_config is not None:litellm_params_dict: Final = get_litellm_params(**kwargs)- _add_trusted_model_credentials_to_litellm_params(+ add_trusted_model_credentials_to_litellm_params(litellm_params_dict=litellm_params_dict,kwargs=kwargs,)@@ -494,7 +486,7 @@ def file_delete(passoptional_params: Final = GenericLiteLLMParams(**kwargs)litellm_params_dict: Final = get_litellm_params(**kwargs)- _add_trusted_model_credentials_to_litellm_params(+ add_trusted_model_credentials_to_litellm_params(litellm_params_dict=litellm_params_dict,kwargs=kwargs,)@@ -834,7 +826,7 @@ def file_content(try:optional_params: Final = GenericLiteLLMParams(**kwargs)litellm_params_dict: Final = get_litellm_params(**kwargs)- _add_trusted_model_credentials_to_litellm_params(+ add_trusted_model_credentials_to_litellm_params(litellm_params_dict=litellm_params_dict,kwargs=kwargs,)diff --git a/litellm/litellm_core_utils/get_litellm_params.py b/litellm/litellm_core_utils/get_litellm_params.pyindex f251ab4d74a..3eb8c163d5c 100644--- a/litellm/litellm_core_utils/get_litellm_params.py+++ b/litellm/litellm_core_utils/get_litellm_params.py@@ -1,3 +1,5 @@+from collections.abc import Mapping, MutableMapping+from types import MappingProxyTypefrom typing import Finalfrom litellm.llms.openai.data_residency import infer_openai_data_residency@@ -184,3 +186,19 @@ def get_litellm_params(litellm_params[key] = kwargs[key]return litellm_params+++def add_trusted_model_credentials_to_litellm_params(+ litellm_params_dict: MutableMapping[str, object], kwargs: Mapping[str, object]+) -> None:+ """+ Carry the immutable server-side credential snapshot into litellm_params.++ get_litellm_params has a fixed signature, so callers that need the snapshot to+ survive into the logging object and the downstream file read have to re-add it. Only+ a MappingProxyType is accepted, since providers resolve trusted configuration such+ as a Bedrock file bucket from it and must not read a request-supplied mapping.+ """+ trusted_model_credentials: Final = kwargs.get("_litellm_internal_model_credentials")+ if isinstance(trusted_model_credentials, MappingProxyType):+ litellm_params_dict["_litellm_internal_model_credentials"] = trusted_model_credentialsdiff --git a/litellm/proxy/batches_endpoints/endpoints.py b/litellm/proxy/batches_endpoints/endpoints.pyindex 1301b9327ec..9a6bb054d1f 100644--- a/litellm/proxy/batches_endpoints/endpoints.py+++ b/litellm/proxy/batches_endpoints/endpoints.py@@ -25,7 +25,7 @@ from litellm.proxy.openai_files_endpoints.common_utils import (_is_base64_encoded_unified_file_id,apply_team_provider_credentials,decode_model_from_file_id,- add_internal_model_credentials_for_batch,+ add_internal_model_credentials,encode_batch_response_ids,encode_file_id_with_model,ensure_batch_response_managed_file_ids,@@ -539,7 +539,7 @@ async def retrieve_batch()if unified_batch_id:- add_internal_model_credentials_for_batch(+ add_internal_model_credentials(data=data,llm_router=llm_router,model_id=get_model_id_from_unified_batch_id(unified_batch_id),diff --git a/litellm/proxy/openai_files_endpoints/common_utils.py b/litellm/proxy/openai_files_endpoints/common_utils.pyindex 32676bd1d9f..f2e6fb633e1 100644--- a/litellm/proxy/openai_files_endpoints/common_utils.py+++ b/litellm/proxy/openai_files_endpoints/common_utils.py@@ -465,7 +465,7 @@ def apply_team_provider_credentials(prepare_data_with_credentials(data=data, credentials=credentials)-def add_internal_model_credentials_for_batch(+def add_internal_model_credentials(data: dict,llm_router: "Router",model_id: str | None,diff --git a/litellm/proxy/openai_files_endpoints/files_endpoints.py b/litellm/proxy/openai_files_endpoints/files_endpoints.pyindex 1cbed2a68f9..361b5b920e2 100644--- a/litellm/proxy/openai_files_endpoints/files_endpoints.py+++ b/litellm/proxy/openai_files_endpoints/files_endpoints.py@@ -7,7 +7,6 @@import asyncioimport traceback-from types import MappingProxyTypefrom typing import Any, BinaryIO, Final, cast, get_argsimport httpx@@ -44,6 +43,7 @@ from litellm.proxy.common_utils.openai_endpoint_utils import ()from litellm.proxy.openai_files_endpoints.common_utils import (_is_base64_encoded_unified_file_id,+ add_internal_model_credentials,apply_team_provider_credentials,encode_file_id_with_model,extract_file_creation_params,@@ -707,18 +707,12 @@ async def get_file_content(model: Final = cast(str | None, data.get("model"))if model:- deployment_credentials: Final = llm_router.get_deployment_credentials_with_provider(model_id=model)- trusted_model_credentials: Final = (- {"_litellm_internal_model_credentials": MappingProxyType(dict(deployment_credentials))}- if deployment_credentials is not None- else {}- )+ add_internal_model_credentials(data=data, llm_router=llm_router, model_id=model)response = await llm_router.afile_content(**{"model": model,"file_id": file_id,**data,- **trusted_model_credentials,})diff --git a/tests/test_litellm/proxy/openai_files_endpoint/test_files_common_utils.py b/tests/test_litellm/proxy/openai_files_endpoint/test_files_common_utils.pyindex a39f0c5f010..ad7f5e4725a 100644--- a/tests/test_litellm/proxy/openai_files_endpoint/test_files_common_utils.py+++ b/tests/test_litellm/proxy/openai_files_endpoint/test_files_common_utils.py@@ -98,7 +98,7 @@ def test_apply_unified_file_ids_swaps_all_three_ids():# =========================================================================== #-# add_internal_model_credentials_for_batch - the snapshot that lets a completed+# add_internal_model_credentials - the snapshot that lets a completed# batch's output file be read, and therefore its cost be recorded# =========================================================================== #@@ -108,7 +108,7 @@ def test_add_internal_model_credentials_attaches_an_immutable_snapshot():that bucket only from this snapshot. It must be immutable so nothing downstream canredirect the bucket that managed file ids are validated against."""from litellm.proxy.openai_files_endpoints.common_utils import (- add_internal_model_credentials_for_batch,+ add_internal_model_credentials,)router = MagicMock()@@ -117,7 +117,7 @@ def test_add_internal_model_credentials_attaches_an_immutable_snapshot():)data = {"batch_id": "unified-batch-id"}- add_internal_model_credentials_for_batch(data=data, llm_router=router, model_id="deployment-1")+ add_internal_model_credentials(data=data, llm_router=router, model_id="deployment-1")snapshot = data["_litellm_internal_model_credentials"]assert snapshot["s3_bucket_name"] == "configured-bucket"@@ -136,14 +136,14 @@ def test_add_internal_model_credentials_is_a_noop_without_a_resolvable_deploymen"""An unroutable batch must be left alone rather than given an empty snapshot, whichwould look like a configured bucket of nothing."""from litellm.proxy.openai_files_endpoints.common_utils import (- add_internal_model_credentials_for_batch,+ add_internal_model_credentials,)router = MagicMock()router.get_deployment_credentials_with_provider = MagicMock(return_value=credentials)data = {"batch_id": "unified-batch-id"}- add_internal_model_credentials_for_batch(data=data, llm_router=router, model_id=model_id)+ add_internal_model_credentials(data=data, llm_router=router, model_id=model_id)assert "_litellm_internal_model_credentials" not in data@@ -153,13 +153,13 @@ def test_add_internal_model_credentials_survives_a_failing_deployment_lookup():resolves, which happens when a model group is removed while batches are in flight,must still be retrievable rather than failing the request on the lookup."""from litellm.proxy.openai_files_endpoints.common_utils import (- add_internal_model_credentials_for_batch,+ add_internal_model_credentials,)router = MagicMock()router.get_deployment_credentials_with_provider = MagicMock(side_effect=KeyError("deployment-gone"))data = {"batch_id": "unified-batch-id"}- add_internal_model_credentials_for_batch(data=data, llm_router=router, model_id="deployment-gone")+ add_internal_model_credentials(data=data, llm_router=router, model_id="deployment-gone")assert data == {"batch_id": "unified-batch-id"}