litellm/litellm/containers/utils.py
Mateo Wang 5084cef13c
refactor(typing): clear 1146 basedpyright Any errors across 365 files (#52768)
* refactor(typing): clear basedpyright Any errors in proxy, router and core utils

* refactor(typing): clear basedpyright Any errors in proxy server, logging and provider handlers

* refactor(typing): clear basedpyright Any errors in types, proxy client and single-Any modules

* refactor(typing): clear basedpyright Any errors in providers, responses and proxy endpoints

* refactor(typing): clear basedpyright Any errors in key management, prometheus, gigachat and http handler

* refactor(typing): clear basedpyright Any errors in a long-tail family sweep

* refactor(typing): clear basedpyright Any errors in decode, guardrail and passthrough paths

* refactor(typing): clear basedpyright Any errors in decode, realtime and management paths

* refactor(typing): clear basedpyright Any errors in source-owned proxy, cache and provider paths

* refactor(typing): clear basedpyright Any errors in wave 10 narrow deep pass

* refactor(typing): clear basedpyright Any errors in wave 11 hotspot and route request pass

* refactor(typing): clear basedpyright Any errors in wave 12 deep coupled pass

* refactor(typing): clear basedpyright Any errors in wave 13 family pass

* refactor(typing): clear basedpyright Any errors in wave 14 single-hotspot pass

* refactor(typing): clear basedpyright Any errors in wave 15 single-hotspot pass

* fix(typing): keep main's merged behavior typed after syncing with main

* refactor(typing): share one int_of helper across batch, file and key endpoints

* test: give mocked Anthropic calls a real message body

The Anthropic chat transform now rejects a decoded body that is not a JSON
object. Four tests mocked the HTTP client with a bare MagicMock, whose json()
is never a real decoded response, so they now return a real Anthropic message.

* fix(utils): keep FileTypes importable from litellm.utils

The wave 14 pass dropped the now-unused FileTypes import, which also removed the litellm.utils.FileTypes attribute SDK users could import. Re-export it as a TypeAlias the same way TextChoices is.

* fix: run real builtins instead of copied CPython errors and keep client SDK types

Replace hand-copied CPython error sites with the real operations through
small core_helpers wrappers, restore main's typed client SDK returns, and
add tests that compare each helper's result and error with the builtin's

* fix: raise instead of recursing in the typed narrowing helpers

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: call the value directly so the helpers test collects on Python 3.10

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor(typing): mark the three widened TypedDict fields read-only

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-21 04:25:33 +00:00

168 lines
6.5 KiB
Python

from collections.abc import Mapping
from types import MappingProxyType
from typing import Final, Protocol, TypeVar
from litellm.litellm_core_utils.core_helpers import require_attributes
from litellm.llms.base_llm.containers.transformation import BaseContainerConfig
from litellm.responses.utils import ResponsesAPIRequestUtils
from litellm.types.containers.main import (
ContainerCreateOptionalRequestParams,
ContainerListOptionalRequestParams,
)
from litellm.types.router import GenericLiteLLMParams
def decode_managed_container_id_for_request(
container_id: str,
custom_llm_provider: str,
litellm_params: GenericLiteLLMParams,
) -> tuple[str, str, GenericLiteLLMParams]:
"""Decode a LiteLLM-managed container ID for upstream API calls.
Returns:
(original_container_id, resolved_provider, updated_litellm_params)
"""
decoded: Final = ResponsesAPIRequestUtils.decode_container_id(container_id)
original_container_id: Final = decoded.get("response_id", container_id)
decoded_provider: Final = decoded.get("custom_llm_provider")
if decoded_provider and custom_llm_provider == "openai":
custom_llm_provider = decoded_provider
decoded_model_id: Final = decoded.get("model_id")
if decoded_model_id and not litellm_params.get("model_id"):
litellm_params["model_id"] = decoded_model_id
return original_container_id, custom_llm_provider, litellm_params
T = TypeVar("T")
class _LooksUpById(Protocol):
def get(self, key: str, /) -> object: ...
_NO_MODEL_INFO: Final[Mapping[str, object]] = MappingProxyType({})
class ContainerRequestUtils:
@staticmethod
def get_requested_container_create_optional_param(
passed_params: dict,
) -> ContainerCreateOptionalRequestParams:
"""Extract only valid container creation parameters from the passed parameters."""
container_create_optional_params: Final = ContainerCreateOptionalRequestParams()
valid_params: Final = [
"expires_after",
"file_ids",
"extra_headers",
"extra_body",
]
for param in valid_params:
if param in passed_params and passed_params[param] is not None:
container_create_optional_params[param] = passed_params[param]
return container_create_optional_params
@staticmethod
def get_optional_params_container_create(
container_provider_config: BaseContainerConfig,
container_create_optional_params: ContainerCreateOptionalRequestParams,
) -> dict:
"""Get the optional parameters for container creation."""
supported_params: Final = container_provider_config.get_supported_openai_params()
# Filter out unsupported parameters
filtered_params: Final = {k: v for k, v in container_create_optional_params.items() if k in supported_params}
return container_provider_config.map_openai_params(
container_create_optional_params=filtered_params,
drop_params=False,
)
@staticmethod
def get_requested_container_list_optional_param(
passed_params: dict,
) -> ContainerListOptionalRequestParams:
"""Extract only valid container list parameters from the passed parameters."""
container_list_optional_params: Final = ContainerListOptionalRequestParams()
valid_params: Final = [
"after",
"limit",
"order",
"extra_headers",
"extra_query",
]
for param in valid_params:
if param in passed_params and passed_params[param] is not None:
container_list_optional_params[param] = passed_params[param]
return container_list_optional_params
@staticmethod
def encode_container_id_in_response(
response_obj: T,
custom_llm_provider: str | None,
litellm_metadata: Mapping[str, object] | None = None,
extra_body: Mapping[str, object] | None = None,
) -> T:
"""
Encode container_id in response object with provider/model metadata for routing.
This mirrors the responses API pattern where response IDs are encoded with
routing metadata so follow-up calls can route to the correct provider.
Encodes when:
1. litellm_metadata contains model_info.id (indicating router/proxy usage), OR
2. extra_body contains target_model_names (indicating model-specific routing)
Direct SDK calls with explicit custom_llm_provider and no routing hints return raw IDs.
Args:
response_obj: Response object with an `id` attribute (ContainerObject, DeleteContainerResult, etc.)
custom_llm_provider: Provider name (e.g., "azure", "openai")
litellm_metadata: Optional litellm_metadata dict that may contain model_info.id
extra_body: Optional extra_body dict that may contain target_model_names
Returns:
The same response object with encoded container_id (if routing metadata present)
"""
# Extract model_id from litellm_metadata
litellm_metadata = litellm_metadata or {}
model_info: Final = litellm_metadata.get("model_info") or _NO_MODEL_INFO
model_id = require_attributes(model_info, _LooksUpById, "get").get("id")
# Check if we should encode based on routing metadata
should_encode = False
# Case 1: Router/proxy usage (model_id from router)
if model_id is not None:
should_encode = True
# Case 2: target_model_names in extra_body (model-specific routing)
if extra_body and "target_model_names" in extra_body:
should_encode = True
# Extract model_id from target_model_names if not already set
if model_id is None:
target_models: Final = extra_body["target_model_names"]
# Use first model as model_id for encoding
if isinstance(target_models, str):
model_id = target_models.split(",")[0].strip()
elif isinstance(target_models, list) and len(target_models) > 0:
model_id = str(target_models[0]).strip()
# Only encode if we have routing metadata
if should_encode and response_obj and hasattr(response_obj, "id"):
encoded_id: Final = ResponsesAPIRequestUtils.build_container_id(
custom_llm_provider=custom_llm_provider,
model_id=model_id,
container_id=response_obj.id,
)
response_obj.id = encoded_id
return response_obj