Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions litellm/llms/azure/containers/transformation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from typing import Optional
from urllib.parse import parse_qs, urlparse, urlunparse

from litellm.llms.azure.common_utils import BaseAzureLLM
from litellm.llms.openai.containers.transformation import OpenAIContainerConfig
from litellm.types.router import GenericLiteLLMParams

# Endpoint-specific path suffixes that may appear in a deployment's api_base
# (e.g. the responses endpoint URL is stored as api_base for Azure models).
# Strip these before building the containers URL so we always start from the
# resource root (https://resource.cognitiveservices.azure.com).
_AZURE_ENDPOINT_PATHS = ("/openai/responses",)


class AzureContainerConfig(OpenAIContainerConfig):
"""
Expand All @@ -27,6 +34,27 @@ def validate_environment(
litellm_params=GenericLiteLLMParams(api_key=api_key),
)

@staticmethod
def _normalize_api_base(api_base: Optional[str]) -> Optional[str]:
"""Strip endpoint-specific path suffixes from api_base to get the resource root."""
if not api_base:
return api_base
parsed = urlparse(api_base)
path = parsed.path.rstrip("/")
for ep in _AZURE_ENDPOINT_PATHS:
if path.endswith(ep):
return urlunparse(
(parsed.scheme, parsed.netloc, path[: -len(ep)], "", "", "")
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
return api_base

@staticmethod
def _extract_api_version(api_base: Optional[str]) -> Optional[str]:
"""Return the api-version query param from api_base if present."""
if not api_base:
return None
return parse_qs(urlparse(api_base).query).get("api-version", [None])[0]

def get_complete_url(
self,
api_base: Optional[str],
Expand All @@ -39,10 +67,19 @@ def get_complete_url(
{endpoint}/openai/v1/containers
when api_version is 'v1', 'latest', or 'preview'; otherwise:
{endpoint}/openai/containers

The deployment's api_base may be the responses endpoint URL
(e.g. .../openai/responses?api-version=2025-04-01-preview). We
prefer the api-version embedded there over the deployment's
api_version field, which may point to an older chat API version.
"""
effective_params = dict(litellm_params)
api_version_from_base = self._extract_api_version(api_base)
if api_version_from_base:
effective_params["api_version"] = api_version_from_base
return BaseAzureLLM._get_base_azure_url(
api_base=api_base,
litellm_params=litellm_params,
api_base=self._normalize_api_base(api_base),
litellm_params=effective_params,
route="/openai/containers",
default_api_version="v1",
)
26 changes: 18 additions & 8 deletions litellm/llms/custom_httpx/container_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,31 @@ def _sync_handle(
returns_binary = endpoint_config.get("returns_binary", False)
is_multipart = endpoint_config.get("is_multipart", False)

# An empty dict passed as `params` to httpx strips any existing query
# string from the URL (e.g. ?api-version=...). Use None instead so
# httpx leaves the URL's own query string intact.
effective_params = query_params or None

try:
if method == "GET":
response = http_client.get(
url=url, headers=headers, params=query_params
url=url, headers=headers, params=effective_params
)
elif method == "DELETE":
response = http_client.delete(
url=url, headers=headers, params=query_params
url=url, headers=headers, params=effective_params
)
elif method == "POST":
if is_multipart and "file" in kwargs:
files, headers = _prepare_multipart_file_upload(
kwargs["file"], headers
)
response = http_client.post(
url=url, headers=headers, params=query_params, files=files
url=url, headers=headers, params=effective_params, files=files
)
else:
response = http_client.post(
url=url, headers=headers, params=query_params
url=url, headers=headers, params=effective_params
)
else:
raise ValueError(f"Unsupported HTTP method: {method}")
Expand Down Expand Up @@ -376,26 +381,31 @@ async def _async_handle(
returns_binary = endpoint_config.get("returns_binary", False)
is_multipart = endpoint_config.get("is_multipart", False)

# An empty dict passed as `params` to httpx strips any existing query
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
# string from the URL (e.g. ?api-version=...). Use None instead so
# httpx leaves the URL's own query string intact.
effective_params = query_params or None

try:
if method == "GET":
response = await http_client.get(
url=url, headers=headers, params=query_params
url=url, headers=headers, params=effective_params
)
elif method == "DELETE":
response = await http_client.delete(
url=url, headers=headers, params=query_params
url=url, headers=headers, params=effective_params
)
elif method == "POST":
if is_multipart and "file" in kwargs:
files, headers = _prepare_multipart_file_upload(
kwargs["file"], headers
)
response = await http_client.post(
url=url, headers=headers, params=query_params, files=files
url=url, headers=headers, params=effective_params, files=files
)
else:
response = await http_client.post(
url=url, headers=headers, params=query_params
url=url, headers=headers, params=effective_params
)
else:
raise ValueError(f"Unsupported HTTP method: {method}")
Expand Down
20 changes: 10 additions & 10 deletions litellm/llms/custom_httpx/llm_http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7815,7 +7815,7 @@ def container_list_handler(
response = sync_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_list_response(
Expand Down Expand Up @@ -7892,7 +7892,7 @@ async def async_container_list_handler(
response = await async_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_list_response(
Expand Down Expand Up @@ -7982,7 +7982,7 @@ def container_retrieve_handler(
response = sync_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_retrieve_response(
Expand Down Expand Up @@ -8059,7 +8059,7 @@ async def async_container_retrieve_handler(
response = await async_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_retrieve_response(
Expand Down Expand Up @@ -8149,7 +8149,7 @@ def container_delete_handler(
response = sync_httpx_client.delete(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_delete_response(
Expand Down Expand Up @@ -8226,7 +8226,7 @@ async def async_container_delete_handler(
response = await async_httpx_client.delete(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_delete_response(
Expand Down Expand Up @@ -8322,7 +8322,7 @@ def container_file_list_handler(
response = sync_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_file_list_response(
Expand Down Expand Up @@ -8401,7 +8401,7 @@ async def async_container_file_list_handler(
response = await async_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_file_list_response(
Expand Down Expand Up @@ -8489,7 +8489,7 @@ def container_file_content_handler(
response = sync_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_file_content_response(
Expand Down Expand Up @@ -8565,7 +8565,7 @@ async def async_container_file_content_handler(
response = await async_httpx_client.get(
url=url,
headers=headers,
params=params,
params=params or None,
)

return container_provider_config.transform_container_file_content_response(
Expand Down
10 changes: 9 additions & 1 deletion litellm/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5551,6 +5551,7 @@ async def _init_containers_api_endpoints(
from litellm.responses.utils import ResponsesAPIRequestUtils

container_id = kwargs.get("container_id")
_forwarded_model_id = kwargs.get("model_id")
if isinstance(container_id, str):
decoded = ResponsesAPIRequestUtils._decode_container_id(container_id)
original_id = decoded.get("response_id", container_id)
Expand All @@ -5559,7 +5560,14 @@ async def _init_containers_api_endpoints(
decoded_provider = decoded.get("custom_llm_provider")
if decoded_provider and kwargs.get("custom_llm_provider") == "openai":
kwargs["custom_llm_provider"] = decoded_provider
model_id = decoded.get("model_id")
# Fall back to the model_id forwarded by the proxy when the container_id
Comment thread
Sameerlite marked this conversation as resolved.
Resolved
# is a native upstream ID (e.g. Azure hex cntr_) that carries no LiteLLM
# routing payload, so deployment credentials (api_base, api_key) are applied.
model_id = decoded.get("model_id") or (
_forwarded_model_id.strip()
if isinstance(_forwarded_model_id, str) and _forwarded_model_id.strip()
else None
)
Comment thread
Sameerlite marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
if model_id:
kwargs["model"] = model_id
return await self._ageneric_api_call_with_fallbacks(
Expand Down
Loading
Loading