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
Add comments
Ephrim Stanleya5626768a307
diff --git a/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py b/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.pyindex 807d237df5c..8e4a154c3cb 100644--- a/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py+++ b/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py@@ -107,8 +107,9 @@ class CheckBatchCost:f"Batch ID: {batch_id} is complete, tracking cost and usage")- # Extract raw provider file ID from the unified file ID- # (async_post_call_success_hook may have replaced output_file_id with a unified ID)+ # This background job runs as default_user_id, so going through the HTTP endpoint+ # would trigger check_managed_file_id_access and get 403. Instead, extract the raw+ # provider file ID and call afile_content directly with deployment credentials.raw_output_file_id = response.output_file_iddecoded = _is_base64_encoded_unified_file_id(raw_output_file_id)if decoded:@@ -117,9 +118,6 @@ class CheckBatchCost:except (IndexError, AttributeError):pass- # Call litellm.afile_content directly with deployment credentials,- # bypassing the managed files access-control hooks that would- # reject this background job's default_user_id identitycredentials = self.llm_router.get_deployment_credentials_with_provider(model_id) or {}_file_content = await afile_content(file_id=raw_output_file_id,diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.pyindex 9601370ef3e..d9514bf28bc 100644--- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py+++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py@@ -230,7 +230,9 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):if managed_file:return managed_file.created_by == user_id- return True # allow through when record not found — downstream will return 404+ # When DB record is missing (file was deleted), allow through so downstream returns 404.+ # Matches can_user_call_unified_object_id which also returns True for missing records.+ return Trueasync def can_user_call_unified_object_id(self, unified_object_id: str, user_api_key_dict: UserAPIKeyAuth@@ -911,7 +913,8 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):)setattr(response, file_attr, unified_file_id)- # Fetch the actual file object from the provider+ # Use llm_router credentials when available. Without credentials,+ # Azure and other auth-required providers return 500/401.file_object = Nonetry:from litellm.proxy.proxy_server import llm_router as _llm_router@@ -1010,6 +1013,8 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):raise Exception(f"LiteLLM Managed File object with id={file_id} not found")# Case 2: Managed file and the file object exists in the database+ # The stored file_object has the raw provider ID. Replace with the unified ID+ # so callers see a consistent ID (matching Case 3 which does response.id = file_id).if stored_file_object and stored_file_object.file_object:stored_file_object.file_object.id = file_idreturn stored_file_object.file_objectdiff --git a/litellm/_service_logger.py b/litellm/_service_logger.pyindex e2759e4fab3..8f9a3c5083f 100644--- a/litellm/_service_logger.py+++ b/litellm/_service_logger.py@@ -312,6 +312,8 @@ class ServiceLogging(CustomLogger):_duration, type(_duration))) # invalid _duration value+ # Batch polling callbacks (check_batch_cost) don't include call_type in kwargs.+ # Use .get() to avoid KeyError.await self.async_service_success_hook(service=ServiceTypes.LITELLM,duration=_duration,diff --git a/litellm/integrations/s3_v2.py b/litellm/integrations/s3_v2.pyindex e847ce27476..e0932fc3373 100644--- a/litellm/integrations/s3_v2.py+++ b/litellm/integrations/s3_v2.py@@ -247,6 +247,8 @@ class S3Logger(CustomBatchLogger, BaseAWSLLM):standard_logging_payload=kwargs.get("standard_logging_object", None),)+ # afile_delete and other non-model call types never produce a standard_logging_object,+ # so s3_batch_logging_element is None. Skip gracefully instead of raising ValueError.if s3_batch_logging_element is None:verbose_logger.debug("s3 Logging - skipping event, no standard_logging_object for call_type=%s",diff --git a/litellm/proxy/batches_endpoints/endpoints.py b/litellm/proxy/batches_endpoints/endpoints.pyindex 25680ab1220..b125b90338b 100644--- a/litellm/proxy/batches_endpoints/endpoints.py+++ b/litellm/proxy/batches_endpoints/endpoints.py@@ -378,7 +378,8 @@ async def retrieve_batch(data=data, user_api_key_dict=user_api_key_dict, response=response)- # Resolve raw input_file_id to unified ID+ # async_post_call_success_hook replaces batch.id and output_file_id with unified IDs+ # but not input_file_id. Look up the unified ID from flat_model_file_ids.if (unified_batch_idand hasattr(response, "input_file_id")@@ -496,7 +497,8 @@ async def retrieve_batch(data=data, user_api_key_dict=user_api_key_dict, response=response)- # Resolve raw input_file_id to unified ID+ # Fix: bug_feb14_batch_retrieve_returns_raw_input_file_id (terminal state path)+ # Same as above — resolve raw provider input_file_id to unified ID.if (unified_batch_idand hasattr(response, "input_file_id")diff --git a/litellm/proxy/hooks/batch_rate_limiter.py b/litellm/proxy/hooks/batch_rate_limiter.pyindex 45b1bd8653f..5bebcc92072 100644--- a/litellm/proxy/hooks/batch_rate_limiter.py+++ b/litellm/proxy/hooks/batch_rate_limiter.py@@ -259,9 +259,10 @@ class _PROXY_BatchRateLimiter(CustomLogger):from litellm.proxy.openai_files_endpoints.common_utils import (_is_base64_encoded_unified_file_id,)+ # Managed files require bypassing the HTTP endpoint (which runs access-check hooks)+ # and calling the managed files hook directly with the user's credentials.is_managed_file = _is_base64_encoded_unified_file_id(file_id)if is_managed_file and user_api_key_dict is not None:- # For managed files, use the managed files hook directlyfile_content = await self._fetch_managed_file_content(file_id=file_id,user_api_key_dict=user_api_key_dict,diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.pyindex 94886293a08..d903ce0d9d7 100644--- a/litellm/proxy/hooks/proxy_track_cost_callback.py+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py@@ -202,6 +202,8 @@ class _ProxyDBLogger(CustomLogger):max_budget=end_user_max_budget,)else:+ # Non-model call types (health checks, afile_delete) have no model or standard_logging_object.+ # Use .get() for "stream" to avoid KeyError on health checks.if sl_object is None and not kwargs.get("model"):verbose_proxy_logger.warning("Cost tracking - skipping, no standard_logging_object and no model for call_type=%s",diff --git a/litellm/proxy/openai_files_endpoints/common_utils.py b/litellm/proxy/openai_files_endpoints/common_utils.pyindex 15c5bdfabb2..0e3b31b2aa7 100644--- a/litellm/proxy/openai_files_endpoints/common_utils.py+++ b/litellm/proxy/openai_files_endpoints/common_utils.py@@ -688,6 +688,7 @@ async def get_batch_from_database(response = LiteLLMBatch(**batch_data)response.id = batch_id+ # The stored batch object has the raw provider input_file_id. Resolve to unified ID.if response.input_file_id and not _is_base64_encoded_unified_file_id(response.input_file_id):try:managed_file = await prisma_client.db.litellm_managedfiletable.find_first(