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
fix(llms): join api_base and route with one shared helper (#47535)
Mateo Wang66c8f5b1a9ce
diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.ymlindex c593acbe8fb..2276efc4e34 100644--- a/.github/workflows/test-unit.yml+++ b/.github/workflows/test-unit.yml@@ -216,10 +216,12 @@ jobs:- shard: llms-providerstest-path: |-+ tests/unit/llms/test_api_base_trailing_slash_routes.pytests/unit/llms/test_cache_control_and_reasoning.pytests/unit/llms/test_custom_llm.pytests/unit/llms/test_file_content_block.pytests/unit/llms/test_file_search_responses.py+ tests/unit/llms/test_get_complete_url_trailing_slash.pytests/unit/llms/test_lifecycle_fix.pytests/unit/llms/test_oss_decision.pytests/unit/llms/test_polling_url_origin_match.pydiff --git a/litellm/litellm_core_utils/url_utils.py b/litellm/litellm_core_utils/url_utils.pyindex 43e16599bf6..4a1eaa87d3a 100644--- a/litellm/litellm_core_utils/url_utils.py+++ b/litellm/litellm_core_utils/url_utils.py@@ -23,7 +23,7 @@ import asyncioimport socketfrom ipaddress import ip_address, ip_networkfrom typing import Any, Final, Protocol-from urllib.parse import quote, urlparse, urlunparse+from urllib.parse import quote, urlparse, urlsplit, urlunparseimport httpxfrom typing_extensions import ReadOnly, TypedDict@@ -129,6 +129,28 @@ def encode_url_path_segments(value: object, *, field_name: str = "path") -> str:return "/".join(encode_url_path_segment(segment, field_name=field_name) for segment in value_str.split("/"))+def _strip_trailing_slashes_keeping_the_scheme(api_base: str) -> str:+ stripped: Final = api_base.rstrip("/")+ parts: Final = urlsplit(stripped)+ if parts.scheme and not (parts.netloc or parts.path or parts.query or parts.fragment):+ return api_base+ if not parts.query or not parts.path.endswith("/"):+ return stripped+ url_before_query, _, query = stripped.partition("?")+ return f"{url_before_query.rstrip('/')}?{query}"+++def join_api_base(api_base: str, route: str) -> str:+ return f"{_strip_trailing_slashes_keeping_the_scheme(api_base)}/{route.lstrip('/')}"+++def ensure_api_route(api_base: str, route: str) -> str:+ base: Final = _strip_trailing_slashes_keeping_the_scheme(api_base)+ if urlsplit(base).path.endswith(f"/{route.lstrip('/')}"):+ return base+ return join_api_base(base, route)++def _is_blocked_ip(addr: str) -> bool:"""Return True for any IP not safe to reach from a user-supplied URL.diff --git a/litellm/llms/aiml/image_generation/transformation.py b/litellm/llms/aiml/image_generation/transformation.pyindex 931038689cc..bb981983e98 100644--- a/litellm/llms/aiml/image_generation/transformation.py+++ b/litellm/llms/aiml/image_generation/transformation.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -121,7 +122,7 @@ class AimlImageGenerationConfig(BaseImageGenerationConfig):complete_url = complete_url.rstrip("/")# Strip /v1 suffix if present since IMAGE_GENERATION_ENDPOINT already includes v1complete_url = complete_url.removesuffix("/v1")- complete_url = f"{complete_url}/{self.IMAGE_GENERATION_ENDPOINT}"+ complete_url = join_api_base(complete_url, self.IMAGE_GENERATION_ENDPOINT)return complete_urldef validate_environment(diff --git a/litellm/llms/aiohttp_openai/chat/transformation.py b/litellm/llms/aiohttp_openai/chat/transformation.pyindex dba7a7405ab..74f782b56e7 100644--- a/litellm/llms/aiohttp_openai/chat/transformation.py+++ b/litellm/llms/aiohttp_openai/chat/transformation.py@@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Any, Finalfrom aiohttp import ClientResponsefrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.openai_like.chat.transformation import OpenAILikeChatConfigfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.utils import Choices, ModelResponse@@ -44,12 +45,7 @@ class AiohttpOpenAIChatConfig(OpenAILikeChatConfig):Ensure - /v1/chat/completions is at the end of the url"""- if api_base is None:- api_base = "https://api.openai.com"-- if not api_base.endswith("/chat/completions"):- api_base += "/chat/completions"- return api_base+ return ensure_api_route("https://api.openai.com" if api_base is None else api_base, "chat/completions")def validate_environment(self,diff --git a/litellm/llms/anthropic/batches/transformation.py b/litellm/llms/anthropic/batches/transformation.pyindex b95b734c761..da6c2871289 100644--- a/litellm/llms/anthropic/batches/transformation.py+++ b/litellm/llms/anthropic/batches/transformation.py@@ -7,7 +7,7 @@ import httpxfrom httpx import Headers, Responsefrom typing_extensions import ReadOnly, TypedDict-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, ensure_api_route, join_api_basefrom litellm.llms.base_llm.batches.transformation import BaseBatchesConfigfrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.llms.openai import AllMessageValues, CreateBatchRequest@@ -106,10 +106,8 @@ class AnthropicBatchesConfig(BaseBatchesConfig):data: CreateBatchRequest,) -> str:"""Get the complete URL for batch creation request."""- api_base = api_base or self.anthropic_model_info.get_api_base(api_base)- if not api_base.endswith("/v1/messages/batches"):- api_base = f"{api_base.rstrip('/')}/v1/messages/batches"- return api_base+ api_base = self.anthropic_model_info.resolve_anthropic_api_base(api_base)+ return ensure_api_route(api_base, "v1/messages/batches")def transform_create_batch_request(self,@@ -158,9 +156,9 @@ class AnthropicBatchesConfig(BaseBatchesConfig):Returns:Complete URL for Anthropic batch retrieval: {api_base}/v1/messages/batches/{batch_id}"""- api_base = api_base or self.anthropic_model_info.get_api_base(api_base)+ api_base = self.anthropic_model_info.resolve_anthropic_api_base(api_base)encoded_batch_id: Final = encode_url_path_segment(batch_id, field_name="batch_id")- return f"{api_base.rstrip('/')}/v1/messages/batches/{encoded_batch_id}"+ return join_api_base(api_base, f"v1/messages/batches/{encoded_batch_id}")def transform_retrieve_batch_request(self,diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.pyindex 6f08d6b0d61..1f5e4e7de39 100644--- a/litellm/llms/anthropic/common_utils.py+++ b/litellm/llms/anthropic/common_utils.py@@ -35,6 +35,7 @@ from litellm.litellm_core_utils.prompt_templates.mid_conversation_system importparts_of,)from litellm.litellm_core_utils.reasoning_effort_utils import non_numeric_thinking_budget_error+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.wif import (aget_anthropic_wif_token,anthropic_base_without_chat_suffix,@@ -468,7 +469,7 @@ def _fetch_anthropic_models_page(# which a read-only one cannot supportquery: Final = f"?after_id={quote(after_id)}" if after_id else ""response: Final = litellm.module_level_client.get(- url=f"{api_base}/v1/models{query}",+ url=f"{join_api_base(api_base, 'v1/models')}{query}",headers=headers,follow_redirects=False,)@@ -1406,7 +1407,7 @@ class AnthropicModelInfo(BaseLLMModelInfo):return {**caller_headers, **anthropic_headers}@staticmethod- def get_api_base(api_base: str | None = None) -> str | None:+ def resolve_anthropic_api_base(api_base: str | None = None) -> str:from litellm.secret_managers.main import get_secret_strreturn (@@ -1416,6 +1417,10 @@ class AnthropicModelInfo(BaseLLMModelInfo):or "https://api.anthropic.com")+ @staticmethod+ def get_api_base(api_base: str | None = None) -> str | None:+ return AnthropicModelInfo.resolve_anthropic_api_base(api_base)+@staticmethoddef get_api_key(api_key: str | None = None) -> str | None:"""An empty or whitespace-only key counts as unset: it can never authenticate anything, and@@ -1533,14 +1538,14 @@ class AnthropicModelInfo(BaseLLMModelInfo):api_base: str | None,litellm_params: Mapping[str, object] | None,) -> list[str]: # mutable-ok: matches get_models' list[str] contract shared by every provider override- resolved_api_base: Final = AnthropicModelInfo.get_api_base(api_base)+ resolved_api_base: Final = AnthropicModelInfo.resolve_anthropic_api_base(api_base)auth_header: Final = AnthropicModelInfo.get_auth_header(api_key,resolved_api_base,litellm_params=litellm_params,allow_workload_identity=config_allows_workload_identity(self),)- if resolved_api_base is None or auth_header is None:+ if auth_header is None:raise ValueError("ANTHROPIC_API_BASE/ANTHROPIC_BASE_URL or ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN (or workload ""identity federation via ANTHROPIC_FEDERATION_RULE_ID/ANTHROPIC_ORGANIZATION_ID/"diff --git a/litellm/llms/anthropic/count_tokens/transformation.py b/litellm/llms/anthropic/count_tokens/transformation.pyindex 592b5fb0f00..c2d87c641be 100644--- a/litellm/llms/anthropic/count_tokens/transformation.py+++ b/litellm/llms/anthropic/count_tokens/transformation.py@@ -25,6 +25,7 @@ from litellm.litellm_core_utils.prompt_templates.mid_conversation_system importanthropic_system_blocks,split_leading_system_run,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.common_utils import merge_anthropic_beta_headersfrom litellm.llms.anthropic.wif import resolve_anthropic_basefrom litellm.types.llms.openai import AllMessageValues, ChatCompletionImageObject, ValidUserMessageContentTypes@@ -317,7 +318,7 @@ class AnthropicCountTokensConfig:Returns:The endpoint URL for the CountTokens API"""- return resolve_anthropic_base(api_base) + "/v1/messages/count_tokens"+ return join_api_base(resolve_anthropic_base(api_base), "v1/messages/count_tokens")def transform_request_to_count_tokens(self,diff --git a/litellm/llms/anthropic/files/handler.py b/litellm/llms/anthropic/files/handler.pyindex f1f9c204b58..1a793e32317 100644--- a/litellm/llms/anthropic/files/handler.py+++ b/litellm/llms/anthropic/files/handler.py@@ -10,7 +10,7 @@ import litellmfrom litellm._logging import verbose_loggerfrom litellm._uuid import uuidfrom litellm.litellm_core_utils.litellm_logging import Logging-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.custom_httpx.http_handler import get_async_httpx_clientfrom litellm.types.llms.openai import (FileContentRequest,@@ -74,7 +74,7 @@ class AnthropicFilesHandler:batch_id = file_id# Get Anthropic API credentials- api_base = self.anthropic_model_info.get_api_base(api_base)+ api_base = self.anthropic_model_info.resolve_anthropic_api_base(api_base)auth_header: Final = await self.anthropic_model_info.aget_auth_header(api_key, api_base, litellm_params=litellm_params, allow_workload_identity=True)@@ -84,7 +84,7 @@ class AnthropicFilesHandler:# Construct the Anthropic batch results URLencoded_batch_id: Final = encode_url_path_segment(batch_id, field_name="batch_id")- results_url: Final = f"{api_base.rstrip('/')}/v1/messages/batches/{encoded_batch_id}/results"+ results_url: Final = join_api_base(api_base, f"v1/messages/batches/{encoded_batch_id}/results")# Prepare headersheaders: Final = {diff --git a/litellm/llms/anthropic/files/transformation.py b/litellm/llms/anthropic/files/transformation.pyindex 8433235dde4..db14d3366ff 100644--- a/litellm/llms/anthropic/files/transformation.py+++ b/litellm/llms/anthropic/files/transformation.py@@ -22,7 +22,7 @@ from openai.types.file_deleted import FileDeletedfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.prompt_templates.common_utils import extract_file_data-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.files.transformation import (BaseFilesConfig,@@ -80,7 +80,7 @@ class AnthropicFilesConfig(BaseFilesConfig):stream: bool | None = None,) -> str:api_base = AnthropicModelInfo.get_api_base(api_base) or ANTHROPIC_FILES_API_BASE- return f"{api_base.rstrip('/')}/v1/files"+ return join_api_base(api_base, "v1/files")def get_error_class(self,@@ -226,7 +226,7 @@ class AnthropicFilesConfig(BaseFilesConfig):) -> tuple[str, dict]:api_base: Final = AnthropicModelInfo.get_api_base(litellm_params.get("api_base")) or ANTHROPIC_FILES_API_BASEencoded_file_id: Final = encode_url_path_segment(file_id, field_name="file_id")- return f"{api_base.rstrip('/')}/v1/files/{encoded_file_id}", {}+ return join_api_base(api_base, f"v1/files/{encoded_file_id}"), {}def transform_retrieve_file_response(self,@@ -245,7 +245,7 @@ class AnthropicFilesConfig(BaseFilesConfig):) -> tuple[str, dict]:api_base: Final = AnthropicModelInfo.get_api_base(litellm_params.get("api_base")) or ANTHROPIC_FILES_API_BASEencoded_file_id: Final = encode_url_path_segment(file_id, field_name="file_id")- return f"{api_base.rstrip('/')}/v1/files/{encoded_file_id}", {}+ return join_api_base(api_base, f"v1/files/{encoded_file_id}"), {}def transform_delete_file_response(self,@@ -264,7 +264,7 @@ class AnthropicFilesConfig(BaseFilesConfig):litellm_params: dict,) -> tuple[str, dict]:api_base: Final = AnthropicModelInfo.get_api_base(litellm_params.get("api_base")) or ANTHROPIC_FILES_API_BASE- url: Final = f"{api_base.rstrip('/')}/v1/files"+ url: Final = join_api_base(api_base, "v1/files")params: Final[dict[str, str]] = {}if purpose:params["purpose"] = purpose@@ -299,8 +299,8 @@ class AnthropicFilesConfig(BaseFilesConfig):api_base: Final = AnthropicModelInfo.get_api_base(litellm_params.get("api_base")) or ANTHROPIC_FILES_API_BASEencoded_file_id: Final = encode_url_path_segment(file_id, field_name="file_id")if file_id.startswith(ANTHROPIC_MESSAGE_BATCH_ID_PREFIX):- return f"{api_base.rstrip('/')}/v1/messages/batches/{encoded_file_id}/results", {}- return f"{api_base.rstrip('/')}/v1/files/{encoded_file_id}/content", {}+ return join_api_base(api_base, f"v1/messages/batches/{encoded_file_id}/results"), {}+ return join_api_base(api_base, f"v1/files/{encoded_file_id}/content"), {}def transform_file_content_response(self,diff --git a/litellm/llms/anthropic/pass_through/messages/transformation.py b/litellm/llms/anthropic/pass_through/messages/transformation.pyindex a71cd72b768..9f3aa0130d1 100644--- a/litellm/llms/anthropic/pass_through/messages/transformation.py+++ b/litellm/llms/anthropic/pass_through/messages/transformation.py@@ -9,6 +9,7 @@ from litellm.anthropic_beta_headers_manager import get_unsupported_headers, is_bfrom litellm.exceptions import AuthenticationErrorfrom litellm.litellm_core_utils.core_helpers import drops_unsupported_paramsfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.anthropic.pass_through.convertible_blocks import drop_non_string_tool_result_textsfrom litellm.llms.anthropic.pass_through.safeguards import proxy_serves_safeguardsfrom litellm.llms.base_llm.anthropic_messages.transformation import (@@ -287,10 +288,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig):litellm_params: dict,stream: bool | None = None,) -> str:- api_base = AnthropicModelInfo.get_api_base(api_base) or "https://api.anthropic.com"- if not api_base.endswith("/v1/messages"):- api_base = f"{api_base}/v1/messages"- return api_base+ return ensure_api_route(AnthropicModelInfo.get_api_base(api_base) or "https://api.anthropic.com", "v1/messages")def validate_anthropic_messages_environment(self,diff --git a/litellm/llms/anthropic/skills/transformation.py b/litellm/llms/anthropic/skills/transformation.pyindex 6d419a56889..8ea9d192c24 100644--- a/litellm/llms/anthropic/skills/transformation.py+++ b/litellm/llms/anthropic/skills/transformation.py@@ -9,7 +9,7 @@ import httpxfrom pydantic import TypeAdapterfrom litellm._logging import verbose_logger-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.skills.transformation import (BaseSkillsAPIConfig,LiteLLMLoggingObj,@@ -75,13 +75,11 @@ class AnthropicSkillsConfig(BaseSkillsAPIConfig):"""Get complete URL for Anthropic Skills API"""from litellm.llms.anthropic.common_utils import AnthropicModelInfo- if api_base is None:- api_base = AnthropicModelInfo.get_api_base()-+ resolved_api_base: Final = AnthropicModelInfo.get_api_base(api_base) or "https://api.anthropic.com"if skill_id:encoded_skill_id: Final = encode_url_path_segment(skill_id, field_name="skill_id")- return f"{api_base}/v1/skills/{encoded_skill_id}"- return f"{api_base}/v1/{endpoint}"+ return join_api_base(resolved_api_base, f"v1/skills/{encoded_skill_id}")+ return join_api_base(resolved_api_base, f"v1/{endpoint}")def transform_create_skill_request(self,diff --git a/litellm/llms/anthropic/wif.py b/litellm/llms/anthropic/wif.pyindex b47c4f28eb2..ccb81c1244f 100644--- a/litellm/llms/anthropic/wif.py+++ b/litellm/llms/anthropic/wif.py@@ -15,6 +15,7 @@ from typing_extensions import assert_neverimport litellmfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.auth.client_credentials import keycloak_assertion_sourcefrom litellm.llms.base_llm.auth.identity_source import (AnthropicIdentitySourceKind,@@ -324,7 +325,7 @@ def anthropic_internal_issuer_jwks(credential_values: Mapping[str, object]) -> Adef build_anthropic_wif_spec(params: AnthropicWifParams, api_base: str) -> TokenExchangeSpec:return TokenExchangeSpec(- token_url=api_base.rstrip("/") + ANTHROPIC_TOKEN_EXCHANGE_PATH,+ token_url=join_api_base(api_base, ANTHROPIC_TOKEN_EXCHANGE_PATH),assertion_ref=params.assertion_ref,assertion_field="assertion",static_body=MappingProxyType(diff --git a/litellm/llms/apiserpent/search/transformation.py b/litellm/llms/apiserpent/search/transformation.pyindex bb8abff7272..faf1903ffe8 100644--- a/litellm/llms/apiserpent/search/transformation.py+++ b/litellm/llms/apiserpent/search/transformation.py@@ -14,6 +14,7 @@ from urllib.parse import urlencodeimport httpxfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.apiserpent.search.defaults import (DEEP_SEARCH_PATH,NUM_MAX,@@ -77,13 +78,11 @@ class APISerpentSearchConfig(BaseSearchConfig):Build the search URL. APISerpent uses GET, so the transformed request isserialized into the query string. The endpoint path (quick vs deep) isalways applied; an ``api_base`` / ``APISERPENT_API_BASE`` override only- changes the host. The ``endswith`` guard keeps this idempotent, since the+ changes the host. ``ensure_api_route`` keeps this idempotent, since thehandler re-invokes this method with the already-resolved URL as api_base."""- base = (api_base or get_secret_str("APISERPENT_API_BASE") or APISERPENT_BASE).rstrip("/")path: Final = DEEP_SEARCH_PATH if self._is_deep_search(optional_params) else QUICK_SEARCH_PATH- if not base.endswith(path):- base = f"{base}{path}"+ base: Final = ensure_api_route(api_base or get_secret_str("APISERPENT_API_BASE") or APISERPENT_BASE, path)if data and isinstance(data, dict) and APISERPENT_PARAMS_KEY in data:query_string: Final = urlencode(data[APISERPENT_PARAMS_KEY], doseq=True)diff --git a/litellm/llms/azure/audio_transcription/transformation.py b/litellm/llms/azure/audio_transcription/transformation.pyindex 858f122f65a..a5fde7f7158 100644--- a/litellm/llms/azure/audio_transcription/transformation.py+++ b/litellm/llms/azure/audio_transcription/transformation.py@@ -13,6 +13,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -110,7 +111,7 @@ class AzureSpeechAudioTranscriptionConfig(BaseAudioTranscriptionConfig):"language": optional_params.get("language", self.DEFAULT_LANGUAGE),"format": self._get_azure_response_format(optional_params.get("response_format")),}- return f"{base_url}{self.STT_ENDPOINT_PATH}?{urlencode(query_params)}"+ return f"{join_api_base(base_url, self.STT_ENDPOINT_PATH)}?{urlencode(query_params)}"def transform_audio_transcription_request(self,@@ -153,7 +154,6 @@ class AzureSpeechAudioTranscriptionConfig(BaseAudioTranscriptionConfig):)def _resolve_stt_base_url(self, api_base: str) -> str:- api_base = api_base.rstrip("/")parsed_url: Final = urlparse(api_base)hostname: Final = parsed_url.hostname or ""diff --git a/litellm/llms/azure/common_utils.py b/litellm/llms/azure/common_utils.pyindex 52e3cdf380e..23c5aa35f4b 100644--- a/litellm/llms/azure/common_utils.py+++ b/litellm/llms/azure/common_utils.py@@ -18,6 +18,7 @@ import litellmfrom litellm._logging import verbose_loggerfrom litellm.caching.caching import DualCachefrom litellm.constants import DEFAULT_MAX_RETRIES+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.custom_httpx.http_handler import HTTPHandlerfrom litellm.llms.openai.common_utils import BaseOpenAILLM, OpenAIAsyncHTTPClient, OpenAIHTTPClient@@ -616,7 +617,7 @@ class BaseAzureLLM(BaseOpenAILLM):v1_params: Final[_AzureV1ClientParams] = {"api_key": v1_api_key,- "base_url": f"{api_base}/openai/v1/",+ "base_url": f"{ensure_api_route(api_base or '', 'openai/v1')}/",}if "timeout" in azure_client_params:v1_params["timeout"] = azure_client_params["timeout"]diff --git a/litellm/llms/azure/realtime/handler.py b/litellm/llms/azure/realtime/handler.pyindex 75f96bfdf63..3d4576fd7ac 100644--- a/litellm/llms/azure/realtime/handler.py+++ b/litellm/llms/azure/realtime/handler.py@@ -10,6 +10,7 @@ from typing import Final, Protocol, castfrom litellm._logging import verbose_proxy_loggerfrom litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.types.realtime import RealtimeQueryParamsfrom ....litellm_core_utils.litellm_logging import Logging as LiteLLMLogging@@ -136,7 +137,8 @@ class AzureOpenAIRealtime(AzureChatCompletion):query_parts.append(urlencode({"intent": intent}))qs: Final = "&".join(query_parts)- return f"{api_base}{path}?{qs}" if qs else f"{api_base}{path}"+ endpoint_url: Final = join_api_base(api_base, path)+ return f"{endpoint_url}?{qs}" if qs else endpoint_urldef construct_url(self,diff --git a/litellm/llms/azure/realtime/http_transformation.py b/litellm/llms/azure/realtime/http_transformation.pyindex 542a6a8c02f..545c3348182 100644--- a/litellm/llms/azure/realtime/http_transformation.py+++ b/litellm/llms/azure/realtime/http_transformation.py@@ -3,6 +3,7 @@from typing import Finalimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.realtime.http_transformation import BaseRealtimeHTTPConfigfrom litellm.secret_managers.main import get_secret_str@@ -39,9 +40,9 @@ class AzureRealtimeHTTPConfig(BaseRealtimeHTTPConfig):return _azure_realtime_v1_url(self.get_api_base(api_base), "calls")def get_transcription_session_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:- base: Final = self.get_api_base(api_base).rstrip("/")+ base: Final = self.get_api_base(api_base)version: Final = api_version or get_secret_str("AZURE_API_VERSION") or "2024-12-17"- return f"{base}/openai/realtime/transcription_sessions?api-version={version}"+ return f"{join_api_base(base, 'openai/realtime/transcription_sessions')}?api-version={version}"def get_realtime_calls_headers(self, ephemeral_key: str) -> dict[str, str]:return {diff --git a/litellm/llms/azure/search/transformation.py b/litellm/llms/azure/search/transformation.pyindex f2bb2ecc68c..f7cdf88ade0 100644--- a/litellm/llms/azure/search/transformation.py+++ b/litellm/llms/azure/search/transformation.py@@ -35,6 +35,7 @@ from typing import TYPE_CHECKING, Final, Literalimport httpxfrom pydantic import ConfigDict, ValidationError+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.azure.common_utils import cached_azure_ad_token_refresh_providerfrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.search.transformation import (@@ -340,10 +341,7 @@ class BingGroundingSearchConfig(BaseSearchConfig):f"{PROJECT_ENDPOINT_ENV} is not set. Set it to your Microsoft Foundry project "f"endpoint, e.g. https://<account>.services.ai.azure.com/api/projects/<project>.")- trimmed: Final = resolved_base.rstrip("/")- if trimmed.endswith(_RESPONSES_PATH):- return trimmed- return f"{trimmed}{_RESPONSES_PATH}"+ return ensure_api_route(resolved_base, _RESPONSES_PATH)def transform_search_request(self,diff --git a/litellm/llms/azure/text_to_speech/transformation.py b/litellm/llms/azure/text_to_speech/transformation.pyindex eed7a3178ca..acae650ed75 100644--- a/litellm/llms/azure/text_to_speech/transformation.py+++ b/litellm/llms/azure/text_to_speech/transformation.py@@ -11,6 +11,7 @@ from urllib.parse import urlparseimport httpximport litellm+from litellm.litellm_core_utils.url_utils import ensure_api_route, join_api_basefrom litellm.llms.base_llm.text_to_speech.transformation import (BaseTextToSpeechConfig,TextToSpeechRequestData,@@ -329,8 +330,6 @@ class AzureAVATextToSpeechConfig(BaseTextToSpeechConfig):f"https://{{region}}.{self.TTS_SPEECH_DOMAIN}")- # Remove trailing slash and parse URL- api_base = api_base.rstrip("/")parsed_url: Final = urlparse(api_base)hostname: Final = parsed_url.hostname or ""@@ -339,14 +338,10 @@ class AzureAVATextToSpeechConfig(BaseTextToSpeechConfig):region: Final = self._extract_region_from_hostname(hostname=hostname, domain=self.COGNITIVE_SERVICES_DOMAIN)return self._build_tts_url(region=region)- # Check if it's already a TTS endpointif self._is_tts_endpoint(hostname=hostname):- if not api_base.endswith(self.TTS_ENDPOINT_PATH):- return f"{api_base}{self.TTS_ENDPOINT_PATH}"- return api_base+ return ensure_api_route(api_base, self.TTS_ENDPOINT_PATH)- # Assume it's a custom endpoint, append the path- return f"{api_base}{self.TTS_ENDPOINT_PATH}"+ return join_api_base(api_base, self.TTS_ENDPOINT_PATH)def _is_cognitive_services_endpoint(self, hostname: str) -> bool:"""Check if hostname is a Cognitive Services endpoint"""diff --git a/litellm/llms/azure_ai/agents/handler.py b/litellm/llms/azure_ai/agents/handler.pyindex 0b0548dd3a2..94f1399f01b 100644--- a/litellm/llms/azure_ai/agents/handler.py+++ b/litellm/llms/azure_ai/agents/handler.py@@ -30,7 +30,7 @@ from typing_extensions import ReadOnlyfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.hidden_params import set_hidden_param-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.azure_ai.agents.transformation import (AzureAIAgentsConfig,AzureAIAgentsError,@@ -130,28 +130,29 @@ class AzureAIAgentsHandler:# See: https://learn.microsoft.com/en-us/azure/ai-foundry/agents/quickstart# -------------------------------------------------------------------------def _build_thread_url(self, api_base: str, api_version: str) -> str:- return f"{api_base}/threads?api-version={api_version}"+ return f"{join_api_base(api_base, 'threads')}?api-version={api_version}"def _build_messages_url(self, api_base: str, thread_id: str, api_version: str) -> str:encoded_thread_id: Final = encode_url_path_segment(thread_id, field_name="thread_id")- return f"{api_base}/threads/{encoded_thread_id}/messages?api-version={api_version}"+ return f"{join_api_base(api_base, f'threads/{encoded_thread_id}/messages')}?api-version={api_version}"def _build_runs_url(self, api_base: str, thread_id: str, api_version: str) -> str:encoded_thread_id: Final = encode_url_path_segment(thread_id, field_name="thread_id")- return f"{api_base}/threads/{encoded_thread_id}/runs?api-version={api_version}"+ return f"{join_api_base(api_base, f'threads/{encoded_thread_id}/runs')}?api-version={api_version}"def _build_run_status_url(self, api_base: str, thread_id: str, run_id: str, api_version: str) -> str:encoded_thread_id: Final = encode_url_path_segment(thread_id, field_name="thread_id")encoded_run_id: Final = encode_url_path_segment(run_id, field_name="run_id")- return f"{api_base}/threads/{encoded_thread_id}/runs/{encoded_run_id}?api-version={api_version}"+ run_status_route: Final = f"threads/{encoded_thread_id}/runs/{encoded_run_id}"+ return f"{join_api_base(api_base, run_status_route)}?api-version={api_version}"def _build_list_messages_url(self, api_base: str, thread_id: str, api_version: str) -> str:encoded_thread_id: Final = encode_url_path_segment(thread_id, field_name="thread_id")- return f"{api_base}/threads/{encoded_thread_id}/messages?api-version={api_version}"+ return f"{join_api_base(api_base, f'threads/{encoded_thread_id}/messages')}?api-version={api_version}"def _build_create_thread_and_run_url(self, api_base: str, api_version: str) -> str:"""URL for the create-thread-and-run endpoint (supports streaming)."""- return f"{api_base}/threads/runs?api-version={api_version}"+ return f"{join_api_base(api_base, 'threads/runs')}?api-version={api_version}"# -------------------------------------------------------------------------# Response Helpers@@ -281,8 +282,6 @@ class AzureAIAgentsHandler:api_version: Final = optional_params.get("api_version", self.config.DEFAULT_API_VERSION)agent_id: Final = self.config.get_agent_id(model, optional_params)thread_id: Final = optional_params.get("thread_id")- api_base = api_base.rstrip("/")-verbose_logger.debug("Azure AI Agents completion - api_base: %s, agent_id: %s", api_base, agent_id)return headers, api_version, agent_id, thread_id, api_basediff --git a/litellm/llms/azure_ai/anthropic/count_tokens/transformation.py b/litellm/llms/azure_ai/anthropic/count_tokens/transformation.pyindex 5946a1f4480..6e3cfce5eb4 100644--- a/litellm/llms/azure_ai/anthropic/count_tokens/transformation.py+++ b/litellm/llms/azure_ai/anthropic/count_tokens/transformation.py@@ -5,8 +5,10 @@ Extends the base Anthropic CountTokens transformation with Azure authentication."""from typing import Final+from urllib.parse import urlsplitfrom litellm.constants import ANTHROPIC_TOKEN_COUNTING_BETA_VERSION+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.count_tokens.transformation import (AnthropicCountTokensConfig,)@@ -76,14 +78,8 @@ class AzureAIAnthropicCountTokensConfig(AnthropicCountTokensConfig):Returns:The endpoint URL for the CountTokens API"""- # Azure AI Anthropic endpoint format:- # https://<resource>.services.ai.azure.com/anthropic/v1/messages/count_tokens- api_base = api_base.rstrip("/")-- # Ensure the URL has /anthropic path- if not api_base.endswith("/anthropic"):- if "/anthropic" not in api_base:- api_base = f"{api_base}/anthropic"-- # Add the count_tokens path- return f"{api_base}/v1/messages/count_tokens"+ base: Final = api_base.rstrip("/")+ if base.endswith("/v1/messages"):+ return join_api_base(base, "count_tokens")+ anthropic_base: Final = base if "/anthropic" in urlsplit(base).path else join_api_base(base, "anthropic")+ return join_api_base(anthropic_base, "v1/messages/count_tokens")diff --git a/litellm/llms/azure_ai/anthropic/messages_transformation.py b/litellm/llms/azure_ai/anthropic/messages_transformation.pyindex 91c75661688..2aacc5771d0 100644--- a/litellm/llms/azure_ai/anthropic/messages_transformation.py+++ b/litellm/llms/azure_ai/anthropic/messages_transformation.py@@ -9,6 +9,7 @@ from litellm.llms.anthropic.pass_through.messages.transformation import (AnthropicMessagesConfig,)from litellm.llms.azure.common_utils import BaseAzureLLM+from litellm.llms.azure_ai.anthropic.transformation import azure_anthropic_messages_urlfrom litellm.types.router import GenericLiteLLMParams@@ -96,25 +97,7 @@ class AzureAnthropicMessagesConfig(AnthropicMessagesConfig):"Expected format: https://<resource-name>.services.ai.azure.com/anthropic")- # Ensure the URL ends with /v1/messages- api_base = api_base.rstrip("/")- if api_base.endswith("/v1/messages") or api_base.endswith("/anthropic/v1/messages"):- # Already correct- pass- else:- # Check if /anthropic is already in the path- if "/anthropic" in api_base:- # /anthropic exists, ensure we end with /anthropic/v1/messages- # Extract the base URL up to and including /anthropic- parts: Final = api_base.split("/anthropic", 1)- api_base = parts[0] + "/anthropic"- else:- # /anthropic not in path, add it- api_base = api_base + "/anthropic"- # Add /v1/messages- api_base = api_base + "/v1/messages"-- return api_base+ return azure_anthropic_messages_url(api_base)def _remove_scope_from_cache_control(self, anthropic_messages_request: dict) -> None:"""diff --git a/litellm/llms/azure_ai/anthropic/transformation.py b/litellm/llms/azure_ai/anthropic/transformation.pyindex 18a4eeb7c0a..c75213c3e2c 100644--- a/litellm/llms/azure_ai/anthropic/transformation.py+++ b/litellm/llms/azure_ai/anthropic/transformation.py@@ -4,12 +4,23 @@ Azure Anthropic transformation config - extends AnthropicConfig with Azure authefrom typing import Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.chat.transformation import AnthropicConfigfrom litellm.llms.azure.common_utils import BaseAzureLLMfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.router import GenericLiteLLMParams+def azure_anthropic_messages_url(api_base: str) -> str:+ base: Final = api_base.rstrip("/")+ if base.endswith("/v1/messages"):+ return base+ path_start: Final = base.find("/", base.find("://") + len("://")) if "://" in base else 0+ anthropic_start: Final = base.find("/anthropic", path_start) if path_start >= 0 else -1+ anthropic_root: Final = base[:anthropic_start] if anthropic_start >= 0 else base+ return join_api_base(anthropic_root, "anthropic/v1/messages")++def _promote_extra_body_to_optional_params(optional_params: dict) -> None:"""Promote anthropic-native passthrough keys out of ``extra_body``.diff --git a/litellm/llms/azure_ai/embed/handler.py b/litellm/llms/azure_ai/embed/handler.pyindex 57e6ca51ade..dc3bc6bdc5e 100644--- a/litellm/llms/azure_ai/embed/handler.py+++ b/litellm/llms/azure_ai/embed/handler.py@@ -5,6 +5,7 @@ from urllib.parse import urlsplit, urlunsplitfrom openai import OpenAIimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.azure_ai.common_utils import is_foundry_model_inference_basefrom litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,@@ -74,13 +75,18 @@ class AzureAIEmbedding(OpenAIChatCompletion):api_base: str | None,client: HTTPHandler | AsyncHTTPHandler | None = None,) -> EmbeddingResponse:+ if api_base is None:+ raise ValueError(+ "api_base is None. Please set AZURE_AI_API_BASE or dynamically via `api_base` param, "+ "to make the request."+ )if client is None or not isinstance(client, AsyncHTTPHandler):client = get_async_httpx_client(llm_provider=litellm.LlmProviders.AZURE_AI,params={"timeout": timeout},)- url: Final = f"{api_base}/images/embeddings"+ url: Final = join_api_base(api_base, "images/embeddings")response: Final = await client.post(url=url,@@ -123,7 +129,7 @@ class AzureAIEmbedding(OpenAIChatCompletion):if client is None or not isinstance(client, HTTPHandler):client = HTTPHandler(timeout=timeout, concurrent_limit=1)- url: Final = f"{api_base}/images/embeddings"+ url: Final = join_api_base(api_base, "images/embeddings")response: Final = client.post(url=url,diff --git a/litellm/llms/azure_ai/image_generation/flux_transformation.py b/litellm/llms/azure_ai/image_generation/flux_transformation.pyindex 8b4fa78cdf4..6a7bebd9f6b 100644--- a/litellm/llms/azure_ai/image_generation/flux_transformation.py+++ b/litellm/llms/azure_ai/image_generation/flux_transformation.py@@ -3,6 +3,7 @@ from types import MappingProxyTypefrom typing import Finalfrom litellm.exceptions import BadRequestError, UnsupportedParamsError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.image_generation import GPTImageGenerationConfigfrom litellm.types.llms.openai import OpenAIImageGenerationOptionalParams@@ -52,7 +53,8 @@ class AzureFoundryFluxImageGenerationConfig(GPTImageGenerationConfig):return f"{api_base}?api-version={api_version}"provider_model_path: Final = AzureFoundryFluxImageGenerationConfig.get_flux2_provider_model_path(model)- return f"{api_base}/providers/blackforestlabs/v1/{provider_model_path}?api-version={api_version}"+ flux_route: Final = f"providers/blackforestlabs/v1/{provider_model_path}"+ return f"{join_api_base(api_base, flux_route)}?api-version={api_version}"@staticmethoddef is_flux2_model(model: str) -> bool:diff --git a/litellm/llms/azure_ai/image_generation/mai_transformation.py b/litellm/llms/azure_ai/image_generation/mai_transformation.pyindex 18b4b6f456a..8bb5d3e6b8d 100644--- a/litellm/llms/azure_ai/image_generation/mai_transformation.py+++ b/litellm/llms/azure_ai/image_generation/mai_transformation.py@@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom litellm.exceptions import UnsupportedParamsError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -41,9 +42,9 @@ class AzureFoundryMAIImageGenerationConfig(BaseImageGenerationConfig):if "/mai/" in path:prefix, _, _ = path.partition("/images/")- path = f"{prefix}/images/generations"+ path = join_api_base(prefix, "images/generations")else:- path = f"{path}/mai/v1/images/generations"+ path = join_api_base(path, "mai/v1/images/generations")if separator:return f"{path}?{query}"@@ -63,9 +64,9 @@ class AzureFoundryMAIImageGenerationConfig(BaseImageGenerationConfig):if "/mai/" in path:prefix, _, _ = path.partition("/images/")- path = f"{prefix}/images/edits"+ path = join_api_base(prefix, "images/edits")else:- path = f"{path}/mai/v1/images/edits"+ path = join_api_base(path, "mai/v1/images/edits")if separator:return f"{path}?{query}"diff --git a/litellm/llms/azure_ai/vector_stores/transformation.py b/litellm/llms/azure_ai/vector_stores/transformation.pyindex 7638229bc32..a4777be146f 100644--- a/litellm/llms/azure_ai/vector_stores/transformation.py+++ b/litellm/llms/azure_ai/vector_stores/transformation.py@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.azure.common_utils import BaseAzureLLMfrom litellm.llms.base_llm.vector_store.transformation import (BaseQueryEmbeddingVectorStoreConfig,@@ -172,7 +173,8 @@ class AzureAIVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig, BaseAzureLLMlitellm_logging_obj.model_call_details["input"] = query_textlitellm_logging_obj.model_call_details["embedding_model"] = litellm_params.get("litellm_embedding_model")litellm_logging_obj.model_call_details["top_k"] = top_k- return f"{api_base}/indexes/{vector_store_id}/docs/search?api-version=2024-07-01", {+ search_route: Final = f"indexes/{vector_store_id}/docs/search"+ return f"{join_api_base(api_base, search_route)}?api-version=2024-07-01", {"search": "*","vectorQueries": [{"vector": query_vector, "fields": vector_field, "kind": "vector", "k": top_k}],"select": "id,content",diff --git a/litellm/llms/base_llm/decisions/transformation.py b/litellm/llms/base_llm/decisions/transformation.pyindex d74257a4a9d..a122aeb3033 100644--- a/litellm/llms/base_llm/decisions/transformation.py+++ b/litellm/llms/base_llm/decisions/transformation.py@@ -12,6 +12,7 @@ from pydantic_core import ErrorDetailsfrom typing_extensions import assert_neverimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.decisions import (@@ -582,7 +583,7 @@ class BaseDecisionsConfig(ABC):)def get_complete_url(self, api_base: str, model: str) -> str:- return f"{api_base.rstrip('/').removesuffix('/v1')}{self.path}"+ return join_api_base(api_base.rstrip("/").removesuffix("/v1"), self.path)def signs_with_aws(self, api_base: str) -> bool:return Falsediff --git a/litellm/llms/base_llm/evals/transformation.py b/litellm/llms/base_llm/evals/transformation.pyindex 49124e271a6..40093953144 100644--- a/litellm/llms/base_llm/evals/transformation.py+++ b/litellm/llms/base_llm/evals/transformation.py@@ -7,6 +7,7 @@ from typing import TYPE_CHECKING, Anyimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.llms.openai_evals import (CancelEvalResponse,@@ -79,7 +80,7 @@ class BaseEvalsAPIConfig(ABC):"""if api_base is None:raise ValueError("api_base is required")- return f"{api_base}/v1/{endpoint}"+ return join_api_base(api_base, f"v1/{endpoint}")@abstractmethoddef transform_create_eval_request(diff --git a/litellm/llms/base_llm/realtime/http_transformation.py b/litellm/llms/base_llm/realtime/http_transformation.pyindex 43a80edb493..a02054ec8cb 100644--- a/litellm/llms/base_llm/realtime/http_transformation.py+++ b/litellm/llms/base_llm/realtime/http_transformation.py@@ -7,10 +7,11 @@ These are HTTP (not WebSocket) endpoints used by the WebRTC flow:"""from abc import ABC, abstractmethod-from typing import Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_base+class BaseRealtimeHTTPConfig(ABC):"""@@ -61,7 +62,7 @@ class BaseRealtimeHTTPConfig(ABC):"""Return the full URL for POST /realtime/transcription_sessions."""base = (api_base or "").rstrip("/")base = base.removesuffix("/v1")- return f"{base}/v1/realtime/transcription_sessions"+ return join_api_base(base, "v1/realtime/transcription_sessions")@abstractmethoddef validate_environment(@@ -83,8 +84,7 @@ class BaseRealtimeHTTPConfig(ABC):def get_realtime_calls_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:"""Return the full URL for POST /realtime/calls (SDP exchange)."""- base: Final = (api_base or "").rstrip("/")- return f"{base}/v1/realtime/calls"+ return join_api_base(api_base or "", "v1/realtime/calls")def get_realtime_calls_headers(self, ephemeral_key: str) -> dict:"""diff --git a/litellm/llms/base_llm/skills/transformation.py b/litellm/llms/base_llm/skills/transformation.pyindex ba202e3db5d..64a8e055832 100644--- a/litellm/llms/base_llm/skills/transformation.py+++ b/litellm/llms/base_llm/skills/transformation.py@@ -7,6 +7,7 @@ from typing import TYPE_CHECKING, Anyimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.llms.anthropic_skills import (CreateSkillRequest,@@ -71,7 +72,7 @@ class BaseSkillsAPIConfig(ABC):"""if api_base is None:raise ValueError("api_base is required")- return f"{api_base}/v1/{endpoint}"+ return join_api_base(api_base, f"v1/{endpoint}")@abstractmethoddef transform_create_skill_request(diff --git a/litellm/llms/bedrock/chat/agentcore/transformation.py b/litellm/llms/bedrock/chat/agentcore/transformation.pyindex 48b3795e274..bb5bb246a93 100644--- a/litellm/llms/bedrock/chat/agentcore/transformation.py+++ b/litellm/llms/bedrock/chat/agentcore/transformation.py@@ -18,6 +18,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (convert_content_list_to_str,)from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.a2a.common_utils import extract_text_from_a2a_responsefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLM@@ -98,14 +99,14 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):# Build the base endpoint URL for AgentCore# Note: We don't use get_runtime_endpoint as AgentCore has its own endpoint structureif aws_bedrock_runtime_endpoint:- base_url = aws_bedrock_runtime_endpoint+ base_url = f"{aws_bedrock_runtime_endpoint}"else:base_url = f"https://bedrock-agentcore.{region}.{get_aws_dns_suffix(region)}"# Based on boto3 client.invoke_agent_runtime, the path is:# /runtimes/{URL-ENCODED-ARN}/invocations?qualifier=<value>encoded_arn: Final = quote(agent_runtime_arn, safe="")- endpoint_url = f"{base_url}/runtimes/{encoded_arn}/invocations"+ endpoint_url = join_api_base(base_url, f"runtimes/{encoded_arn}/invocations")# Add qualifier as query parameter if providedif "qualifier" in optional_params:diff --git a/litellm/llms/bedrock/chat/chat_completions/transformation.py b/litellm/llms/bedrock/chat/chat_completions/transformation.pyindex 907f8e50ac7..123e4a9e6b4 100644--- a/litellm/llms/bedrock/chat/chat_completions/transformation.py+++ b/litellm/llms/bedrock/chat/chat_completions/transformation.py@@ -33,6 +33,7 @@ from litellm.litellm_core_utils.prompt_templates.image_handling import (inline_remote_image_urls,inline_remote_media,)+from litellm.litellm_core_utils.url_utils import ensure_api_route, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLM, bedrock_bearer_tokenfrom litellm.llms.bedrock.common_utils import (@@ -371,12 +372,9 @@ class AmazonBedrockRuntimeChatCompletionsConfig(OpenAILikeChatConfig):),aws_region_name=aws_region_name,)- base: Final = proxy_endpoint_url.rstrip("/")- if base.endswith("/openai/v1/chat/completions"):- return base- if base.endswith("/openai/v1"):- return f"{base}/chat/completions"- return f"{base}/openai/v1/chat/completions"+ if proxy_endpoint_url.rstrip("/").endswith("/openai/v1"):+ return join_api_base(proxy_endpoint_url, "chat/completions")+ return ensure_api_route(proxy_endpoint_url, "openai/v1/chat/completions")def _params_with_region_from_path(self, optional_params: dict, model: str | Nonediff --git a/litellm/llms/bedrock/chat/converse_handler.py b/litellm/llms/bedrock/chat/converse_handler.pyindex 7d02b9ef230..a6f9192339f 100644--- a/litellm/llms/bedrock/chat/converse_handler.py+++ b/litellm/llms/bedrock/chat/converse_handler.py@@ -10,6 +10,7 @@ from litellm.anthropic_beta_headers_manager import ()from litellm.litellm_core_utils.get_litellm_params import stored_control_optionsfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObject+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm import base_model_iteratorfrom litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,@@ -363,11 +364,11 @@ class BedrockConverseLLM(BaseAWSLLM):aws_region_name=aws_region_name,)if (stream is not None and stream is True) and not fake_stream:- endpoint_url = f"{endpoint_url}/model/{modelId}/converse-stream"- proxy_endpoint_url = f"{proxy_endpoint_url}/model/{modelId}/converse-stream"+ endpoint_url = join_api_base(endpoint_url, f"model/{modelId}/converse-stream")+ proxy_endpoint_url = join_api_base(proxy_endpoint_url, f"model/{modelId}/converse-stream")else:- endpoint_url = f"{endpoint_url}/model/{modelId}/converse"- proxy_endpoint_url = f"{proxy_endpoint_url}/model/{modelId}/converse"+ endpoint_url = join_api_base(endpoint_url, f"model/{modelId}/converse")+ proxy_endpoint_url = join_api_base(proxy_endpoint_url, f"model/{modelId}/converse")## COMPLETION CALLheaders = {"Content-Type": "application/json"}diff --git a/litellm/llms/bedrock/chat/invoke_agent/transformation.py b/litellm/llms/bedrock/chat/invoke_agent/transformation.pyindex 6877af74494..baef89912fa 100644--- a/litellm/llms/bedrock/chat/invoke_agent/transformation.py+++ b/litellm/llms/bedrock/chat/invoke_agent/transformation.py@@ -15,7 +15,7 @@ from litellm._uuid import uuidfrom litellm.litellm_core_utils.prompt_templates.common_utils import (convert_content_list_to_str,)-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLMfrom litellm.llms.bedrock.common_utils import BedrockError@@ -101,9 +101,10 @@ class AmazonInvokeAgentConfig(BaseConfig, BaseAWSLLM):encoded_agent_alias_id: Final = encode_url_path_segment(agent_alias_id, field_name="agent_alias_id")encoded_session_id: Final = encode_url_path_segment(session_id, field_name="session_id")- endpoint_url = f"{endpoint_url}/agents/{encoded_agent_id}/agentAliases/{encoded_agent_alias_id}/sessions/{encoded_session_id}/text"-- return endpoint_url+ return join_api_base(+ endpoint_url,+ f"agents/{encoded_agent_id}/agentAliases/{encoded_agent_alias_id}/sessions/{encoded_session_id}/text",+ )def sign_request(self,diff --git a/litellm/llms/bedrock/chat/invoke_transformations/amazon_openai_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/amazon_openai_transformation.pyindex 4bf1a1cba73..f1e1f3e8738 100644--- a/litellm/llms/bedrock/chat/invoke_transformations/amazon_openai_transformation.py+++ b/litellm/llms/bedrock/chat/invoke_transformations/amazon_openai_transformation.py@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLMfrom litellm.llms.bedrock.common_utils import BedrockErrorfrom litellm.llms.bedrock.request_metadata import (@@ -97,13 +98,8 @@ class AmazonBedrockOpenAIConfig(OpenAIGPTConfig, BaseAWSLLM):# Encode model ID for ARNs (e.g., :imported-model/ -> :imported-model%2F)model_id = CommonUtils.encode_bedrock_runtime_modelid_arn(model_id)- # Build the invoke URL- if stream:- endpoint_url = f"{endpoint_url}/model/{model_id}/invoke-with-response-stream"- else:- endpoint_url = f"{endpoint_url}/model/{model_id}/invoke"-- return endpoint_url+ invoke_route: Final = "invoke-with-response-stream" if stream else "invoke"+ return join_api_base(endpoint_url, f"model/{model_id}/{invoke_route}")def sign_request(self,diff --git a/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.pyindex 87d4fc0b32e..51b5a99272e 100644--- a/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py+++ b/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py@@ -18,6 +18,7 @@ from litellm.litellm_core_utils.prompt_templates.factory import (prompt_factory,render_custom_prompt_template,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.llms.bedrock.chat.invoke_handler import make_call, make_sync_callfrom litellm.llms.bedrock.common_utils import BedrockError@@ -136,20 +137,13 @@ class AmazonInvokeConfig(BaseConfig, BaseAWSLLM):aws_bedrock_runtime_endpoint: Final = optional_params.get("aws_bedrock_runtime_endpoint", None) # https://bedrock-runtime.{region_name}.amazonaws.com- endpoint_url, proxy_endpoint_url = self.get_runtime_endpoint(+ endpoint_url, _ = self.get_runtime_endpoint(api_base=api_base,aws_bedrock_runtime_endpoint=aws_bedrock_runtime_endpoint,aws_region_name=self._get_aws_region_name(optional_params=optional_params, model=model),)-- if (stream is not None and stream is True) and provider != "ai21":- endpoint_url = f"{endpoint_url}/model/{modelId}/invoke-with-response-stream"- proxy_endpoint_url = f"{proxy_endpoint_url}/model/{modelId}/invoke-with-response-stream"- else:- endpoint_url = f"{endpoint_url}/model/{modelId}/invoke"- proxy_endpoint_url = f"{proxy_endpoint_url}/model/{modelId}/invoke"-- return endpoint_url+ invoke_route: Final = "invoke-with-response-stream" if stream is True and provider != "ai21" else "invoke"+ return join_api_base(endpoint_url, f"model/{modelId}/{invoke_route}")def sign_request(self,diff --git a/litellm/llms/bedrock/claude_platform/common_utils.py b/litellm/llms/bedrock/claude_platform/common_utils.pyindex e25e58056f5..77cae177792 100644--- a/litellm/llms/bedrock/claude_platform/common_utils.py+++ b/litellm/llms/bedrock/claude_platform/common_utils.py@@ -5,6 +5,7 @@ import httpximport litellmfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLMfrom litellm.llms.bedrock.common_utils import BedrockErrorfrom litellm.secret_managers.main import get_secret_str@@ -140,9 +141,7 @@ class BedrockClaudePlatformMixin(BaseAWSLLM):if api_base is None:aws_region_name: Final = self._get_required_aws_region_name(optional_params)api_base = f"https://{CLAUDE_PLATFORM_SERVICE_NAME}.{aws_region_name}.api.aws"- if not api_base.endswith("/v1/messages"):- api_base = f"{api_base.rstrip('/')}/v1/messages"- return api_base+ return ensure_api_route(api_base, "v1/messages")def sign_request(self,diff --git a/litellm/llms/bedrock/common_utils.py b/litellm/llms/bedrock/common_utils.pyindex 0cf3e895ad2..4954e646f58 100644--- a/litellm/llms/bedrock/common_utils.py+++ b/litellm/llms/bedrock/common_utils.py@@ -31,6 +31,7 @@ from litellm import verbose_loggerfrom litellm.litellm_core_utils.aws_partition import get_aws_dns_suffixfrom litellm.litellm_core_utils.optional_imports import ensure_optional_importfrom litellm.litellm_core_utils.prompt_templates.common_utils import schema_property_count+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.anthropic_messages.transformation import (BaseAnthropicMessagesConfig,)@@ -1305,10 +1306,7 @@ def build_mantle_messages_url("""override: Final = api_base or aws_bedrock_runtime_endpoint or _mantle_api_base_from_env()if override:- base: Final = override.rstrip("/")- if base.endswith(MANTLE_MESSAGES_PATH):- return base- return f"{base}{MANTLE_MESSAGES_PATH}"+ return ensure_api_route(override, MANTLE_MESSAGES_PATH)return f"https://bedrock-mantle.{region}.api.aws{MANTLE_MESSAGES_PATH}"diff --git a/litellm/llms/bedrock/count_tokens/mantle_handler.py b/litellm/llms/bedrock/count_tokens/mantle_handler.pyindex 14edd39c386..34e622b5e8d 100644--- a/litellm/llms/bedrock/count_tokens/mantle_handler.py+++ b/litellm/llms/bedrock/count_tokens/mantle_handler.py@@ -7,6 +7,7 @@ from typing_extensions import NotRequired, ReadOnly, TypedDictimport litellmfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.count_tokens.transformation import COUNT_TOKEN_OPTION_NAMES, AnthropicCountTokensConfigfrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLM, run_aws_signingfrom litellm.llms.bedrock.common_utils import BedrockError, build_mantle_messages_url@@ -43,7 +44,7 @@ class BedrockMantleCountTokensHandler(BaseAWSLLM):messages_url: Final = build_mantle_messages_url(api_base=None, aws_bedrock_runtime_endpoint=None, region=aws_region_name)- return f"{messages_url}{MANTLE_COUNT_TOKENS_SUFFIX}"+ return join_api_base(messages_url, MANTLE_COUNT_TOKENS_SUFFIX)async def handle_count_tokens_request(self,diff --git a/litellm/llms/bedrock/count_tokens/transformation.py b/litellm/llms/bedrock/count_tokens/transformation.pyindex 7cca4f69acc..a9f95c1f9b9 100644--- a/litellm/llms/bedrock/count_tokens/transformation.py+++ b/litellm/llms/bedrock/count_tokens/transformation.py@@ -11,6 +11,7 @@ from typing import Final, Literalfrom pydantic import JsonValue+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLMfrom litellm.llms.bedrock.common_utils import get_bedrock_base_model@@ -236,9 +237,7 @@ class BedrockCountTokensConfig(BaseAWSLLM):aws_bedrock_runtime_endpoint=aws_bedrock_runtime_endpoint,aws_region_name=aws_region_name,)- endpoint: Final = f"{base_url}/model/{encoded_model_id}/count-tokens"-- return endpoint+ return join_api_base(base_url, f"model/{encoded_model_id}/count-tokens")def transform_bedrock_response_to_anthropic(self, bedrock_response: Mapping[str, JsonValue]diff --git a/litellm/llms/bedrock/embed/embedding.py b/litellm/llms/bedrock/embed/embedding.pyindex 58cfb6cb95e..4d27d94664f 100644--- a/litellm/llms/bedrock/embed/embedding.py+++ b/litellm/llms/bedrock/embed/embedding.py@@ -12,6 +12,7 @@ import httpximport litellmfrom litellm.constants import BEDROCK_EMBEDDING_PROVIDERS_LITERAL+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.cohere.embed.handler import embedding as cohere_embeddingfrom litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,@@ -512,9 +513,9 @@ class BedrockEmbedding(BaseAWSLLM):aws_region_name=aws_region_name,)if has_async_invoke:- endpoint_url = f"{endpoint_url}/async-invoke"+ endpoint_url = join_api_base(endpoint_url, "async-invoke")else:- endpoint_url = f"{endpoint_url}/model/{modelId}/invoke"+ endpoint_url = join_api_base(endpoint_url, f"model/{modelId}/invoke")if batch_data is not None:if aembedding:@@ -611,7 +612,7 @@ class BedrockEmbedding(BaseAWSLLM):# Encode the ARN for use in URL pathencoded_arn: Final = quote(invocation_arn, safe="")- status_url: Final = f"{endpoint_url.rstrip('/')}/async-invoke/{encoded_arn}"+ status_url: Final = join_api_base(endpoint_url, f"async-invoke/{encoded_arn}")# Prepare headers for GET requestheaders: Final = {"Content-Type": "application/json"}diff --git a/litellm/llms/bedrock/image_edit/handler.py b/litellm/llms/bedrock/image_edit/handler.pyindex 53bb332f511..466993de9d1 100644--- a/litellm/llms/bedrock/image_edit/handler.py+++ b/litellm/llms/bedrock/image_edit/handler.py@@ -14,6 +14,7 @@ import httpximport litellmfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.litellm_logging import Logging as LitellmLogging+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.bedrock.image_edit.amazon_nova_canvas_image_edit_transformation import (BedrockAmazonNovaCanvasImageEditConfig,)@@ -226,7 +227,7 @@ class BedrockImageEdit(BaseAWSLLM):aws_bedrock_runtime_endpoint=boto3_credentials_info.aws_bedrock_runtime_endpoint,aws_region_name=boto3_credentials_info.aws_region_name,)- proxy_endpoint_url = f"{proxy_endpoint_url}/model/{modelId}/invoke"+ proxy_endpoint_url = join_api_base(proxy_endpoint_url, f"model/{modelId}/invoke")data: Final = self._get_request_body(model=model,image=image,diff --git a/litellm/llms/bedrock/image_generation/image_handler.py b/litellm/llms/bedrock/image_generation/image_handler.pyindex e81dfe76942..abd6d40f9e9 100644--- a/litellm/llms/bedrock/image_generation/image_handler.py+++ b/litellm/llms/bedrock/image_generation/image_handler.py@@ -8,6 +8,7 @@ import httpximport litellmfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.litellm_logging import Logging as LitellmLogging+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.bedrock.image_generation.amazon_nova_canvas_transformation import (AmazonNovaCanvasConfig,)@@ -248,7 +249,7 @@ class BedrockImageGeneration(BaseAWSLLM):aws_bedrock_runtime_endpoint=boto3_credentials_info.aws_bedrock_runtime_endpoint,aws_region_name=boto3_credentials_info.aws_region_name,)- proxy_endpoint_url = f"{proxy_endpoint_url}/model/{modelId}/invoke"+ proxy_endpoint_url = join_api_base(proxy_endpoint_url, f"model/{modelId}/invoke")data: Final = self._get_request_body(model=model,prompt=prompt,diff --git a/litellm/llms/bedrock/rerank/handler.py b/litellm/llms/bedrock/rerank/handler.pyindex 01ef5af2b67..0634275135b 100644--- a/litellm/llms/bedrock/rerank/handler.py+++ b/litellm/llms/bedrock/rerank/handler.py@@ -6,6 +6,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm.litellm_core_utils.litellm_logging import Logging as LitellmLogging+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,HTTPHandler,@@ -160,7 +161,7 @@ class BedrockRerankHandler(BaseAWSLLM):aws_region_name=boto3_credentials_info.aws_region_name,)proxy_endpoint_url = proxy_endpoint_url.replace("bedrock-runtime", "bedrock-agent-runtime")- proxy_endpoint_url = f"{proxy_endpoint_url}/rerank"+ proxy_endpoint_url = join_api_base(proxy_endpoint_url, "rerank")body: Final = json.dumps(data).encode("utf-8")headers = {"Content-Type": "application/json"}diff --git a/litellm/llms/bedrock/responses/transformation.py b/litellm/llms/bedrock/responses/transformation.pyindex 086d1211835..7cdd7b08f99 100644--- a/litellm/llms/bedrock/responses/transformation.py+++ b/litellm/llms/bedrock/responses/transformation.py@@ -46,6 +46,7 @@ from litellm.litellm_core_utils.prompt_templates.image_handling import (async_convert_url_to_base64,convert_url_to_base64,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.responses.codex_compat import drop_unsupported_tools, normalize_codex_input_itemsfrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLM@@ -223,9 +224,10 @@ class BedrockOpenAIResponsesConfig(BaseAWSLLM, OpenAIResponsesAPIConfig):litellm_params: dict, # mutable-ok: signature fixed by the BaseResponsesAPIConfig override contract) -> str:region: Final = self._get_aws_region_name(optional_params=litellm_params, model=None)+ configured_endpoint: Final = litellm_params.get("aws_bedrock_runtime_endpoint")override: Final = (api_base- or litellm_params.get("aws_bedrock_runtime_endpoint")+ or (f"{configured_endpoint}" if configured_endpoint else None)or get_secret_str("AWS_BEDROCK_RUNTIME_ENDPOINT"))# Partition-aware: bedrock-runtime is amazonaws.com.cn in China, and other@@ -237,7 +239,7 @@ class BedrockOpenAIResponsesConfig(BaseAWSLLM, OpenAIResponsesAPIConfig):(host[: -len(suffix)] for suffix in BEDROCK_RUNTIME_OPENAI_BASE_SUFFIXES if host.endswith(suffix)),host,)- return f"{base}{BEDROCK_RUNTIME_OPENAI_RESPONSES_PATH}"+ return join_api_base(base, BEDROCK_RUNTIME_OPENAI_RESPONSES_PATH)def supports_native_file_search(self) -> bool:return Falsediff --git a/litellm/llms/bedrock/vector_stores/transformation.py b/litellm/llms/bedrock/vector_stores/transformation.pyindex ba8ce7e5625..96d1da19bcc 100644--- a/litellm/llms/bedrock/vector_stores/transformation.py+++ b/litellm/llms/bedrock/vector_stores/transformation.py@@ -6,7 +6,7 @@ from urllib.parse import urlparseimport httpxfrom litellm._logging import verbose_logger-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.vector_store.transformation import BaseVectorStoreConfigfrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLMfrom litellm.llms.bedrock.common_utils import BedrockError@@ -196,7 +196,7 @@ class BedrockVectorStoreConfig(BaseVectorStoreConfig, BaseAWSLLM):aws_region_name=self.get_aws_region_name_for_non_llm_api_calls(aws_region_name=aws_region_name),endpoint_type="agent",)- return f"{endpoint_url}/knowledgebases"+ return join_api_base(endpoint_url, "knowledgebases")def transform_search_vector_store_request(self,@@ -212,7 +212,7 @@ class BedrockVectorStoreConfig(BaseVectorStoreConfig, BaseAWSLLM):query = " ".join(query)encoded_vector_store_id: Final = encode_url_path_segment(vector_store_id, field_name="vector_store_id")- url: Final = f"{api_base}/{encoded_vector_store_id}/retrieve"+ url: Final = join_api_base(api_base, f"{encoded_vector_store_id}/retrieve")request_body: Final[dict[str, object]] = {"retrievalQuery": BedrockKBRetrievalQuery(text=query),diff --git a/litellm/llms/bedrock_mantle/messages/transformation.py b/litellm/llms/bedrock_mantle/messages/transformation.pyindex 78881153e91..aa6510f0491 100644--- a/litellm/llms/bedrock_mantle/messages/transformation.py+++ b/litellm/llms/bedrock_mantle/messages/transformation.py@@ -2,6 +2,7 @@ from collections.abc import Mappingfrom types import MappingProxyTypefrom typing import Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.bedrock.base_aws_llm import BaseAWSLLMfrom litellm.llms.bedrock.common_utils import MANTLE_MESSAGES_PATHfrom litellm.llms.bedrock.messages.mantle_transformation import AmazonMantleMessagesConfig@@ -33,7 +34,7 @@ def build_mantle_native_messages_url(api_base: str | None, litellm_params: Mappiconfigured,)host: Final = f"https://bedrock-mantle.{region}.api.aws" if MANTLE_HOST_RE.match(stripped) else stripped- return f"{host}{MANTLE_MESSAGES_PATH}"+ return join_api_base(host, MANTLE_MESSAGES_PATH)class BedrockMantleAnthropicMessagesConfig(BedrockMantleAuthMixin, AmazonMantleMessagesConfig):diff --git a/litellm/llms/bedrock_mantle/responses/transformation.py b/litellm/llms/bedrock_mantle/responses/transformation.pyindex 8a0ce58c8b2..3532cfbd963 100644--- a/litellm/llms/bedrock_mantle/responses/transformation.py+++ b/litellm/llms/bedrock_mantle/responses/transformation.py@@ -22,6 +22,7 @@ import httpximport litellmfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.responses.codex_compat import drop_unsupported_tools, normalize_codex_input_itemsfrom litellm.llms.base_llm.responses.transformation import ReasoningSummaryLevel@@ -100,7 +101,7 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPIif MANTLE_HOST_RE.match(base):base = f"https://bedrock-mantle.{region}.api.aws"path: Final = "/openai/v1/responses" if self.use_openai_path else "/v1/responses"- return f"{base}{path}"+ return join_api_base(base, path)def validate_environment(self, headers: dict, model: str, litellm_params: GenericLiteLLMParams | None) -> dict:litellm_params = litellm_params or GenericLiteLLMParams()diff --git a/litellm/llms/black_forest_labs/image_edit/transformation.py b/litellm/llms/black_forest_labs/image_edit/transformation.pyindex 66eacc97fba..7cfb0daf334 100644--- a/litellm/llms/black_forest_labs/image_edit/transformation.py+++ b/litellm/llms/black_forest_labs/image_edit/transformation.py@@ -19,7 +19,7 @@ from typing_extensions import NotRequired, ReadOnly, TypedDictimport litellmfrom litellm.constants import DEFAULT_MAX_RECURSE_DEPTH-from litellm.litellm_core_utils.url_utils import async_safe_get, safe_get+from litellm.litellm_core_utils.url_utils import async_safe_get, join_api_base, safe_getfrom litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.images.main import ImageEditOptionalRequestParams@@ -189,11 +189,10 @@ class BlackForestLabsImageEditConfig(BaseImageEditConfig):"""Get the complete URL for the Black Forest Labs API request."""- base_url: str = api_base or get_secret_str("BFL_API_BASE") or DEFAULT_API_BASE- base_url = base_url.rstrip("/")+ base_url: Final = api_base or get_secret_str("BFL_API_BASE") or DEFAULT_API_BASEendpoint: Final = self._get_model_endpoint(model)- return f"{base_url}{endpoint}"+ return join_api_base(base_url, endpoint)def _read_image_bytes(self,diff --git a/litellm/llms/black_forest_labs/image_generation/transformation.py b/litellm/llms/black_forest_labs/image_generation/transformation.pyindex 9d586fba26a..928af314506 100644--- a/litellm/llms/black_forest_labs/image_generation/transformation.py+++ b/litellm/llms/black_forest_labs/image_generation/transformation.py@@ -14,6 +14,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -202,11 +203,10 @@ class BlackForestLabsImageGenerationConfig(BaseImageGenerationConfig):"""Get the complete URL for the Black Forest Labs API request."""- base_url: str = api_base or get_secret_str("BFL_API_BASE") or DEFAULT_API_BASE- base_url = base_url.rstrip("/")+ base_url: Final = api_base or get_secret_str("BFL_API_BASE") or DEFAULT_API_BASEendpoint: Final = self._get_model_endpoint(model)- return f"{base_url}{endpoint}"+ return join_api_base(base_url, endpoint)def transform_image_generation_request(self,diff --git a/litellm/llms/chatgpt/responses/transformation.py b/litellm/llms/chatgpt/responses/transformation.pyindex 3e7db2981ef..34953a79c60 100644--- a/litellm/llms/chatgpt/responses/transformation.py+++ b/litellm/llms/chatgpt/responses/transformation.py@@ -9,6 +9,7 @@ from litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR, set_hidfrom litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (safe_convert_created_field,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.common_utils import OpenAIErrorfrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.responses.sse_output_recovery import (@@ -253,8 +254,7 @@ class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig):litellm_params: dict,) -> str:api_base = api_base or self.authenticator.get_api_base() or CHATGPT_API_BASE- api_base = api_base.rstrip("/")- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")def supports_native_websocket(self) -> bool:"""ChatGPT does not support native WebSocket for Responses API"""diff --git a/litellm/llms/cloudflare/chat/transformation.py b/litellm/llms/cloudflare/chat/transformation.pyindex ef11e51a5eb..0a76588f767 100644--- a/litellm/llms/cloudflare/chat/transformation.py+++ b/litellm/llms/cloudflare/chat/transformation.py@@ -3,6 +3,7 @@ from typing import Finalimport httpxfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfigfrom litellm.secret_managers.main import (@@ -59,7 +60,7 @@ class CloudflareChatConfig(OpenAIGPTConfig):verbose_logger.warning("Cloudflare api_base ending in '/ai/run' is the legacy Workers AI path and no longer serves OpenAI-compatible requests; rewriting to the '/ai/v1' endpoint")- return f"{trimmed[: -len('/ai/run')]}/ai/v1"+ return join_api_base(trimmed[: -len("/ai/run")], "ai/v1")return api_basedef validate_environment(diff --git a/litellm/llms/cloudflare/common_utils.py b/litellm/llms/cloudflare/common_utils.pyindex 9f1293cb9c4..02855a879a2 100644--- a/litellm/llms/cloudflare/common_utils.py+++ b/litellm/llms/cloudflare/common_utils.py@@ -3,6 +3,7 @@ from typing import Finalfrom pydantic import TypeAdapter, ValidationError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.secret_managers.main import (get_secret_str,normalize_nonempty_secret_str,@@ -34,10 +35,10 @@ def workers_ai_request_model(model: str) -> str:def workers_ai_run_url(api_base: str, model: str) -> str:normalized_api_base: Final = api_base.rstrip("/")if normalized_api_base.endswith("/ai/v1"):- return f"{normalized_api_base.removesuffix('/ai/v1')}/ai/run/{model}"+ return join_api_base(normalized_api_base.removesuffix("/ai/v1"), f"ai/run/{model}")if normalized_api_base.endswith("/ai/run"):return f"{normalized_api_base}/{model}"- return f"{normalized_api_base}/ai/run/{model}"+ return join_api_base(normalized_api_base, f"ai/run/{model}")def unwrap_workers_ai_result(payload: object) -> object:diff --git a/litellm/llms/cohere/rerank/transformation.py b/litellm/llms/cohere/rerank/transformation.pyindex b6d94d4853a..8b641c24e9b 100644--- a/litellm/llms/cohere/rerank/transformation.py+++ b/litellm/llms/cohere/rerank/transformation.py@@ -6,6 +6,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.secret_managers.main import get_secret_str@@ -31,11 +32,7 @@ class CohereRerankConfig(BaseRerankConfig):optional_params: dict | None = None,) -> str:if api_base:- # Remove trailing slashes and ensure clean base URL- api_base = api_base.rstrip("/")- if not api_base.endswith("/v1/rerank"):- api_base = f"{api_base}/v1/rerank"- return api_base+ return ensure_api_route(api_base, "v1/rerank")return "https://api.cohere.ai/v1/rerank"def get_supported_cohere_rerank_params(self, model: str) -> list:diff --git a/litellm/llms/cohere/rerank_v2/transformation.py b/litellm/llms/cohere/rerank_v2/transformation.pyindex e79357bf52f..009a220534c 100644--- a/litellm/llms/cohere/rerank_v2/transformation.py+++ b/litellm/llms/cohere/rerank_v2/transformation.py@@ -1,5 +1,6 @@from typing import Any, Final+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.cohere.rerank.transformation import CohereRerankConfigfrom litellm.types.rerank import OptionalRerankParams, RerankRequest@@ -19,11 +20,7 @@ class CohereRerankV2Config(CohereRerankConfig):optional_params: dict | None = None,) -> str:if api_base:- # Remove trailing slashes and ensure clean base URL- api_base = api_base.rstrip("/")- if not api_base.endswith("/v2/rerank"):- api_base = f"{api_base}/v2/rerank"- return api_base+ return ensure_api_route(api_base, "v2/rerank")return "https://api.cohere.ai/v2/rerank"def get_supported_cohere_rerank_params(self, model: str) -> list:diff --git a/litellm/llms/cometapi/chat/transformation.py b/litellm/llms/cometapi/chat/transformation.pyindex 26050700efc..2bf119a0a4a 100644--- a/litellm/llms/cometapi/chat/transformation.py+++ b/litellm/llms/cometapi/chat/transformation.py@@ -10,6 +10,7 @@ from typing import Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.llms.openai import AllMessageValues, ChatCompletionToolParam@@ -112,16 +113,14 @@ class CometAPIConfig(OpenAIGPTConfig):# Ensure we include /v1 prefix when missingif api_base.endswith("/v1"):- return f"{api_base}/{endpoint}"- if api_base.endswith("/v1/"):- return f"{api_base}{endpoint}"+ return join_api_base(api_base, endpoint)# If user provided https://api.cometapi.com, add /v1if api_base == "https://api.cometapi.com":- return f"{api_base}/v1/{endpoint}"+ return join_api_base(api_base, f"v1/{endpoint}")# Generic fallback: if '/v1' not in path, add itif "/v1" not in api_base.split("//", 1)[-1]:- return f"{api_base}/v1/{endpoint}"- return f"{api_base}/{endpoint}"+ return join_api_base(api_base, f"v1/{endpoint}")+ return join_api_base(api_base, endpoint)def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:"""diff --git a/litellm/llms/cometapi/embed/transformation.py b/litellm/llms/cometapi/embed/transformation.pyindex 60f781be261..e3b5c722664 100644--- a/litellm/llms/cometapi/embed/transformation.py+++ b/litellm/llms/cometapi/embed/transformation.py@@ -9,6 +9,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -44,9 +45,7 @@ class CometAPIEmbeddingConfig(BaseEmbeddingConfig):"""Get the complete URL for the CometAPI embedding endpoint."""- api_base = "https://api.cometapi.com/v1" if api_base is None else api_base.rstrip("/")- complete_url: Final = f"{api_base}/embeddings"- return complete_url+ return join_api_base("https://api.cometapi.com/v1" if api_base is None else api_base, "embeddings")def validate_environment(self,diff --git a/litellm/llms/cometapi/image_generation/transformation.py b/litellm/llms/cometapi/image_generation/transformation.pyindex 567dbfd308e..ffaabbb28cd 100644--- a/litellm/llms/cometapi/image_generation/transformation.py+++ b/litellm/llms/cometapi/image_generation/transformation.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -77,16 +78,13 @@ class CometAPIImageGenerationConfig(BaseImageGenerationConfig):"""Get the complete url for the request"""- complete_url: str = (+ base_url: Final = (api_baseor get_secret_str("COMETAPI_BASE_URL")or get_secret_str("COMETAPI_API_BASE")or self.DEFAULT_BASE_URL)-- complete_url = complete_url.rstrip("/")- complete_url = f"{complete_url}/{self.IMAGE_GENERATION_ENDPOINT}"- return complete_url+ return join_api_base(base_url, self.IMAGE_GENERATION_ENDPOINT)def validate_environment(self,diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.pyindex ac2ac1fe31b..a7ca0d5f49e 100644--- a/litellm/llms/custom_httpx/llm_http_handler.py+++ b/litellm/llms/custom_httpx/llm_http_handler.py@@ -59,7 +59,7 @@ from litellm.litellm_core_utils.realtime_errors import (websocket_close_reason,)from litellm.litellm_core_utils.realtime_streaming import RealTimeStreaming-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.anthropic_messages.transformation import (BaseAnthropicMessagesConfig,)@@ -6937,7 +6937,7 @@ class BaseLLMHTTPHandler:headers={}, model=model or "", api_key=api_key)else:- url = f"{api_base.rstrip('/')}/v1/realtime/{endpoint}"+ url = join_api_base(api_base, f"v1/realtime/{endpoint}")headers = {"Authorization": f"Bearer {api_key}","Content-Type": "application/json",@@ -7006,7 +7006,7 @@ class BaseLLMHTTPHandler:url = provider_config.get_realtime_calls_url(api_base=api_base, model=model or "", api_version=api_version)headers: dict[str, object] = provider_config.get_realtime_calls_headers(ephemeral_key=openai_ephemeral_key)else:- url = f"{api_base.rstrip('/')}/v1/realtime/calls"+ url = join_api_base(api_base, "v1/realtime/calls")headers = {"Authorization": f"Bearer {openai_ephemeral_key}",}diff --git a/litellm/llms/dashscope/chat/transformation.py b/litellm/llms/dashscope/chat/transformation.pyindex 9a1b381a2ba..966d1a01b8d 100644--- a/litellm/llms/dashscope/chat/transformation.py+++ b/litellm/llms/dashscope/chat/transformation.py@@ -5,6 +5,7 @@ Translates from OpenAI's `/v1/chat/completions` to DashScope's `/v1/chat/completfrom collections.abc import Coroutinefrom typing import Final, Literal, overload+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues, ChatCompletionToolParam@@ -82,7 +83,4 @@ class DashScopeChatConfig(OpenAIGPTConfig):"""If api_base is not provided, use the default DashScope /chat/completions endpoint."""- resolved_api_base: Final = self._resolve_chat_api_base(api_base).rstrip("/")- if resolved_api_base.endswith("/chat/completions"):- return resolved_api_base- return f"{resolved_api_base}/chat/completions"+ return ensure_api_route(self._resolve_chat_api_base(api_base), "chat/completions")diff --git a/litellm/llms/dashscope/embed/transformation.py b/litellm/llms/dashscope/embed/transformation.pyindex e5a987ca1ad..d0c2b4dff0c 100644--- a/litellm/llms/dashscope/embed/transformation.py+++ b/litellm/llms/dashscope/embed/transformation.py@@ -16,6 +16,7 @@ from typing import Finalimport httpxfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -98,10 +99,7 @@ class DashScopeEmbeddingConfig(BaseEmbeddingConfig):litellm_params: dict,stream: bool | None = None,) -> str:- base: Final = self._resolve_embedding_api_base(api_base).rstrip("/")- if base.endswith("/embeddings"):- return base- return f"{base}/embeddings"+ return ensure_api_route(self._resolve_embedding_api_base(api_base), "embeddings")def transform_embedding_request(self,diff --git a/litellm/llms/dashscope/rerank/transformation.py b/litellm/llms/dashscope/rerank/transformation.pyindex 4ec7d697f02..48ce4b5a07b 100644--- a/litellm/llms/dashscope/rerank/transformation.py+++ b/litellm/llms/dashscope/rerank/transformation.py@@ -34,6 +34,7 @@ from pydantic import ConfigDict, TypeAdapterfrom litellm._uuid import uuidfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.secret_managers.main import get_secret_str@@ -94,10 +95,10 @@ class DashScopeRerankConfig(BaseRerankConfig):return cleanedif cleaned.endswith("/v1"):- return f"{cleaned}/reranks"+ return join_api_base(cleaned, "reranks")# Unknown base: append /reranks rather than silently ignoring the caller's api_base.- return f"{cleaned}/reranks"+ return join_api_base(cleaned, "reranks")def validate_environment(self,diff --git a/litellm/llms/dashscope/videos/transformation.py b/litellm/llms/dashscope/videos/transformation.pyindex 1db91cbb127..5c738423a5a 100644--- a/litellm/llms/dashscope/videos/transformation.py+++ b/litellm/llms/dashscope/videos/transformation.py@@ -16,7 +16,7 @@ from pydantic import BaseModel, Field, StrictBool, StrictInt, StrictStr, TypeAdaimport litellmfrom litellm.exceptions import UnsupportedParamsError-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.videos.async_task_utils import (input_reference_url,@@ -708,7 +708,7 @@ class DashScopeVideoConfig(BaseVideoConfig):if parameters:request_data["parameters"] = parameters- return request_data, (), f"{api_base}{DASHSCOPE_VIDEO_SYNTHESIS_PATH}"+ return request_data, (), join_api_base(api_base, DASHSCOPE_VIDEO_SYNTHESIS_PATH)@staticmethoddef _reference_input(@@ -773,7 +773,7 @@ class DashScopeVideoConfig(BaseVideoConfig):def _task_url(video_id: str, api_base: str) -> str:original_task_id: Final = extract_original_video_id(video_id)encoded_task_id: Final = encode_url_path_segment(original_task_id, field_name="video_id")- return f"{api_base}{DASHSCOPE_TASKS_PATH}/{encoded_task_id}"+ return join_api_base(api_base, f"{DASHSCOPE_TASKS_PATH}/{encoded_task_id}")def transform_video_status_retrieve_response(self,diff --git a/litellm/llms/databricks/chat/transformation.py b/litellm/llms/databricks/chat/transformation.pyindex d46d04578cc..61b41c2a173 100644--- a/litellm/llms/databricks/chat/transformation.py+++ b/litellm/llms/databricks/chat/transformation.py@@ -22,6 +22,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (strip_name_from_message,strip_prefill_prefix_field,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.llms.base_llm.base_utils import (type_to_response_format_param, # pyright: ignore[reportUnknownVariableType] # base_utils helper returns an untyped dict@@ -339,10 +340,7 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig):stream: bool | None = None,) -> str:use_ai_gateway: Final = self.uses_ai_gateway(model)- api_base = self._get_api_base(api_base, use_ai_gateway=use_ai_gateway)- url_base: Final = api_base.rstrip("/") if use_ai_gateway else api_base- complete_url: Final = f"{url_base}/chat/completions"- return complete_url+ return join_api_base(self._get_api_base(api_base, use_ai_gateway=use_ai_gateway), "chat/completions")def get_supported_openai_params(self, model: str | None = None) -> list:return [diff --git a/litellm/llms/databricks/common_utils.py b/litellm/llms/databricks/common_utils.pyindex 3112b2458b4..cd9432ac36f 100644--- a/litellm/llms/databricks/common_utils.py+++ b/litellm/llms/databricks/common_utils.py@@ -20,6 +20,7 @@ from urllib.parse import urlsplit, urlunsplitfrom pydantic import BaseModel, ConfigDictimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_str@@ -278,10 +279,10 @@ class DatabricksBase:if normalized_api_base.endswith("/ai-gateway/mlflow/v1"):return normalized_api_baseif normalized_api_base.endswith("/serving-endpoints"):- return f"{normalized_api_base.removesuffix('/serving-endpoints')}/ai-gateway/mlflow/v1"+ return join_api_base(normalized_api_base.removesuffix("/serving-endpoints"), "ai-gateway/mlflow/v1")api_base_parts: Final = urlsplit(normalized_api_base)if api_base_parts.path in ("", "/"):- return f"{normalized_api_base}/ai-gateway/mlflow/v1"+ return join_api_base(normalized_api_base, "ai-gateway/mlflow/v1")return api_basedef get_oauth_m2m_token(@@ -319,7 +320,7 @@ class DatabricksBase:api_base_parts: Final = urlsplit(api_base)workspace_url: Final = urlunsplit((api_base_parts.scheme, api_base_parts.netloc, "", "", ""))- token_url: Final = f"{workspace_url}/oidc/v1/token"+ token_url: Final = join_api_base(workspace_url, "oidc/v1/token")try:response: Final = requests.post(@@ -477,5 +478,6 @@ class DatabricksBase:# Debug logging with redaction (never log actual tokens)verbose_logger.debug("Databricks request headers: %s", self.redact_headers_for_logging(headers))- endpoint_path: Final = "" if custom_endpoint is True else DATABRICKS_ENDPOINT_PATHS[endpoint_type]- return f"{api_base}{endpoint_path}", headers+ if custom_endpoint is True:+ return api_base, headers+ return join_api_base(api_base, DATABRICKS_ENDPOINT_PATHS[endpoint_type]), headersdiff --git a/litellm/llms/databricks/decisions/transformation.py b/litellm/llms/databricks/decisions/transformation.pyindex c2ac1f220b8..d1698fdf5e2 100644--- a/litellm/llms/databricks/decisions/transformation.py+++ b/litellm/llms/databricks/decisions/transformation.py@@ -6,6 +6,7 @@ from typing import Finalfrom pydantic import TypeAdapter, ValidationError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.decisions.transformation import BaseDecisionsConfigfrom litellm.types.decisions import DecisionsIRRequest, UnsupportedDecisionsRequest@@ -84,8 +85,8 @@ class DatabricksDecisionsConfig(BaseDecisionsConfig):def get_complete_url(self, api_base: str, model: str) -> str:if is_ai_decide_model(model):- return f"{databricks_workspace_host(api_base)}{_AI_DECIDE_PATH}"- return f"{api_base.rstrip('/')}/{validate_databricks_decisions_model(model)}/invocations"+ return join_api_base(databricks_workspace_host(api_base), _AI_DECIDE_PATH)+ return join_api_base(api_base, f"{validate_databricks_decisions_model(model)}/invocations")def transform_decisions_request(self,diff --git a/litellm/llms/databricks/responses/transformation.py b/litellm/llms/databricks/responses/transformation.pyindex 804c79ea79d..55c3edfefd0 100644--- a/litellm/llms/databricks/responses/transformation.py+++ b/litellm/llms/databricks/responses/transformation.py@@ -10,6 +10,7 @@ Reference: https://docs.databricks.com/aws/en/machine-learning/foundation-model-import osfrom typing import TYPE_CHECKING, Any, Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.databricks.common_utils import DatabricksBasefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.types.llms.openai import ResponseInputParam@@ -70,8 +71,7 @@ class DatabricksResponsesAPIConfig(DatabricksBase, OpenAIResponsesAPIConfig):) -> str:api_base = api_base or os.getenv("DATABRICKS_API_BASE")api_base = self._get_api_base(api_base)- api_base = api_base.rstrip("/")- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")def transform_responses_api_request(self,diff --git a/litellm/llms/deepgram/audio_transcription/transformation.py b/litellm/llms/deepgram/audio_transcription/transformation.pyindex a3812dac8ef..a1c3021c9f5 100644--- a/litellm/llms/deepgram/audio_transcription/transformation.py+++ b/litellm/llms/deepgram/audio_transcription/transformation.py@@ -10,6 +10,7 @@ from httpx import Headers, Responsefrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import (@@ -179,7 +180,6 @@ class DeepgramAudioTranscriptionConfig(BaseAudioTranscriptionConfig):) -> str:if api_base is None:api_base = get_secret_str("DEEPGRAM_API_BASE") or "https://api.deepgram.com/v1"- api_base = api_base.rstrip("/") # Remove trailing slash if present# Build query parameters including the modelall_query_params: Final = {"model": model}@@ -189,7 +189,7 @@ class DeepgramAudioTranscriptionConfig(BaseAudioTranscriptionConfig):all_query_params.update(additional_params)# Construct URL with proper query string encoding- base_url: Final = f"{api_base}/listen"+ base_url: Final = join_api_base(api_base, "listen")query_string: Final = urlencode(all_query_params)url: Final = f"{base_url}?{query_string}"diff --git a/litellm/llms/deepgram/common_utils.py b/litellm/llms/deepgram/common_utils.pyindex 9b071ac8321..29998895bbc 100644--- a/litellm/llms/deepgram/common_utils.py+++ b/litellm/llms/deepgram/common_utils.py@@ -8,6 +8,7 @@ import httpximport litellmfrom litellm.constants import DEEPGRAM_DEFAULT_API_BASE, DEEPGRAM_LISTEN_DEFAULT_MODEL+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.utils import LlmProviders@@ -50,7 +51,7 @@ def _first_occurrences(query_string: str) -> httpx.QueryParams:def deepgram_listen_websocket_target(api_base: str | None, query_string: str) -> str:- listen_url: Final = httpx.URL(f"{(api_base or DEEPGRAM_DEFAULT_API_BASE).rstrip('/')}/listen")+ listen_url: Final = httpx.URL(join_api_base(api_base or DEEPGRAM_DEFAULT_API_BASE, "listen"))websocket_url: Final = listen_url.copy_with(scheme=_WEBSOCKET_SCHEMES.get(listen_url.scheme, listen_url.scheme))params: Final = _first_occurrences(query_string)query: Final = params if params.get("model") else params.remove("model").add("model", DEEPGRAM_LISTEN_DEFAULT_MODEL)diff --git a/litellm/llms/deepseek/chat/transformation.py b/litellm/llms/deepseek/chat/transformation.pyindex d3319856d29..a9a20101bb1 100644--- a/litellm/llms/deepseek/chat/transformation.py+++ b/litellm/llms/deepseek/chat/transformation.py@@ -10,6 +10,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (convert_content_list_to_str,extract_search_results_text,)+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.utils import supports_reasoning, supports_vision@@ -363,9 +364,4 @@ class DeepSeekChatConfig(OpenAIGPTConfig):"""If api_base is not provided, use the default DeepSeek /chat/completions endpoint."""- clean_api_base: Final = (api_base or "https://api.deepseek.com/beta").rstrip("/")-- if not clean_api_base.endswith("/chat/completions"):- return f"{clean_api_base}/chat/completions"-- return clean_api_base+ return ensure_api_route(api_base or "https://api.deepseek.com/beta", "chat/completions")diff --git a/litellm/llms/deepseek/messages/transformation.py b/litellm/llms/deepseek/messages/transformation.pyindex 6a6b57131ad..6ddee99aea3 100644--- a/litellm/llms/deepseek/messages/transformation.py+++ b/litellm/llms/deepseek/messages/transformation.py@@ -6,6 +6,7 @@ from collections.abc import Sequencefrom typing import Any, Finalimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.pass_through.messages.transformation import (AnthropicMessagesConfig,)@@ -90,9 +91,9 @@ class DeepSeekAnthropicMessagesConfig(AnthropicMessagesConfig):base_url = base_url.removesuffix("/beta")if not base_url.endswith("/anthropic") and "/anthropic/" not in base_url:- base_url = f"{base_url}/anthropic"+ base_url = join_api_base(base_url, "anthropic")- return f"{base_url}/v1/messages"+ return join_api_base(base_url, "v1/messages")@staticmethoddef _sanitize_tools_for_deepseek(tools: Any) -> object:diff --git a/litellm/llms/docker_model_runner/chat/transformation.py b/litellm/llms/docker_model_runner/chat/transformation.pyindex adb40311efd..68b63812248 100644--- a/litellm/llms/docker_model_runner/chat/transformation.py+++ b/litellm/llms/docker_model_runner/chat/transformation.py@@ -10,6 +10,7 @@ from typing import Final, Literal, overloadfrom litellm.litellm_core_utils.prompt_templates.common_utils import (handle_messages_with_content_list_to_str_conversion,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues@@ -104,12 +105,9 @@ class DockerModelRunnerChatConfig(OpenAIGPTConfig):if not api_base:api_base = "http://localhost:22088/engines/llama.cpp"- # Remove trailing slashes from api_base- api_base = api_base.rstrip("/")-# Build the URL: {api_base}/v1/chat/completions# api_base is expected to already contain the engine path- complete_url: Final = f"{api_base}/v1/chat/completions"+ complete_url: Final = join_api_base(api_base, "v1/chat/completions")return complete_urldiff --git a/litellm/llms/duckduckgo/search/transformation.py b/litellm/llms/duckduckgo/search/transformation.pyindex 1816a0d24e4..36893b4bd27 100644--- a/litellm/llms/duckduckgo/search/transformation.py+++ b/litellm/llms/duckduckgo/search/transformation.py@@ -12,6 +12,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -91,7 +92,7 @@ class DuckDuckGoSearchConfig(BaseSearchConfig):if data and isinstance(data, dict) and "_duckduckgo_params" in data:params: Final = data["_duckduckgo_params"]query_string: Final = urlencode(params, doseq=True)- return f"{api_base}/?{query_string}"+ return f"{join_api_base(api_base, '')}?{query_string}"return api_basediff --git a/litellm/llms/e2b/sandbox/transformation.py b/litellm/llms/e2b/sandbox/transformation.pyindex dbf3996248a..b5f42632eef 100644--- a/litellm/llms/e2b/sandbox/transformation.py+++ b/litellm/llms/e2b/sandbox/transformation.py@@ -15,6 +15,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.sandbox.transformation import (SANDBOX_MAX_OUTPUT_BYTES,BaseSandboxConfig,@@ -78,7 +79,7 @@ class E2BSandboxConfig(BaseSandboxConfig):body["metadata"] = metadataresponse: Final = await self._http(client).post(- url=f"{base}/sandboxes",+ url=join_api_base(base, "sandboxes"),headers={"X-API-Key": key, "Content-Type": "application/json"},json=body,)@@ -151,10 +152,10 @@ class E2BSandboxConfig(BaseSandboxConfig):dict[str, object], getattr(handle, HIDDEN_PARAMS_ATTR))key: Final = api_key or hidden_params.get("api_key") or self.validate_environment()- base: Final = api_base or hidden_params.get("api_base") or E2B_API_BASE+ base: Final = str(api_base or hidden_params.get("api_base") or E2B_API_BASE)try:response: Final = await self._http(client).delete(- url=f"{base}/sandboxes/{handle.id}",+ url=join_api_base(base, f"sandboxes/{handle.id}"),headers={"X-API-Key": key},)except httpx.HTTPStatusError as e:diff --git a/litellm/llms/edenai/chat/transformation.py b/litellm/llms/edenai/chat/transformation.pyindex ae5da5d1cfd..480b382076d 100644--- a/litellm/llms/edenai/chat/transformation.py+++ b/litellm/llms/edenai/chat/transformation.py@@ -23,7 +23,7 @@ from litellm.types.llms.base import LiteLLMBaseModelfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.utils import ModelResponse, ModelResponseStream, Usage-from ..common_utils import EdenAIException, reported_cost, resolve_api_base, resolve_api_key+from ..common_utils import EdenAIException, endpoint_url, reported_cost, resolve_api_base, resolve_api_keyif TYPE_CHECKING:from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj@@ -138,7 +138,7 @@ class EdenAIChatConfig(OpenAIGPTConfig):def get_models(self, api_key: str | None = None, api_base: str | None = None) -> list[str]: # mutable-ok: inherited contract- response: Final = litellm.module_level_client.get(url=f"{self.get_api_base(api_base)}/models")+ response: Final = litellm.module_level_client.get(url=endpoint_url(api_base, "models"))if not response.is_success:raise EdenAIException(status_code=response.status_code, message=response.text, headers=response.headers)catalog: Final = _EdenAIModelCatalog.model_validate(response.json())diff --git a/litellm/llms/edenai/common_utils.py b/litellm/llms/edenai/common_utils.pyindex 3216f798fbe..60c83f7ea75 100644--- a/litellm/llms/edenai/common_utils.py+++ b/litellm/llms/edenai/common_utils.py@@ -11,6 +11,7 @@ from pydantic import AliasChoices, Field, ValidationErrorimport litellmfrom litellm.exceptions import AuthenticationError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.base import LiteLLMBaseModel@@ -74,7 +75,7 @@ def json_headers(def endpoint_url(api_base: str | None, path: str) -> str:- return f"{resolve_api_base(api_base).rstrip('/')}/{path}"+ return join_api_base(resolve_api_base(api_base), path)def pick(params: Mapping[str, object], keys: Container[str]) -> Mapping[str, object]:diff --git a/litellm/llms/elevenlabs/audio_transcription/transformation.py b/litellm/llms/elevenlabs/audio_transcription/transformation.pyindex 04d2717d64f..5a673b5ac41 100644--- a/litellm/llms/elevenlabs/audio_transcription/transformation.py+++ b/litellm/llms/elevenlabs/audio_transcription/transformation.py@@ -10,6 +10,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import (@@ -165,12 +166,7 @@ class ElevenLabsAudioTranscriptionConfig(BaseAudioTranscriptionConfig):) -> str:if api_base is None:api_base = get_secret_str("ELEVENLABS_API_BASE") or "https://api.elevenlabs.io"- api_base = api_base.rstrip("/") # Remove trailing slash if present-- # ElevenLabs speech-to-text endpoint- url: Final = f"{api_base}/v1/speech-to-text"-- return url+ return join_api_base(api_base, "v1/speech-to-text")def validate_environment(self,diff --git a/litellm/llms/elevenlabs/text_to_speech/transformation.py b/litellm/llms/elevenlabs/text_to_speech/transformation.pyindex eb93543df46..f910ef8c057 100644--- a/litellm/llms/elevenlabs/text_to_speech/transformation.py+++ b/litellm/llms/elevenlabs/text_to_speech/transformation.py@@ -11,7 +11,7 @@ import httpxfrom httpx import Headersimport litellm-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.text_to_speech.transformation import (BaseTextToSpeechConfig,@@ -297,15 +297,14 @@ class ElevenLabsTextToSpeechConfig(BaseTextToSpeechConfig):"""Construct the ElevenLabs endpoint URL, including path voice_id and query params."""- base_url = api_base or get_secret_str("ELEVENLABS_API_BASE") or self.TTS_BASE_URL- base_url = base_url.rstrip("/")+ base_url: Final = api_base or get_secret_str("ELEVENLABS_API_BASE") or self.TTS_BASE_URLvoice_id: Final = litellm_params.get(self.ELEVENLABS_VOICE_ID_KEY)if not isinstance(voice_id, str) or not voice_id.strip():raise ValueError("ElevenLabs voice_id is required. Pass `voice` when calling `litellm.speech()`.")encoded_voice_id: Final = encode_url_path_segment(voice_id, field_name="voice_id")- url = f"{base_url}{self.TTS_ENDPOINT_PATH}/{encoded_voice_id}"+ url = join_api_base(base_url, f"{self.TTS_ENDPOINT_PATH}/{encoded_voice_id}")query_params: Final = litellm_params.get(self.ELEVENLABS_QUERY_PARAMS_KEY, {})if query_params:diff --git a/litellm/llms/exa_ai/search/transformation.py b/litellm/llms/exa_ai/search/transformation.pyindex 89073409599..73e341d0033 100644--- a/litellm/llms/exa_ai/search/transformation.py+++ b/litellm/llms/exa_ai/search/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -93,11 +94,7 @@ class ExaAISearchConfig(BaseSearchConfig):"""Get complete URL for Search endpoint."""- resolved_api_base: Final = api_base or get_secret_str("EXA_API_BASE") or self.EXA_AI_API_BASE- clean_api_base: Final = resolved_api_base.rstrip("/")- if clean_api_base.endswith("/search"):- return clean_api_base- return f"{clean_api_base}/search"+ return ensure_api_route(api_base or get_secret_str("EXA_API_BASE") or self.EXA_AI_API_BASE, "search")def transform_search_request(self,diff --git a/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.py b/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.pyindex 6d58caeb384..344d9d3c75b 100644--- a/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.py+++ b/litellm/llms/fal_ai/image_edit/flux_lora_depth_transformation.py@@ -4,6 +4,7 @@ from typing import Finalfrom httpx._types import RequestFiles+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.images.main import ImageEditOptionalRequestParamsfrom litellm.types.router import GenericLiteLLMParams@@ -45,8 +46,8 @@ class FalAIFluxLoraDepthEditConfig(FalAIImageEditConfig):api_base: str | None,litellm_params: dict, # mutable-ok: base class contract) -> str:- base_url: Final = (api_base or get_secret_str("FAL_AI_API_BASE") or DEFAULT_BASE_URL).rstrip("/")- return f"{base_url}/{FLUX_LORA_DEPTH_ENDPOINT}"+ base_url: Final = api_base or get_secret_str("FAL_AI_API_BASE") or DEFAULT_BASE_URL+ return join_api_base(base_url, FLUX_LORA_DEPTH_ENDPOINT)def transform_image_edit_request(self,diff --git a/litellm/llms/fal_ai/image_generation/transformation.py b/litellm/llms/fal_ai/image_generation/transformation.pyindex 0bb5dada071..79913d5c651 100644--- a/litellm/llms/fal_ai/image_generation/transformation.py+++ b/litellm/llms/fal_ai/image_generation/transformation.py@@ -5,6 +5,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom typing_extensions import ReadOnly, TypedDict+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -85,12 +86,10 @@ class FalAIBaseConfig(BaseImageGenerationConfig):Some providers need `model` in `api_base`"""- complete_url: str = api_base or get_secret_str("FAL_AI_API_BASE") or self.DEFAULT_BASE_URL-- complete_url = complete_url.rstrip("/")- if self.IMAGE_GENERATION_ENDPOINT:- complete_url = f"{complete_url}/{self.IMAGE_GENERATION_ENDPOINT}"- return complete_url+ base_url: Final = api_base or get_secret_str("FAL_AI_API_BASE") or self.DEFAULT_BASE_URL+ if not self.IMAGE_GENERATION_ENDPOINT:+ return base_url.rstrip("/")+ return join_api_base(base_url, self.IMAGE_GENERATION_ENDPOINT)def validate_environment(self,diff --git a/litellm/llms/fal_ai/videos/transformation.py b/litellm/llms/fal_ai/videos/transformation.pyindex 3136763e1be..86aa6388ff0 100644--- a/litellm/llms/fal_ai/videos/transformation.py+++ b/litellm/llms/fal_ai/videos/transformation.py@@ -10,7 +10,7 @@ import httpxfrom httpx._types import FileContent, RequestFilesfrom pydantic import TypeAdapter-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.videos.transformation import BaseVideoConfigfrom litellm.llms.custom_httpx.http_handler import (@@ -445,7 +445,7 @@ class FalAIVideoConfig(BaseVideoConfig):request_id, model_id = self._decode_video_id(video_id)encoded_request_id: Final[str] = encode_url_path_segment(request_id, field_name="video_id")return (- f"{api_base.rstrip('/')}/{_queue_request_base_path(model_id)}/requests/{encoded_request_id}/status",+ join_api_base(api_base, f"{_queue_request_base_path(model_id)}/requests/{encoded_request_id}/status"),{},)@@ -541,7 +541,7 @@ class FalAIVideoConfig(BaseVideoConfig):request_id, model_id = self._decode_video_id(video_id)encoded_request_id: Final[str] = encode_url_path_segment(request_id, field_name="video_id")return (- f"{api_base.rstrip('/')}/{_queue_request_base_path(model_id)}/requests/{encoded_request_id}",+ join_api_base(api_base, f"{_queue_request_base_path(model_id)}/requests/{encoded_request_id}"),{},)diff --git a/litellm/llms/fastcrw/search/transformation.py b/litellm/llms/fastcrw/search/transformation.pyindex 8f6f8d87300..27db6a95170 100644--- a/litellm/llms/fastcrw/search/transformation.py+++ b/litellm/llms/fastcrw/search/transformation.py@@ -15,6 +15,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -83,11 +84,7 @@ class FastCRWSearchConfig(BaseSearchConfig):"""Get complete URL for Search endpoint."""- resolved_api_base: Final = api_base or get_secret_str("CRW_API_BASE") or self.FASTCRW_API_BASE- clean_api_base: Final = resolved_api_base.rstrip("/")- if clean_api_base.endswith("/search"):- return clean_api_base- return f"{clean_api_base}/search"+ return ensure_api_route(api_base or get_secret_str("CRW_API_BASE") or self.FASTCRW_API_BASE, "search")def transform_search_request(self,diff --git a/litellm/llms/firecrawl/search/transformation.py b/litellm/llms/firecrawl/search/transformation.pyindex ee99ab24413..6d1e2b22918 100644--- a/litellm/llms/firecrawl/search/transformation.py+++ b/litellm/llms/firecrawl/search/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -85,11 +86,7 @@ class FirecrawlSearchConfig(BaseSearchConfig):"""Get complete URL for Search endpoint."""- resolved_api_base: Final = api_base or get_secret_str("FIRECRAWL_API_BASE") or self.FIRECRAWL_API_BASE- clean_api_base: Final = resolved_api_base.rstrip("/")- if clean_api_base.endswith("/search"):- return clean_api_base- return f"{clean_api_base}/search"+ return ensure_api_route(api_base or get_secret_str("FIRECRAWL_API_BASE") or self.FIRECRAWL_API_BASE, "search")def transform_search_request(self,diff --git a/litellm/llms/fireworks_ai/chat/transformation.py b/litellm/llms/fireworks_ai/chat/transformation.pyindex 14ae59d4f85..fa5794d71b3 100644--- a/litellm/llms/fireworks_ai/chat/transformation.py+++ b/litellm/llms/fireworks_ai/chat/transformation.py@@ -17,6 +17,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (strip_litellm_response_only_fields,unpack_legacy_defs,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import (AllMessageValues,@@ -826,7 +827,7 @@ class FireworksAIConfig(FireworksAIMixin, OpenAIGPTConfig):base = api_base.rstrip("/")base = base.removesuffix("/v1")response: Final = litellm.module_level_client.get(- url=f"{base}/v1/accounts/{account_id}/models",+ url=join_api_base(base, f"v1/accounts/{account_id}/models"),headers={"Authorization": f"Bearer {api_key}"},)diff --git a/litellm/llms/fireworks_ai/rerank/transformation.py b/litellm/llms/fireworks_ai/rerank/transformation.pyindex 4eff64e351f..964f85c821e 100644--- a/litellm/llms/fireworks_ai/rerank/transformation.py+++ b/litellm/llms/fireworks_ai/rerank/transformation.py@@ -12,6 +12,7 @@ from pydantic import ConfigDict, TypeAdapterfrom litellm._uuid import uuidfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.llms.fireworks_ai.common_utils import FireworksAIMixinfrom litellm.types.llms.base import LiteLLMBaseModel@@ -61,9 +62,9 @@ class FireworksAIRerankConfig(FireworksAIMixin, BaseRerankConfig):api_base = api_base.rstrip("/")if not api_base.endswith("/rerank"):if api_base.endswith("/v1") or api_base.endswith("/inference/v1"):- api_base = f"{api_base}/rerank"+ api_base = join_api_base(api_base, "rerank")else:- api_base = f"{api_base}/inference/v1/rerank"+ api_base = join_api_base(api_base, "inference/v1/rerank")return api_basereturn "https://api.fireworks.ai/inference/v1/rerank"diff --git a/litellm/llms/fireworks_ai/responses/transformation.py b/litellm/llms/fireworks_ai/responses/transformation.pyindex 30eaa5a7ca2..db234c09930 100644--- a/litellm/llms/fireworks_ai/responses/transformation.py+++ b/litellm/llms/fireworks_ai/responses/transformation.py@@ -6,6 +6,7 @@ from urllib.parse import unquoteimport httpxfrom openai.types.responses import EasyInputMessageParam, ResponseInputContentParam, ResponseInputItemParam+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.fireworks_ai.common_utils import (FIREWORKS_FORWARD_USER_ID_PARAM,get_fireworks_forwarded_user_id,@@ -150,8 +151,8 @@ class FireworksAIResponsesAPIConfig(OpenAIResponsesAPIConfig):return dict(pinned)def get_complete_url(self, api_base: str | None, litellm_params: Mapping[str, object]) -> str:- base: Final = (api_base or get_secret_str("FIREWORKS_API_BASE") or FIREWORKS_AI_DEFAULT_API_BASE).rstrip("/")- return f"{base}/responses"+ base: Final = api_base or get_secret_str("FIREWORKS_API_BASE") or FIREWORKS_AI_DEFAULT_API_BASE+ return join_api_base(base, "responses")def transform_responses_api_request(self,diff --git a/litellm/llms/gdc/chat/transformation.py b/litellm/llms/gdc/chat/transformation.pyindex 6eac3ac79cd..cd9ffe9e03e 100644--- a/litellm/llms/gdc/chat/transformation.py+++ b/litellm/llms/gdc/chat/transformation.py@@ -13,6 +13,7 @@ from urllib.parse import urlsplitfrom typing_extensions import ReadOnly, TypedDict, Unpackimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai_like.chat.transformation import OpenAILikeChatConfigfrom litellm.types.llms.openai import AllMessageValues@@ -140,7 +141,7 @@ class GDCGeminiConfig(OpenAILikeChatConfig):project = self._validate_path_id(project, "vertex_project", model)location = self._validate_path_id(location, "vertex_location", model)- return f"{api_base}/v1/projects/{project}/locations/{location}/chat/completions"+ return join_api_base(api_base, f"v1/projects/{project}/locations/{location}/chat/completions")def _read_env_bool(self, val: bool | str | None, env_var: str, default: bool = True) -> bool | str:def _parse(s: str) -> bool | str:diff --git a/litellm/llms/gemini/agents/transformation.py b/litellm/llms/gemini/agents/transformation.pyindex cfdb55f2048..c19bd9ce87c 100644--- a/litellm/llms/gemini/agents/transformation.py+++ b/litellm/llms/gemini/agents/transformation.py@@ -15,6 +15,7 @@ from typing import Any, Finalimport httpxfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.agents.transformation import BaseAgentsAPIConfigfrom litellm.llms.gemini.common_utils import GeminiError, GeminiModelInfofrom litellm.types.agents import (@@ -67,7 +68,7 @@ class GeminiAgentsConfig(BaseAgentsAPIConfig):return "v1beta"def _base_url(self, api_base: str | None) -> str:- return f"{GeminiModelInfo.get_api_base(api_base)}/{self.api_version}"+ return join_api_base(GeminiModelInfo.get_api_base(api_base), self.api_version)# ------------------------------------------------------------------ ## Shared helpers #@@ -90,7 +91,7 @@ class GeminiAgentsConfig(BaseAgentsAPIConfig):api_base: str | None,litellm_params: Mapping[str, object],) -> str:- return f"{self._base_url(api_base)}/agents"+ return join_api_base(self._base_url(api_base), "agents")def validate_environment(self,@@ -177,7 +178,7 @@ class GeminiAgentsConfig(BaseAgentsAPIConfig):api_base: str | None,litellm_params: Mapping[str, object],) -> tuple[str, dict[str, object]]:- url: Final = f"{self._base_url(api_base)}/agents"+ url: Final = join_api_base(self._base_url(api_base), "agents")params: Final[dict[str, object]] = {}if litellm_params.get("page_size"):params["pageSize"] = litellm_params["page_size"]@@ -210,7 +211,7 @@ class GeminiAgentsConfig(BaseAgentsAPIConfig):api_base: str | None,litellm_params: Mapping[str, object],) -> tuple[str, dict[str, object]]:- url: Final = f"{self._base_url(api_base)}/agents/{name}"+ url: Final = join_api_base(self._base_url(api_base), f"agents/{name}")return url, {}def transform_get_response(@@ -239,7 +240,7 @@ class GeminiAgentsConfig(BaseAgentsAPIConfig):api_base: str | None,litellm_params: Mapping[str, object],) -> str:- return f"{self._base_url(api_base)}/agents/{name}"+ return join_api_base(self._base_url(api_base), f"agents/{name}")def transform_delete_response(self,@@ -265,7 +266,7 @@ class GeminiAgentsConfig(BaseAgentsAPIConfig):api_base: str | None,litellm_params: Mapping[str, object],) -> tuple[str, dict[str, object]]:- url: Final = f"{self._base_url(api_base)}/agents/{name}/versions"+ url: Final = join_api_base(self._base_url(api_base), f"agents/{name}/versions")params: Final[dict[str, object]] = {}if litellm_params.get("page_size"):params["pageSize"] = litellm_params["page_size"]diff --git a/litellm/llms/gemini/audio_transcription/transformation.py b/litellm/llms/gemini/audio_transcription/transformation.pyindex 48a345a4940..ea116a7e9de 100644--- a/litellm/llms/gemini/audio_transcription/transformation.py+++ b/litellm/llms/gemini/audio_transcription/transformation.py@@ -9,6 +9,7 @@ from litellm.litellm_core_utils.audio_utils.utils import (normalize_transcription_language_to_bcp47,process_audio_file,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -104,8 +105,7 @@ class GeminiAudioTranscriptionConfig(BaseAudioTranscriptionConfig):litellm_params: Mapping[str, object],stream: bool | None = None,) -> str:- resolved_api_base: Final = GeminiModelInfo.get_api_base(api_base)- return f"{resolved_api_base}/v1beta/interactions"+ return join_api_base(GeminiModelInfo.get_api_base(api_base), "v1beta/interactions")def transform_audio_transcription_request(self,diff --git a/litellm/llms/gemini/common_utils.py b/litellm/llms/gemini/common_utils.pyindex 8ee7a463828..13df3921d13 100644--- a/litellm/llms/gemini/common_utils.py+++ b/litellm/llms/gemini/common_utils.py@@ -11,6 +11,7 @@ from pydantic import JsonValue, TypeAdapterimport litellmfrom litellm.constants import DEFAULT_MAX_RECURSE_DEPTH+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_utils import BaseLLMModelInfo, BaseTokenCounterfrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_str@@ -377,7 +378,7 @@ class GeminiModelInfo(BaseLLMModelInfo):return "v1beta"@staticmethod- def get_api_base(api_base: str | None = None) -> str | None:+ def get_api_base(api_base: str | None = None) -> str:return api_base or get_secret_str("GEMINI_API_BASE") or "https://generativelanguage.googleapis.com"@staticmethod@@ -400,13 +401,13 @@ class GeminiModelInfo(BaseLLMModelInfo):api_base = GeminiModelInfo.get_api_base(api_base)api_key = GeminiModelInfo.get_api_key(api_key)endpoint: Final = f"/{self.api_version}/models"- if api_base is None or api_key is None:+ if api_key is None:raise ValueError("GEMINI_API_BASE or GEMINI_API_KEY/GOOGLE_API_KEY is not set. Please set the environment variable, to query Gemini's `/models` endpoint.")response: Final = litellm.module_level_client.get(- url=f"{api_base}{endpoint}",+ url=join_api_base(api_base, endpoint),headers={"x-goog-api-key": api_key},)diff --git a/litellm/llms/gemini/count_tokens/handler.py b/litellm/llms/gemini/count_tokens/handler.pyindex 1c9d09330a8..4ed47323d4f 100644--- a/litellm/llms/gemini/count_tokens/handler.py+++ b/litellm/llms/gemini/count_tokens/handler.py@@ -6,6 +6,7 @@ from pydantic import ConfigDict, RootModel, with_configfrom typing_extensions import NotRequired, ReadOnly, TypedDictimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, get_async_httpx_clientfrom litellm.types.utils import LlmProviders@@ -65,7 +66,7 @@ class GoogleAIStudioTokenCounter:Construct the URL for the Google Gen AI Studio countTokens endpoint."""base_url: Final = api_base or "https://generativelanguage.googleapis.com"- return f"{base_url}/v1beta/models/{model}:countTokens"+ return join_api_base(base_url, f"v1beta/models/{model}:countTokens")def __init__(self, client: AsyncHTTPHandler | None = None) -> None:super().__init__()diff --git a/litellm/llms/gemini/files/transformation.py b/litellm/llms/gemini/files/transformation.pyindex 2c62e04c5a3..f9a7e230ea9 100644--- a/litellm/llms/gemini/files/transformation.py+++ b/litellm/llms/gemini/files/transformation.py@@ -15,7 +15,7 @@ from typing_extensions import ReadOnly, Requiredfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.prompt_templates.common_utils import extract_file_data-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.files.transformation import (BaseFilesConfig,LiteLLMLoggingObj,@@ -106,8 +106,7 @@ class GoogleAIStudioFilesHandler(GeminiModelInfo, BaseFilesConfig):if not final_api_key:raise ValueError("api_key is required")- url: Final = f"{api_base}/{endpoint}"- return url+ return join_api_base(api_base, endpoint)def get_supported_openai_params(self, model: str) -> list[OpenAICreateFileRequestOptionalParams]:return []@@ -235,10 +234,11 @@ class GoogleAIStudioFilesHandler(GeminiModelInfo, BaseFilesConfig):file_part: Final = self._normalize_gemini_file_id(file_id)- api_base = self.get_api_base(litellm_params.get("api_base")) or "https://generativelanguage.googleapis.com"- api_base = api_base.rstrip("/")+ api_base: Final = (+ self.get_api_base(litellm_params.get("api_base")) or "https://generativelanguage.googleapis.com"+ )- url: Final = f"{api_base}/v1beta/{file_part}"+ url: Final = join_api_base(api_base, f"v1beta/{file_part}")# API key is passed via x-goog-api-key header (set in validate_environment)return url, {}@@ -345,7 +345,7 @@ class GoogleAIStudioFilesHandler(GeminiModelInfo, BaseFilesConfig):file_name: Final = self._normalize_gemini_file_id(file_id)# Construct the delete URL- url: Final = f"{api_base}/v1beta/{file_name}"+ url: Final = join_api_base(api_base, f"v1beta/{file_name}")# Add API key as header (Google AI Studio uses x-goog-api-key header)params: Final[dict] = {}diff --git a/litellm/llms/gemini/image_edit/transformation.py b/litellm/llms/gemini/image_edit/transformation.pyindex 31d3963c70c..450c38a2206 100644--- a/litellm/llms/gemini/image_edit/transformation.py+++ b/litellm/llms/gemini/image_edit/transformation.py@@ -7,6 +7,7 @@ import httpxfrom httpx._types import RequestFilesfrom litellm.images.utils import ImageEditRequestUtils+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfigfrom litellm.llms.gemini.common_utils import (get_gemini_image_generation_config,@@ -79,9 +80,8 @@ class GeminiImageEditConfig(BaseImageEditConfig):api_base: str | None,litellm_params: dict,) -> str:- base_url = api_base or get_secret_str("GEMINI_API_BASE") or self.DEFAULT_BASE_URL- base_url = base_url.rstrip("/")- return f"{base_url}/models/{model}:generateContent"+ base_url: Final = api_base or get_secret_str("GEMINI_API_BASE") or self.DEFAULT_BASE_URL+ return join_api_base(base_url, f"models/{model}:generateContent")def transform_image_edit_request(self,diff --git a/litellm/llms/gemini/image_generation/transformation.py b/litellm/llms/gemini/image_generation/transformation.pyindex fe82a9cae81..1aa3d074b1a 100644--- a/litellm/llms/gemini/image_generation/transformation.py+++ b/litellm/llms/gemini/image_generation/transformation.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -88,14 +89,12 @@ class GoogleImageGenConfig(BaseImageGenerationConfig):"""complete_url: str = api_base or get_secret_str("GEMINI_API_BASE") or self.DEFAULT_BASE_URL- complete_url = complete_url.rstrip("/")-# Gemini Flash Image Preview models use generateContent endpointif is_gemini_image_model(model):- complete_url = f"{complete_url}/models/{model}:generateContent"+ complete_url = join_api_base(complete_url, f"models/{model}:generateContent")else:# All other Imagen models use predict endpoint- complete_url = f"{complete_url}/models/{model}:predict"+ complete_url = join_api_base(complete_url, f"models/{model}:predict")return complete_urldiff --git a/litellm/llms/gemini/interactions/transformation.py b/litellm/llms/gemini/interactions/transformation.pyindex a58037f691f..6f3e9a56b92 100644--- a/litellm/llms/gemini/interactions/transformation.py+++ b/litellm/llms/gemini/interactions/transformation.py@@ -16,7 +16,7 @@ from typing_extensions import ReadOnly, TypedDictfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.core_helpers import process_response_headers-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.interactions.transformation import BaseInteractionsAPIConfigfrom litellm.llms.gemini.common_utils import GeminiError, GeminiModelInfofrom litellm.types.interactions import (@@ -153,10 +153,8 @@ class GoogleAIStudioInteractionsConfig(BaseInteractionsAPIConfig):if not api_key:raise ValueError("Google API key is required. Set GOOGLE_API_KEY or GEMINI_API_KEY environment variable.")- if stream:- return f"{api_base}/{self.api_version}/interactions?alt=sse"-- return f"{api_base}/{self.api_version}/interactions"+ collection_url: Final = join_api_base(api_base, f"{self.api_version}/interactions")+ return f"{collection_url}?alt=sse" if stream else collection_urldef transform_request(self,@@ -304,7 +302,7 @@ class GoogleAIStudioInteractionsConfig(BaseInteractionsAPIConfig):raise ValueError("Google API key is required")encoded_interaction_id: Final = encode_url_path_segment(interaction_id, field_name="interaction_id")return (- f"{resolved_api_base}/{self.api_version}/interactions/{encoded_interaction_id}",+ join_api_base(resolved_api_base, f"{self.api_version}/interactions/{encoded_interaction_id}"),{},)@@ -344,7 +342,7 @@ class GoogleAIStudioInteractionsConfig(BaseInteractionsAPIConfig):raise ValueError("Google API key is required")encoded_interaction_id: Final = encode_url_path_segment(interaction_id, field_name="interaction_id")return (- f"{resolved_api_base}/{self.api_version}/interactions/{encoded_interaction_id}",+ join_api_base(resolved_api_base, f"{self.api_version}/interactions/{encoded_interaction_id}"),{},)@@ -375,7 +373,7 @@ class GoogleAIStudioInteractionsConfig(BaseInteractionsAPIConfig):raise ValueError("Google API key is required")encoded_interaction_id: Final = encode_url_path_segment(interaction_id, field_name="interaction_id")return (- f"{resolved_api_base}/{self.api_version}/interactions/{encoded_interaction_id}/cancel",+ join_api_base(resolved_api_base, f"{self.api_version}/interactions/{encoded_interaction_id}/cancel"),{},)diff --git a/litellm/llms/gemini/realtime/transformation.py b/litellm/llms/gemini/realtime/transformation.pyindex f4e2941670a..9af7edeac86 100644--- a/litellm/llms/gemini/realtime/transformation.py+++ b/litellm/llms/gemini/realtime/transformation.py@@ -13,6 +13,7 @@ import litellmfrom litellm import verbose_loggerfrom litellm._uuid import uuidfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.realtime.transformation import BaseRealtimeConfigfrom litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import (VertexGeminiConfig,@@ -212,7 +213,10 @@ class GeminiRealtimeConfig(BaseRealtimeConfig):# so the API key must remain as a query parameter here. This is an accepted# limitation; httpx is not used for WebSocket so MaskedHTTPStatusError# already covers the main leak vector.- return f"{api_base}/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent?key={api_key}"+ live_url: Final = join_api_base(+ api_base, "ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent"+ )+ return f"{live_url}?key={api_key}"def map_model_turn_event(self, model_turn: HttpxContentType) -> OpenAIRealtimeEventTypes:"""diff --git a/litellm/llms/gemini/vector_stores/transformation.py b/litellm/llms/gemini/vector_stores/transformation.pyindex 34bd9660c43..11e619eb4c7 100644--- a/litellm/llms/gemini/vector_stores/transformation.py+++ b/litellm/llms/gemini/vector_stores/transformation.py@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom typing_extensions import ReadOnly, TypedDict+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.vector_store.transformation import BaseVectorStoreConfigfrom litellm.llms.gemini.common_utils import (GeminiError,@@ -141,15 +142,7 @@ class GeminiVectorStoreConfig(BaseVectorStoreConfig):Note: This returns the base URL WITHOUT the API key.The API key will be appended to specific endpoint URLs in the transform methods."""- if api_base is None:- api_base = GeminiModelInfo.get_api_base()-- if api_base is None:- raise ValueError("GEMINI_API_BASE is not set")-- # Ensure we're using the v1beta version for File Search- api_version: Final = "v1beta"- return f"{api_base}/{api_version}"+ return join_api_base(GeminiModelInfo.get_api_base(api_base), "v1beta")def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> GeminiError:"""Return Gemini-specific error class."""@@ -187,7 +180,7 @@ class GeminiVectorStoreConfig(BaseVectorStoreConfig):api_key: Final = litellm_params.get("api_key") or GeminiModelInfo.get_api_key()if not api_key:raise ValueError("GEMINI_API_KEY or GOOGLE_API_KEY is required")- url: Final = f"{api_base}/models/{model}:generateContent"+ url: Final = join_api_base(api_base, f"models/{model}:generateContent")# Build file_search tool configuration (using snake_case as per Gemini docs)file_search_config: Final[dict[str, object]] = {"file_search_store_names": [vector_store_id]}@@ -328,7 +321,7 @@ class GeminiVectorStoreConfig(BaseVectorStoreConfig):"""Transform create request to Gemini's fileSearchStores format."""- url: Final = f"{api_base}/fileSearchStores"+ url: Final = join_api_base(api_base, "fileSearchStores")# API key is passed via x-goog-api-key header (set in validate_environment)diff --git a/litellm/llms/gemini/videos/transformation.py b/litellm/llms/gemini/videos/transformation.pyindex 53ca5ed4adc..0656095927c 100644--- a/litellm/llms/gemini/videos/transformation.py+++ b/litellm/llms/gemini/videos/transformation.py@@ -8,6 +8,7 @@ from httpx._types import RequestFilesimport litellmfrom litellm.constants import DEFAULT_GOOGLE_VIDEO_DURATION_SECONDSfrom litellm.images.utils import ImageEditRequestUtils+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.videos.transformation import BaseVideoConfigfrom litellm.llms.vertex_ai.videos.transformation import veo_video_count_from_parametersfrom litellm.secret_managers.main import get_secret_str@@ -241,7 +242,7 @@ class GeminiVideoConfig(BaseVideoConfig):return api_base.rstrip("/")model_name: Final = model.replace("gemini/", "")- url: Final = f"{api_base.rstrip('/')}/v1beta/models/{model_name}:predictLongRunning"+ url: Final = join_api_base(api_base, f"v1beta/models/{model_name}:predictLongRunning")return url@@ -377,7 +378,7 @@ class GeminiVideoConfig(BaseVideoConfig):GET https://generativelanguage.googleapis.com/v1beta/{operation_name}"""operation_name: Final = extract_original_video_id(video_id)- url: Final = f"{api_base.rstrip('/')}/v1beta/{operation_name}"+ url: Final = join_api_base(api_base, f"v1beta/{operation_name}")params: Final[dict[str, object]] = {}return url, params@@ -451,7 +452,7 @@ class GeminiVideoConfig(BaseVideoConfig):"""operation_name: Final = extract_original_video_id(video_id)- status_url: Final = f"{api_base.rstrip('/')}/v1beta/{operation_name}"+ status_url: Final = join_api_base(api_base, f"v1beta/{operation_name}")client: Final = litellm.module_level_clientstatus_response: Final = client.get(url=status_url, headers=headers)status_response.raise_for_status()diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.pyindex 1fe7de0ba85..936e6339865 100644--- a/litellm/llms/gigachat/chat/transformation.py+++ b/litellm/llms/gigachat/chat/transformation.py@@ -17,6 +17,7 @@ import httpxfrom pydantic import TypeAdapterfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.llms.gigachat.utils import convert_usage, get_api_basefrom litellm.secret_managers.main import get_secret_str@@ -108,8 +109,7 @@ class GigaChatConfig(BaseConfig):stream: bool | None = None,) -> str:"""Get complete API URL for chat completions."""- base: Final = get_api_base(api_base)- return f"{base}/chat/completions"+ return join_api_base(get_api_base(api_base), "chat/completions")def validate_environment(self,diff --git a/litellm/llms/gigachat/embedding/transformation.py b/litellm/llms/gigachat/embedding/transformation.pyindex e0e746427ad..30ca7147865 100644--- a/litellm/llms/gigachat/embedding/transformation.py+++ b/litellm/llms/gigachat/embedding/transformation.py@@ -16,6 +16,7 @@ from pydantic import ConfigDict, TypeAdapterfrom litellm import LlmProvidersfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.llms.gigachat.utils import get_api_base@@ -98,8 +99,7 @@ class GigaChatEmbeddingConfig(BaseEmbeddingConfig):stream: bool | None = None,) -> str:"""Get the complete URL for embeddings endpoint."""- base: Final = get_api_base(api_base)- return f"{base}/embeddings"+ return join_api_base(get_api_base(api_base), "embeddings")def transform_embedding_request(self,diff --git a/litellm/llms/gigachat/file_handler.py b/litellm/llms/gigachat/file_handler.pyindex c051d693720..aff0465d1ec 100644--- a/litellm/llms/gigachat/file_handler.py+++ b/litellm/llms/gigachat/file_handler.py@@ -13,6 +13,7 @@ from collections.abc import Mappingfrom typing import Finalfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import (get_async_httpx_client,get_httpx_client,@@ -125,8 +126,7 @@ def upload_file_sync(access_token: Final = get_access_token(credentials=credentials, litellm_params=litellm_params)# Upload to GigaChat- base_url: Final = get_api_base(api_base)- upload_url: Final = f"{base_url}/files"+ upload_url: Final = join_api_base(get_api_base(api_base), "files")client: Final = get_httpx_client(params={"ssl_verify": False})response: Final = client.post(@@ -191,8 +191,7 @@ async def upload_file_async(access_token: Final = await get_access_token_async(credentials=credentials, litellm_params=litellm_params)# Upload to GigaChat- base_url: Final = get_api_base(api_base)- upload_url: Final = f"{base_url}/files"+ upload_url: Final = join_api_base(get_api_base(api_base), "files")client: Final = get_async_httpx_client(llm_provider=LlmProviders.GIGACHAT,diff --git a/litellm/llms/gigachat/passthrough/transformation.py b/litellm/llms/gigachat/passthrough/transformation.pyindex f16d263fd4d..872eee6dbf9 100644--- a/litellm/llms/gigachat/passthrough/transformation.py+++ b/litellm/llms/gigachat/passthrough/transformation.py@@ -40,7 +40,7 @@ class GigaChatPassthroughConfig(BasePassthroughConfig):if base_target_url is None:raise Exception("GigaChat api base not found")- complete_url: Final = f"{base_target_url}/{endpoint.lstrip('/')}"+ complete_url: Final = f"{base_target_url.rstrip('/')}/{endpoint.lstrip('/')}"return (httpx.URL(complete_url),@@ -193,8 +193,7 @@ class GigaChatPassthroughConfig(BasePassthroughConfig):@staticmethoddef get_api_base(api_base: str | None = None) -> str | None:- resolved_api_base: Final = api_base or get_secret_str("GIGACHAT_API_BASE") or GIGACHAT_BASE_URL- return resolved_api_base.rstrip("/") if resolved_api_base else None+ return api_base or get_secret_str("GIGACHAT_API_BASE") or GIGACHAT_BASE_URL@staticmethoddef get_api_key(diff --git a/litellm/llms/gigachat/utils.py b/litellm/llms/gigachat/utils.pyindex ab1890ad73b..55ca4260221 100644--- a/litellm/llms/gigachat/utils.py+++ b/litellm/llms/gigachat/utils.py@@ -21,5 +21,5 @@ def convert_usage(usage_data: Mapping[str, int]) -> Usage:)-def get_api_base(api_base: str | None = None) -> str | None:+def get_api_base(api_base: str | None = None) -> str:return (api_base or get_secret_str("GIGACHAT_API_BASE") or GIGACHAT_BASE_URL).rstrip("/")diff --git a/litellm/llms/github_copilot/embedding/transformation.py b/litellm/llms/github_copilot/embedding/transformation.pyindex 7f9209afd65..f16564ba505 100644--- a/litellm/llms/github_copilot/embedding/transformation.py+++ b/litellm/llms/github_copilot/embedding/transformation.py@@ -20,6 +20,7 @@ from pydantic import ConfigDict, TypeAdapterfrom litellm._logging import verbose_loggerfrom litellm.exceptions import AuthenticationError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.types.llms.openai import AllEmbeddingInputValues@@ -121,7 +122,7 @@ class GithubCopilotEmbeddingConfig(BaseEmbeddingConfig):user_session: Final = require_github_copilot_user_session(cast("dict[str, object]", litellm_params) # cast-ok: litellm_params arrives as an untyped request dict)- effective_api_base = (+ effective_api_base: Final = ((user_session.api_base if user_session is not None else None)or api_baseor self.authenticator.get_api_base()@@ -129,11 +130,7 @@ class GithubCopilotEmbeddingConfig(BaseEmbeddingConfig):or DEFAULT_GITHUB_COPILOT_API_BASE)- # Remove trailing slashes- effective_api_base = effective_api_base.rstrip("/")-- # Return the embeddings endpoint- return f"{effective_api_base}/embeddings"+ return join_api_base(effective_api_base, "embeddings")def transform_embedding_request(self,diff --git a/litellm/llms/github_copilot/messages/transformation.py b/litellm/llms/github_copilot/messages/transformation.pyindex be138755f85..56c3693a02c 100644--- a/litellm/llms/github_copilot/messages/transformation.py+++ b/litellm/llms/github_copilot/messages/transformation.py@@ -1,6 +1,7 @@from typing import Any, Final, cast # noqa: TID251 # narrows untyped request dicts at the session boundaryfrom litellm.exceptions import AuthenticationError+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.anthropic.pass_through.messages.transformation import (AnthropicMessagesConfig,)@@ -140,5 +141,5 @@ class GithubCopilotAnthropicMessagesConfig(AnthropicMessagesConfig):or api_baseor self.authenticator.get_api_base()or DEFAULT_GITHUB_COPILOT_API_BASE- ).rstrip("/")- return resolved if resolved.endswith("/v1/messages") else f"{resolved}/v1/messages"+ )+ return ensure_api_route(resolved, "v1/messages")diff --git a/litellm/llms/github_copilot/responses/transformation.py b/litellm/llms/github_copilot/responses/transformation.pyindex d468889b626..bf0b8c79068 100644--- a/litellm/llms/github_copilot/responses/transformation.py+++ b/litellm/llms/github_copilot/responses/transformation.py@@ -20,6 +20,7 @@ import litellmfrom litellm._logging import verbose_loggerfrom litellm.constants import DEFAULT_MAX_RECURSE_DEPTHfrom litellm.exceptions import AuthenticationError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.types.llms.openai import (ResponseInputParam,@@ -266,7 +267,7 @@ class GithubCopilotResponsesAPIConfig(OpenAIResponsesAPIConfig):user_session: Final = require_github_copilot_user_session(cast("dict[str, object]", litellm_params) # cast-ok: litellm_params arrives as an untyped request dict)- effective_api_base = (+ effective_api_base: Final = ((user_session.api_base if user_session is not None else None)or api_baseor self.authenticator.get_api_base()@@ -274,11 +275,7 @@ class GithubCopilotResponsesAPIConfig(OpenAIResponsesAPIConfig):or DEFAULT_GITHUB_COPILOT_API_BASE)- # Remove trailing slashes- effective_api_base = effective_api_base.rstrip("/")-- # Return the responses endpoint- return f"{effective_api_base}/responses"+ return join_api_base(effective_api_base, "responses")def _handle_reasoning_item(self, item: dict[str, object]) -> dict[str, object]:"""diff --git a/litellm/llms/gradient_ai/chat/transformation.py b/litellm/llms/gradient_ai/chat/transformation.pyindex fcee55a27e9..0ba5ca45c2a 100644--- a/litellm/llms/gradient_ai/chat/transformation.py+++ b/litellm/llms/gradient_ai/chat/transformation.py@@ -1,5 +1,6 @@from typing import Final, Literal+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import (AllMessageValues,@@ -105,10 +106,10 @@ class GradientAIConfig(OpenAILikeChatConfig):) -> str:clean_api_base: Final = (api_base or "").rstrip("/")if clean_api_base and clean_api_base != GRADIENT_AI_SERVERLESS_ENDPOINT:- return f"{clean_api_base}/api/v1/chat/completions"+ return join_api_base(clean_api_base, "api/v1/chat/completions")agent_endpoint: Final = (get_secret_str("GRADIENT_AI_AGENT_ENDPOINT") or "").rstrip("/")if agent_endpoint and agent_endpoint != GRADIENT_AI_SERVERLESS_ENDPOINT:- return f"{agent_endpoint}/api/v1/chat/completions"+ return join_api_base(agent_endpoint, "api/v1/chat/completions")return f"{GRADIENT_AI_SERVERLESS_ENDPOINT}/v1/chat/completions"def _get_openai_compatible_provider_info(diff --git a/litellm/llms/heroku/chat/transformation.py b/litellm/llms/heroku/chat/transformation.pyindex acd630c85d0..002078dd04b 100644--- a/litellm/llms/heroku/chat/transformation.py+++ b/litellm/llms/heroku/chat/transformation.py@@ -6,11 +6,12 @@ this is OpenAI compatible - no translation needed / occursimport osfrom collections.abc import Coroutine-from typing import Final, Literal, overload+from typing import Literal, overloadfrom litellm.litellm_core_utils.prompt_templates.common_utils import (handle_messages_with_content_list_to_str_conversion,)+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfigfrom litellm.types.llms.openai import AllMessageValues@@ -78,9 +79,4 @@ class HerokuChatConfig(OpenAIGPTConfig):"No api base was set. Please provide an api_base, or set the HEROKU_API_BASE environment variable.")- clean_api_base: Final = resolved_base.rstrip("/")-- if not clean_api_base.endswith("/v1/chat/completions"):- return f"{clean_api_base}/v1/chat/completions"-- return clean_api_base+ return ensure_api_route(resolved_base, "v1/chat/completions")diff --git a/litellm/llms/hosted_vllm/embedding/transformation.py b/litellm/llms/hosted_vllm/embedding/transformation.pyindex 3b54145a67b..d72a8d06f21 100644--- a/litellm/llms/hosted_vllm/embedding/transformation.py+++ b/litellm/llms/hosted_vllm/embedding/transformation.py@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -81,14 +82,7 @@ class HostedVLLMEmbeddingConfig(BaseEmbeddingConfig):if api_base is None:raise ValueError("api_base is required for hosted_vllm embeddings")- # Remove trailing slashes- api_base = api_base.rstrip("/")-- # Ensure the URL ends with /embeddings- if not api_base.endswith("/embeddings"):- api_base = f"{api_base}/embeddings"-- return api_base+ return ensure_api_route(api_base, "embeddings")def transform_embedding_request(self,diff --git a/litellm/llms/hosted_vllm/image_edit/transformation.py b/litellm/llms/hosted_vllm/image_edit/transformation.pyindex 6804d3a0fb8..8e05b6dfb46 100644--- a/litellm/llms/hosted_vllm/image_edit/transformation.py+++ b/litellm/llms/hosted_vllm/image_edit/transformation.py@@ -1,5 +1,6 @@from typing import Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.image_edit.transformation import OpenAIImageEditConfigfrom litellm.secret_managers.main import get_secret_str@@ -39,5 +40,5 @@ class HostedVLLMImageEditConfig(OpenAIImageEditConfig):)trimmed: Final = resolved_api_base.rstrip("/")if trimmed.endswith("/v1"):- return f"{trimmed}/images/edits"- return f"{trimmed}/v1/images/edits"+ return join_api_base(trimmed, "images/edits")+ return join_api_base(trimmed, "v1/images/edits")diff --git a/litellm/llms/hosted_vllm/rerank/transformation.py b/litellm/llms/hosted_vllm/rerank/transformation.pyindex a463fc1d3c9..4a5729945b4 100644--- a/litellm/llms/hosted_vllm/rerank/transformation.py+++ b/litellm/llms/hosted_vllm/rerank/transformation.py@@ -12,6 +12,7 @@ from pydantic import ValidationErrorfrom litellm._uuid import uuidfrom litellm.exceptions import UnsupportedParamsErrorfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.secret_managers.main import get_secret_str@@ -62,7 +63,7 @@ class HostedVLLMRerankConfig(BaseRerankConfig):if api_base.endswith("/v1/rerank"):api_base = api_base.replace("/v1/rerank", "/rerank")elif not api_base.endswith("/rerank"):- api_base = f"{api_base}/rerank"+ api_base = join_api_base(api_base, "rerank")return api_baseraise ValueError("api_base must be provided for Hosted VLLM rerank")diff --git a/litellm/llms/hosted_vllm/responses/transformation.py b/litellm/llms/hosted_vllm/responses/transformation.pyindex 20d755a2273..8489bc0da84 100644--- a/litellm/llms/hosted_vllm/responses/transformation.py+++ b/litellm/llms/hosted_vllm/responses/transformation.py@@ -8,6 +8,7 @@ the chat completions → responses conversion pipeline.from typing import Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.router import GenericLiteLLMParams@@ -64,9 +65,9 @@ class HostedVLLMResponsesAPIConfig(OpenAIResponsesAPIConfig):# If api_base already ends with /v1, append /responses# Otherwise append /v1/responsesif api_base.endswith("/v1"):- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")- return f"{api_base}/v1/responses"+ return join_api_base(api_base, "v1/responses")def supports_native_websocket(self) -> bool:"""Hosted vLLM does not support native WebSocket for Responses API"""diff --git a/litellm/llms/hosted_vllm/transcriptions/transformation.py b/litellm/llms/hosted_vllm/transcriptions/transformation.pyindex 9a8c67fadc0..785ccd52a69 100644--- a/litellm/llms/hosted_vllm/transcriptions/transformation.py+++ b/litellm/llms/hosted_vllm/transcriptions/transformation.py@@ -6,6 +6,7 @@ from typing import Finalimport httpx+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,)@@ -40,11 +41,7 @@ class HostedVLLMAudioTranscriptionConfig(OpenAIWhisperAudioTranscriptionConfig):stream: bool | None = None,) -> str:if api_base:- # Remove trailing slashes and ensure clean base URL- api_base = api_base.rstrip("/")- if not api_base.endswith("/v1/audio/transcriptions"):- api_base = f"{api_base}/v1/audio/transcriptions"- return api_base+ return ensure_api_route(api_base, "v1/audio/transcriptions")raise ValueError("api_base must be provided for Hosted VLLM rerank")def transform_audio_transcription_request(diff --git a/litellm/llms/hosted_vllm/videos/transformation.py b/litellm/llms/hosted_vllm/videos/transformation.pyindex 22e4f4876fb..30ae6835263 100644--- a/litellm/llms/hosted_vllm/videos/transformation.py+++ b/litellm/llms/hosted_vllm/videos/transformation.py@@ -10,7 +10,7 @@ from urllib.parse import urlparsefrom httpx._types import FileTypes, RequestFilesfrom litellm.images.utils import ImageEditRequestUtils-from litellm.litellm_core_utils.url_utils import SSRFError, validate_url+from litellm.litellm_core_utils.url_utils import SSRFError, join_api_base, validate_urlfrom litellm.llms.openai.videos.transformation import OpenAIVideoConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.router import GenericLiteLLMParams@@ -177,8 +177,8 @@ class HostedVLLMVideoConfig(OpenAIVideoConfig):)trimmed: Final = resolved_api_base.rstrip("/")if trimmed.endswith("/v1"):- return f"{trimmed}/videos"- return f"{trimmed}/v1/videos"+ return join_api_base(trimmed, "videos")+ return join_api_base(trimmed, "v1/videos")def transform_video_create_request(self,diff --git a/litellm/llms/huggingface/chat/transformation.py b/litellm/llms/huggingface/chat/transformation.pyindex b45c706f8bf..3fcb738eacb 100644--- a/litellm/llms/huggingface/chat/transformation.py+++ b/litellm/llms/huggingface/chat/transformation.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.types.llms.openai import AllMessageValues, ChatCompletionRequestif TYPE_CHECKING:@@ -29,11 +30,11 @@ def _build_chat_completion_url(model_url: str) -> str:# Append /chat/completions if not already presentif model_url.endswith("/v1"):- model_url += "/chat/completions"+ model_url = join_api_base(model_url, "chat/completions")# Append /v1/chat/completions if not already presentif not model_url.endswith("/chat/completions"):- model_url += "/v1/chat/completions"+ model_url = join_api_base(model_url, "v1/chat/completions")return model_urldiff --git a/litellm/llms/huggingface/rerank/transformation.py b/litellm/llms/huggingface/rerank/transformation.pyindex 334c60ee848..1217a2540d4 100644--- a/litellm/llms/huggingface/rerank/transformation.py+++ b/litellm/llms/huggingface/rerank/transformation.py@@ -7,6 +7,7 @@ from typing_extensions import TypedDictimport litellmfrom litellm._uuid import uuid+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.secret_managers.main import get_secret_str@@ -69,15 +70,7 @@ class HuggingFaceRerankConfig(BaseRerankConfig):"""Get the complete URL for the API call, including the /rerank suffix if necessary."""- # Get base URL from api_base or default- base_url = self.get_api_base(model=model, api_base=api_base)-- # Remove trailing slashes and ensure we have the /rerank endpoint- base_url = base_url.rstrip("/")- if not base_url.endswith("/rerank"):- base_url = f"{base_url}/rerank"-- return base_url+ return ensure_api_route(self.get_api_base(model=model, api_base=api_base), "rerank")def get_supported_cohere_rerank_params(self, model: str) -> list:return [diff --git a/litellm/llms/infinity/embedding/transformation.py b/litellm/llms/infinity/embedding/transformation.pyindex f289918cb3c..a467c56fc74 100644--- a/litellm/llms/infinity/embedding/transformation.py+++ b/litellm/llms/infinity/embedding/transformation.py@@ -5,6 +5,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -36,11 +37,7 @@ class InfinityEmbeddingConfig(BaseEmbeddingConfig):) -> str:if api_base is None:raise ValueError("api_base is required for Infinity embeddings")- # Remove trailing slashes and ensure clean base URL- api_base = api_base.rstrip("/")- if not api_base.endswith("/embeddings"):- api_base = f"{api_base}/embeddings"- return api_base+ return ensure_api_route(api_base, "embeddings")def validate_environment(self,diff --git a/litellm/llms/infinity/rerank/transformation.py b/litellm/llms/infinity/rerank/transformation.pyindex e089b3fecbe..07f3bb2c26b 100644--- a/litellm/llms/infinity/rerank/transformation.py+++ b/litellm/llms/infinity/rerank/transformation.py@@ -13,6 +13,7 @@ from typing_extensions import NotRequired, ReadOnly, TypedDictimport litellmfrom litellm._uuid import uuidfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.cohere.rerank.transformation import CohereRerankConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.rerank import (@@ -61,11 +62,7 @@ class InfinityRerankConfig(CohereRerankConfig):) -> str:if api_base is None:raise ValueError("api_base is required for Infinity rerank")- # Remove trailing slashes and ensure clean base URL- api_base = api_base.rstrip("/")- if not api_base.endswith("/rerank"):- api_base = f"{api_base}/rerank"- return api_base+ return ensure_api_route(api_base, "rerank")def validate_environment(self,diff --git a/litellm/llms/jina_ai/embedding/transformation.py b/litellm/llms/jina_ai/embedding/transformation.pyindex 9b156e5ae21..04ff8f941a3 100644--- a/litellm/llms/jina_ai/embedding/transformation.py+++ b/litellm/llms/jina_ai/embedding/transformation.py@@ -15,6 +15,7 @@ from pydantic import ConfigDict, TypeAdapterfrom litellm import LlmProvidersfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm import BaseEmbeddingConfigfrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_str@@ -115,12 +116,7 @@ class JinaAIEmbeddingConfig(BaseEmbeddingConfig):litellm_params: dict,stream: bool | None = None,) -> str:- if api_base:- clean_api_base: Final = api_base.rstrip("/")- if not clean_api_base.endswith("/embeddings"):- return f"{clean_api_base}/embeddings"- return clean_api_base- return "https://api.jina.ai/v1/embeddings"+ return ensure_api_route(api_base or "https://api.jina.ai/v1", "embeddings")def transform_embedding_request(self,diff --git a/litellm/llms/langflow/chat/transformation.py b/litellm/llms/langflow/chat/transformation.pyindex 1781082d36f..0938cc0d07b 100644--- a/litellm/llms/langflow/chat/transformation.py+++ b/litellm/llms/langflow/chat/transformation.py@@ -9,6 +9,7 @@ from litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.prompt_templates.common_utils import (convert_content_list_to_str,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.utils import Choices, Message, ModelResponse, Usage@@ -107,9 +108,8 @@ class LangFlowConfig(BaseConfig):"api_base is required for LangFlow. Set it via LANGFLOW_API_BASE env var or api_base parameter.")- api_base = api_base.rstrip("/")flow_id: Final = quote(self._get_flow_id(model, optional_params), safe="")- return f"{api_base}/api/v1/run/{flow_id}"+ return join_api_base(api_base, f"api/v1/run/{flow_id}")def _get_last_user_message(self, messages: list[AllMessageValues]) -> str:"""Extract the text of the last user message to use as input_value."""diff --git a/litellm/llms/langgraph/chat/transformation.py b/litellm/llms/langgraph/chat/transformation.pyindex 4f93f435957..ce624eecc0e 100644--- a/litellm/llms/langgraph/chat/transformation.py+++ b/litellm/llms/langgraph/chat/transformation.py@@ -18,6 +18,7 @@ from litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.prompt_templates.common_utils import (convert_content_list_to_str,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.llms.langgraph.chat.sse_iterator import LangGraphSSEStreamIteratorfrom litellm.types.llms.openai import AllMessageValues@@ -116,14 +117,11 @@ class LangGraphConfig(BaseConfig):"api_base is required for LangGraph. Set it via LANGGRAPH_API_BASE env var or api_base parameter.")- # Remove trailing slash if present- api_base = api_base.rstrip("/")-# Choose endpoint based on streaming modeif stream:- return f"{api_base}/runs/stream"+ return join_api_base(api_base, "runs/stream")else:- return f"{api_base}/runs/wait"+ return join_api_base(api_base, "runs/wait")def _get_assistant_id(self, model: str, optional_params: dict) -> str:"""diff --git a/litellm/llms/lemonade/chat/transformation.py b/litellm/llms/lemonade/chat/transformation.pyindex a740050dd94..01fdc0b8bf8 100644--- a/litellm/llms/lemonade/chat/transformation.py+++ b/litellm/llms/lemonade/chat/transformation.py@@ -12,6 +12,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import (AllMessageValues,@@ -105,7 +106,7 @@ class LemonadeChatConfig(OpenAILikeChatConfig):# Getting the list of models from lemonadetry:response: Final = litellm.module_level_client.get(- url=f"{api_base}/models",+ url=join_api_base(api_base, "models"),headers=self._get_auth_headers(api_key),)except Exception as e:@@ -193,7 +194,7 @@ class LemonadeChatConfig(OpenAILikeChatConfig):try:response: Final = litellm.module_level_client.get(- url=f"{api_base}/models/{encoded_model}",+ url=join_api_base(str(api_base), f"models/{encoded_model}"),headers=self._get_auth_headers(api_key),)response.raise_for_status()diff --git a/litellm/llms/linkup/search/transformation.py b/litellm/llms/linkup/search/transformation.pyindex e219a227b1f..fd1ca792302 100644--- a/litellm/llms/linkup/search/transformation.py+++ b/litellm/llms/linkup/search/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -87,11 +88,7 @@ class LinkupSearchConfig(BaseSearchConfig):"""Get complete URL for Search endpoint."""- resolved_api_base: Final = api_base or get_secret_str("LINKUP_API_BASE") or self.LINKUP_API_BASE- clean_api_base: Final = resolved_api_base.rstrip("/")- if clean_api_base.endswith("/search"):- return clean_api_base- return f"{clean_api_base}/search"+ return ensure_api_route(api_base or get_secret_str("LINKUP_API_BASE") or self.LINKUP_API_BASE, "search")def transform_search_request(self,diff --git a/litellm/llms/litellm_proxy/image_edit/transformation.py b/litellm/llms/litellm_proxy/image_edit/transformation.pyindex 9e5a2a2c343..f319e31ff3f 100644--- a/litellm/llms/litellm_proxy/image_edit/transformation.py+++ b/litellm/llms/litellm_proxy/image_edit/transformation.py@@ -1,3 +1,4 @@+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.image_edit.transformation import OpenAIImageEditConfigfrom litellm.secret_managers.main import get_secret_str@@ -21,5 +22,4 @@ class LiteLLMProxyImageEditConfig(OpenAIImageEditConfig):api_base = api_base or get_secret_str("LITELLM_PROXY_API_BASE")if api_base is None:raise ValueError("api_base not set for LiteLLM Proxy route. Set in env via `LITELLM_PROXY_API_BASE`")- api_base = api_base.rstrip("/")- return f"{api_base}/images/edits"+ return join_api_base(api_base, "images/edits")diff --git a/litellm/llms/litellm_proxy/image_generation/transformation.py b/litellm/llms/litellm_proxy/image_generation/transformation.pyindex fcddeabfc85..05e536eece5 100644--- a/litellm/llms/litellm_proxy/image_generation/transformation.py+++ b/litellm/llms/litellm_proxy/image_generation/transformation.py@@ -1,3 +1,4 @@+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.image_generation.gpt_transformation import (GPTImageGenerationConfig,)@@ -33,5 +34,4 @@ class LiteLLMProxyImageGenerationConfig(GPTImageGenerationConfig):api_base = api_base or get_secret_str("LITELLM_PROXY_API_BASE")if api_base is None:raise ValueError("api_base not set for LiteLLM Proxy route. Set in env via `LITELLM_PROXY_API_BASE`")- api_base = api_base.rstrip("/")- return f"{api_base}/images/generations"+ return join_api_base(api_base, "images/generations")diff --git a/litellm/llms/litellm_proxy/responses/transformation.py b/litellm/llms/litellm_proxy/responses/transformation.pyindex 2928b07665a..3092b49b743 100644--- a/litellm/llms/litellm_proxy/responses/transformation.py+++ b/litellm/llms/litellm_proxy/responses/transformation.py@@ -5,6 +5,7 @@ LiteLLM Proxy supports the OpenAI Responses API natively when the underlying modThis config enables pass-through behavior to the proxy's /v1/responses endpoint."""+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.utils import LlmProviders@@ -40,10 +41,7 @@ class LiteLLMProxyResponsesAPIConfig(OpenAIResponsesAPIConfig):"Set via api_base parameter or LITELLM_PROXY_API_BASE environment variable")- # Remove trailing slashes- api_base = api_base.rstrip("/")-- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")def supports_native_websocket(self) -> bool:"""LiteLLM Proxy does not support native WebSocket for Responses API"""diff --git a/litellm/llms/manus/files/transformation.py b/litellm/llms/manus/files/transformation.pyindex 94b0e62199d..991aa4cbc32 100644--- a/litellm/llms/manus/files/transformation.py+++ b/litellm/llms/manus/files/transformation.py@@ -21,7 +21,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.prompt_templates.common_utils import extract_file_data-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.files.transformation import (BaseFilesConfig,@@ -140,8 +140,8 @@ class ManusFilesConfig(BaseFilesConfig):# Manus API uses /v1/files endpointif api_base.endswith("/v1"):- return f"{api_base}/files"- return f"{api_base}/v1/files"+ return join_api_base(api_base, "files")+ return join_api_base(api_base, "v1/files")def get_error_class(self,@@ -423,7 +423,7 @@ class ManusFilesConfig(BaseFilesConfig):litellm_params=litellm_params,)encoded_file_id: Final = encode_url_path_segment(file_id, field_name="file_id")- return f"{api_base}/{encoded_file_id}/content", {}+ return join_api_base(api_base, f"{encoded_file_id}/content"), {}def transform_file_content_response(self,diff --git a/litellm/llms/manus/responses/transformation.py b/litellm/llms/manus/responses/transformation.pyindex 9785a50bbce..ed06391a260 100644--- a/litellm/llms/manus/responses/transformation.py+++ b/litellm/llms/manus/responses/transformation.py@@ -10,7 +10,7 @@ from litellm.litellm_core_utils.core_helpers import process_response_headersfrom litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (safe_convert_created_field,)-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.openai.common_utils import OpenAIErrorfrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.secret_managers.main import get_secret_str@@ -119,8 +119,8 @@ class ManusResponsesAPIConfig(OpenAIResponsesAPIConfig):# Manus API uses /v1/responses endpoint (OpenAI-compatible)if api_base.endswith("/v1"):- return f"{api_base}/responses"- return f"{api_base}/v1/responses"+ return join_api_base(api_base, "responses")+ return join_api_base(api_base, "v1/responses")def transform_responses_api_request(self,diff --git a/litellm/llms/milvus/vector_stores/transformation.py b/litellm/llms/milvus/vector_stores/transformation.pyindex 70d3649debd..67de3a06af8 100644--- a/litellm/llms/milvus/vector_stores/transformation.py+++ b/litellm/llms/milvus/vector_stores/transformation.py@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.vector_store.transformation import (BaseQueryEmbeddingVectorStoreConfig,VectorStoreEmbeddingExecutor,@@ -185,7 +186,7 @@ class MilvusVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig):}litellm_logging_obj.model_call_details["input"] = query_textlitellm_logging_obj.model_call_details["embedding_model"] = litellm_params.get("litellm_embedding_model")- return f"{api_base}/v2/vectordb/entities/search", {+ return join_api_base(api_base, "v2/vectordb/entities/search"), {"collectionName": vector_store_id,"data": [query_vector],"annsField": "book_intro_vector",diff --git a/litellm/llms/minimax/chat/transformation.py b/litellm/llms/minimax/chat/transformation.pyindex c5aa8811f02..f397bf7fbb9 100644--- a/litellm/llms/minimax/chat/transformation.py+++ b/litellm/llms/minimax/chat/transformation.py@@ -5,6 +5,7 @@ MiniMax OpenAI transformation config - extends OpenAI chat config for MiniMax'sfrom typing import Finalimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues, ChatCompletionToolParam@@ -55,17 +56,14 @@ class MinimaxChatConfig(OpenAIGPTConfig):Override to ensure we use MiniMax's endpoint."""# Get the base URL (either provided or default MiniMax endpoint)- base_url: Final = self.get_api_base(api_base=api_base)+ base_url: Final = self.get_api_base(api_base=api_base).rstrip("/")# Ensure it ends with /chat/completionsif base_url.endswith("/chat/completions"):return base_url- elif base_url.endswith("/v1"):- return f"{base_url}/chat/completions"- elif base_url.endswith("/"):- return f"{base_url}v1/chat/completions"- else:- return f"{base_url}/v1/chat/completions"+ if base_url.endswith("/v1"):+ return join_api_base(base_url, "chat/completions")+ return join_api_base(base_url, "v1/chat/completions")def remove_cache_control_flag_from_messages_and_tools(self,diff --git a/litellm/llms/minimax/messages/transformation.py b/litellm/llms/minimax/messages/transformation.pyindex d62b88a24c6..f9f3502237c 100644--- a/litellm/llms/minimax/messages/transformation.py+++ b/litellm/llms/minimax/messages/transformation.py@@ -2,9 +2,10 @@MiniMax Anthropic transformation config - extends AnthropicConfig for MiniMax's Anthropic-compatible API"""-from typing import Any, Final # noqa: TID251 # override below must mirror the legacy base signature+from typing import Any # noqa: TID251 # override below must mirror the legacy base signatureimport litellm+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.anthropic.pass_through.messages.transformation import (AnthropicMessagesConfig,)@@ -82,15 +83,4 @@ class MinimaxMessagesConfig(AnthropicMessagesConfig):Get the complete URL for MiniMax API.Override to ensure we use MiniMax's endpoint, not Anthropic's."""- # Get the base URL (either provided or default MiniMax endpoint)- base_url: Final = self.get_api_base(api_base=api_base)-- # If the base URL already includes the full path, return it- if base_url.endswith("/v1/messages"):- return base_url-- # Otherwise append the messages endpoint- if base_url.endswith("/"):- return f"{base_url}v1/messages"- else:- return f"{base_url}/v1/messages"+ return ensure_api_route(self.get_api_base(api_base=api_base), "v1/messages")diff --git a/litellm/llms/minimax/text_to_speech/transformation.py b/litellm/llms/minimax/text_to_speech/transformation.pyindex 1cabc43eb1c..6f2cb36ed03 100644--- a/litellm/llms/minimax/text_to_speech/transformation.py+++ b/litellm/llms/minimax/text_to_speech/transformation.py@@ -13,6 +13,7 @@ from httpx import Headersfrom pydantic import ConfigDict, TypeAdapterimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.text_to_speech.transformation import (BaseTextToSpeechConfig,@@ -404,10 +405,5 @@ class MinimaxTextToSpeechConfig(BaseTextToSpeechConfig):"""Construct the MiniMax endpoint URL."""- base_url = api_base or get_secret_str("MINIMAX_API_BASE") or self.TTS_BASE_URL- base_url = base_url.rstrip("/")-- # MiniMax uses a simple endpoint path- url: Final = f"{base_url}{self.TTS_ENDPOINT_PATH}"-- return url+ base_url: Final = api_base or get_secret_str("MINIMAX_API_BASE") or self.TTS_BASE_URL+ return join_api_base(base_url, self.TTS_ENDPOINT_PATH)diff --git a/litellm/llms/mistral/audio_speech/transformation.py b/litellm/llms/mistral/audio_speech/transformation.pyindex 5a8960d0898..ac0e1a33620 100644--- a/litellm/llms/mistral/audio_speech/transformation.py+++ b/litellm/llms/mistral/audio_speech/transformation.py@@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter, ValidationError+from litellm.litellm_core_utils.url_utils import ensure_api_route, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.text_to_speech.transformation import (BaseTextToSpeechConfig,@@ -118,9 +119,7 @@ class MistralTextToSpeechConfig(BaseTextToSpeechConfig):api_base: str | None,litellm_params: Mapping[str, object],) -> str:- configured_base: Final = (api_base or self.TTS_BASE_URL).rstrip("/")- versioned_base: Final = configured_base if configured_base.endswith("/v1") else f"{configured_base}/v1"- return f"{versioned_base}/audio/speech"+ return join_api_base(ensure_api_route(api_base or self.TTS_BASE_URL, "v1"), "audio/speech")def transform_text_to_speech_request(self,diff --git a/litellm/llms/mistral/audio_transcription/transformation.py b/litellm/llms/mistral/audio_transcription/transformation.pyindex 4fce7a95350..3eb9e122265 100644--- a/litellm/llms/mistral/audio_transcription/transformation.py+++ b/litellm/llms/mistral/audio_transcription/transformation.py@@ -9,6 +9,7 @@ from typing import Finalimport httpxfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -57,8 +58,7 @@ class MistralAudioTranscriptionConfig(BaseAudioTranscriptionConfig):litellm_params: dict,stream: bool | None = None,) -> str:- api_base = "https://api.mistral.ai/v1" if api_base is None else api_base.rstrip("/")- return f"{api_base}/audio/transcriptions"+ return join_api_base("https://api.mistral.ai/v1" if api_base is None else api_base, "audio/transcriptions")def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:return MistralAudioTranscriptionException(diff --git a/litellm/llms/mistral/batches/transformation.py b/litellm/llms/mistral/batches/transformation.pyindex 86de2836048..18f5df9b6e9 100644--- a/litellm/llms/mistral/batches/transformation.py+++ b/litellm/llms/mistral/batches/transformation.py@@ -18,7 +18,7 @@ from openai.types.batch_error import BatchErrorfrom pydantic import ConfigDictfrom typing_extensions import NotRequired, ReadOnly, TypedDict-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.batches.transformation import BaseBatchesConfigfrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.llms.base import LiteLLMBaseModel@@ -156,7 +156,7 @@ class MistralBatchesConfig(BaseBatchesConfig):litellm_params: Mapping[str, object],data: CreateBatchRequest,) -> str:- return f"{get_mistral_api_base(api_base)}/v1/batch/jobs"+ return join_api_base(get_mistral_api_base(api_base), "v1/batch/jobs")def transform_create_batch_request(self,@@ -199,7 +199,10 @@ class MistralBatchesConfig(BaseBatchesConfig):api_key: Final = litellm_params.get("api_key")request: Final = MistralPresignedRequest(method="GET",- url=f"{get_mistral_api_base(api_base if isinstance(api_base, str) else None)}/v1/batch/jobs/{encoded_batch_id}",+ url=join_api_base(+ get_mistral_api_base(api_base if isinstance(api_base, str) else None),+ f"v1/batch/jobs/{encoded_batch_id}",+ ),headers=get_mistral_auth_headers(_NO_HEADERS, api_key if isinstance(api_key, str) else None),)return dict(request)diff --git a/litellm/llms/mistral/chat/transformation.py b/litellm/llms/mistral/chat/transformation.pyindex b8a6cb1beb9..2c0acacff23 100644--- a/litellm/llms/mistral/chat/transformation.py+++ b/litellm/llms/mistral/chat/transformation.py@@ -19,6 +19,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (strip_litellm_internal_message_fields,strip_none_values_from_message,)+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.openai.chat.gpt_transformation import (OpenAIChatCompletionStreamingHandler,OpenAIGPTConfig,@@ -220,8 +221,7 @@ class MistralConfig(OpenAIGPTConfig):)# if api_base does not end with /v1 we add it- if api_base is not None and not api_base.endswith("/v1"): # Mistral always needs a /v1 at the end- api_base = api_base + "/v1"+ api_base = ensure_api_route(api_base, "v1")dynamic_api_key: Final = (api_keyor get_secret_str("MISTRAL_AZURE_API_KEY") # for Azure AI Mistraldiff --git a/litellm/llms/mistral/files/transformation.py b/litellm/llms/mistral/files/transformation.pyindex 86bce763917..908567e88fb 100644--- a/litellm/llms/mistral/files/transformation.py+++ b/litellm/llms/mistral/files/transformation.py@@ -18,7 +18,7 @@ from pydantic import ConfigDictfrom typing_extensions import ReadOnly, TypedDictfrom litellm.litellm_core_utils.prompt_templates.common_utils import extract_file_data-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.files.transformation import BaseFilesConfig, LiteLLMLoggingObjfrom litellm.types.llms.base import LiteLLMBaseModel@@ -130,11 +130,11 @@ class MistralFilesConfig(BaseFilesConfig):litellm_params: Mapping[str, object],stream: bool | None = None,) -> str:- return f"{get_mistral_api_base(api_base)}/v1/files"+ return join_api_base(get_mistral_api_base(api_base), "v1/files")def _file_url(self, file_id: str, litellm_params: Mapping[str, object], suffix: str = "") -> str:encoded_file_id: Final = encode_url_path_segment(file_id, field_name="file_id")- return f"{_api_base_from(litellm_params)}/v1/files/{encoded_file_id}{suffix}"+ return join_api_base(_api_base_from(litellm_params), f"v1/files/{encoded_file_id}{suffix}")def get_error_class(self, error_message: str, status_code: int, headers: Mapping[str, str] | httpx.Headers@@ -233,7 +233,7 @@ class MistralFilesConfig(BaseFilesConfig):optional_params: Mapping[str, object],litellm_params: Mapping[str, object],) -> tuple[str, dict[str, str]]: # mutable-ok: BaseFilesConfig signature- url: Final = f"{_api_base_from(litellm_params)}/v1/files"+ url: Final = join_api_base(_api_base_from(litellm_params), "v1/files")if not purpose:return url, _NO_QUERY_PARAMSreturn url, {"purpose": _to_mistral_purpose(purpose)}diff --git a/litellm/llms/modelscope/chat/transformation.py b/litellm/llms/modelscope/chat/transformation.pyindex c54437b9a21..ec48b344bd9 100644--- a/litellm/llms/modelscope/chat/transformation.py+++ b/litellm/llms/modelscope/chat/transformation.py@@ -7,6 +7,7 @@ from typing import Final, Literal, cast, overloadfrom typing_extensions import override+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues@@ -86,9 +87,4 @@ class ModelScopeChatConfig(OpenAIGPTConfig):"""If api_base is not provided, use the default ModelScope /chat/completions endpoint."""- clean_api_base: Final = (api_base or self.DEFAULT_BASE_URL).rstrip("/")-- if not clean_api_base.endswith("/chat/completions"):- return f"{clean_api_base}/chat/completions"-- return clean_api_base+ return ensure_api_route(api_base or self.DEFAULT_BASE_URL, "chat/completions")diff --git a/litellm/llms/modelscope/image_generation/transformation.py b/litellm/llms/modelscope/image_generation/transformation.pyindex 08cdbacdad0..36c8705f256 100644--- a/litellm/llms/modelscope/image_generation/transformation.py+++ b/litellm/llms/modelscope/image_generation/transformation.py@@ -13,6 +13,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom typing_extensions import override+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,@@ -90,11 +91,10 @@ class ModelScopeImageGenerationConfig(BaseImageGenerationConfig):"""Get the complete URL for the ModelScope image generation API request."""- base_url: str = api_base or get_secret_str("MODELSCOPE_API_BASE") or self.DEFAULT_BASE_URL- base_url = base_url.rstrip("/")+ base_url: Final = api_base or get_secret_str("MODELSCOPE_API_BASE") or self.DEFAULT_BASE_URL# Return the images endpoint- return f"{base_url}/images/generations"+ return join_api_base(base_url, "images/generations")@overridedef validate_environment(diff --git a/litellm/llms/mongodb/vector_stores/transformation.py b/litellm/llms/mongodb/vector_stores/transformation.pyindex 0d931ee78cb..7bf9d8b8441 100644--- a/litellm/llms/mongodb/vector_stores/transformation.py+++ b/litellm/llms/mongodb/vector_stores/transformation.py@@ -9,6 +9,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapter, ValidationErrorfrom litellm.exceptions import AuthenticationError, BadRequestError, ServiceUnavailableError, Timeout+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.vector_store.transformation import (BaseQueryEmbeddingVectorStoreConfig,@@ -283,7 +284,7 @@ class MongoDBVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig):raise config_error("The embedding model must return a non-empty, finite query vector.")limit: Final = cls._limit(optional_params)return (- f"{api_base}/v1/vector_stores/{quote(vector_store_id, safe='')}/search",+ join_api_base(api_base, f"v1/vector_stores/{quote(vector_store_id, safe='')}/search"),{"query": query_text,"query_vector": tuple(vector),diff --git a/litellm/llms/moonshot/chat/transformation.py b/litellm/llms/moonshot/chat/transformation.pyindex 93496e775db..698db8b7c25 100644--- a/litellm/llms/moonshot/chat/transformation.py+++ b/litellm/llms/moonshot/chat/transformation.py@@ -9,6 +9,7 @@ import litellmfrom litellm.litellm_core_utils.prompt_templates.common_utils import (handle_messages_with_content_list_to_str_conversion,)+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.utils import supports_reasoning@@ -93,12 +94,7 @@ class MoonshotChatConfig(OpenAIGPTConfig):"""If api_base is not provided, use the default Moonshot AI /chat/completions endpoint."""- clean_api_base: Final = (api_base or "https://api.moonshot.ai/v1").rstrip("/")-- if not clean_api_base.endswith("/chat/completions"):- return f"{clean_api_base}/chat/completions"-- return clean_api_base+ return ensure_api_route(api_base or "https://api.moonshot.ai/v1", "chat/completions")def get_supported_openai_params(self, model: str) -> list:"""diff --git a/litellm/llms/nimble/search/transformation.py b/litellm/llms/nimble/search/transformation.pyindex 30cfc38eb87..b4d9be909c3 100644--- a/litellm/llms/nimble/search/transformation.py+++ b/litellm/llms/nimble/search/transformation.py@@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter, ValidationError+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,@@ -124,10 +125,7 @@ class NimbleSearchConfig(BaseSearchConfig):data: dict[str, object] | list[dict[str, object]] | None = None, # mutable-ok: base signature**kwargs: object, # kwargs-ok: BaseSearchConfig.get_complete_url signature) -> str:- resolved_base: Final = (api_base or get_secret_str("NIMBLE_API_BASE") or self.NIMBLE_API_BASE).rstrip("/")- if resolved_base.endswith("/search"):- return resolved_base- return f"{resolved_base}/search"+ return ensure_api_route(api_base or get_secret_str("NIMBLE_API_BASE") or self.NIMBLE_API_BASE, "search")def transform_search_request(self,diff --git a/litellm/llms/nvidia_nim/rerank/ranking_transformation.py b/litellm/llms/nvidia_nim/rerank/ranking_transformation.pyindex 177d7883feb..39391be0fdb 100644--- a/litellm/llms/nvidia_nim/rerank/ranking_transformation.py+++ b/litellm/llms/nvidia_nim/rerank/ranking_transformation.py@@ -11,6 +11,7 @@ from typing import Finalimport httpxfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.nvidia_nim.rerank.transformation import NvidiaNimRerankConfigfrom litellm.types.rerank import RerankResponse@@ -76,7 +77,7 @@ class NvidiaNimRankingConfig(NvidiaNimRerankConfig):api_base = api_base.removesuffix("/v1")- return f"{api_base}/v1/ranking"+ return join_api_base(api_base, "v1/ranking")def map_cohere_rerank_params(self,diff --git a/litellm/llms/nvidia_nim/rerank/transformation.py b/litellm/llms/nvidia_nim/rerank/transformation.pyindex c8e1a331c8c..12377d8f854 100644--- a/litellm/llms/nvidia_nim/rerank/transformation.py+++ b/litellm/llms/nvidia_nim/rerank/transformation.py@@ -8,6 +8,7 @@ from typing_extensions import Required, TypedDictimport litellmfrom litellm._uuid import uuidfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.secret_managers.main import get_secret_str@@ -106,7 +107,7 @@ class NvidiaNimRerankConfig(BaseRerankConfig):# Strip nvidia_nim/ prefix from model name if presentclean_model: Final = self._get_clean_model_name(model)- return f"{api_base}/v1/retrieval/{clean_model}/reranking"+ return join_api_base(api_base, f"v1/retrieval/{clean_model}/reranking")def get_supported_cohere_rerank_params(self, model: str) -> list:"""diff --git a/litellm/llms/oci/chat/transformation.py b/litellm/llms/oci/chat/transformation.pyindex e62e5e63642..8e1502b2610 100644--- a/litellm/llms/oci/chat/transformation.py+++ b/litellm/llms/oci/chat/transformation.py@@ -21,6 +21,7 @@ from litellm._logging import verbose_loggerfrom litellm.constants import DEFAULT_OCI_CHAT_MAX_TOKENSfrom litellm.litellm_core_utils.logging_utils import track_llm_api_timingfrom litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,@@ -538,7 +539,7 @@ class OCIChatConfig(BaseConfig):stream: bool | None = None,) -> str:base: Final = get_oci_base_url(optional_params, api_base or litellm.api_base)- return f"{base}/{OCI_API_VERSION}/actions/chat"+ return join_api_base(base, f"{OCI_API_VERSION}/actions/chat")def _get_optional_params(self, vendor: OCIVendors, optional_params: dict, model: str = "") -> dict:param_map: Final = (diff --git a/litellm/llms/oci/embed/transformation.py b/litellm/llms/oci/embed/transformation.pyindex edadc293771..ffcb156bb16 100644--- a/litellm/llms/oci/embed/transformation.py+++ b/litellm/llms/oci/embed/transformation.py@@ -29,6 +29,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.llms.oci.common_utils import (@@ -156,7 +157,7 @@ class OCIEmbedConfig(BaseEmbeddingConfig):stream: bool | None = None,) -> str:base: Final = get_oci_base_url(optional_params, api_base or litellm.api_base)- return f"{base}/{OCI_API_VERSION}/actions/embedText"+ return join_api_base(base, f"{OCI_API_VERSION}/actions/embedText")def sign_request(self,diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.pyindex 644ab187f15..f9e7071db41 100644--- a/litellm/llms/ollama/chat/transformation.py+++ b/litellm/llms/ollama/chat/transformation.py@@ -14,6 +14,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (extract_images_from_message,extract_reasoning_content,)+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.types.llms.ollama import (@@ -218,10 +219,7 @@ class OllamaChatConfig(BaseConfig):Some providers need `model` in `api_base`"""- clean_api_base: Final = (api_base or "http://localhost:11434").rstrip("/")- if clean_api_base.endswith("/api/chat"):- return clean_api_base- return f"{clean_api_base}/api/chat"+ return ensure_api_route(api_base or "http://localhost:11434", "api/chat")def transform_request(self,diff --git a/litellm/llms/ollama/common_utils.py b/litellm/llms/ollama/common_utils.pyindex 284a6ffea62..7894904c870 100644--- a/litellm/llms/ollama/common_utils.py+++ b/litellm/llms/ollama/common_utils.py@@ -8,6 +8,7 @@ from pydantic import BaseModel, TypeAdapterfrom litellm import verbose_loggerfrom litellm._uuid import uuid+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.utils import ModelResponse, ModelResponseStream@@ -149,7 +150,7 @@ class OllamaModelInfo(BaseLLMModelInfo):names: Final[set[str]] = set()try:- resp: Final = httpx.get(f"{base}/api/tags", headers=headers)+ resp: Final = httpx.get(join_api_base(base, "api/tags"), headers=headers)resp.raise_for_status()data: Final = resp.json()# Expecting a dict with a 'models' list@@ -230,7 +231,7 @@ class OllamaModelInfo(BaseLLMModelInfo):try:response: Final = module_level_client.post(- url=f"{api_base}/api/show",+ url=join_api_base(api_base, "api/show"),json={"name": model},headers=headers,)diff --git a/litellm/llms/ollama/completion/handler.py b/litellm/llms/ollama/completion/handler.pyindex 816028499e8..d4110e55bf0 100644--- a/litellm/llms/ollama/completion/handler.py+++ b/litellm/llms/ollama/completion/handler.py@@ -10,6 +10,7 @@ from typing import Any, Final, Protocol, TypedDictfrom typing_extensions import NotRequired, ReadOnlyimport litellm+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.types.utils import EmbeddingResponse@@ -26,13 +27,6 @@ class OllamaEmbeddingResponse(TypedDict):prompt_eval_count: ReadOnly[NotRequired[int]]-def _ollama_embed_url(api_base: str) -> str:- clean_api_base: Final = api_base.rstrip("/")- if clean_api_base.endswith("/api/embed"):- return clean_api_base- return f"{clean_api_base}/api/embed"--def _prepare_ollama_embedding_payload(model: str, prompts: list[str], optional_params: Mapping[str, object]) -> dict[str, object]:@@ -99,7 +93,7 @@ async def ollama_aembeddings():data: Final = _prepare_ollama_embedding_payload(model, prompts, optional_params)- response: Final = await litellm.module_level_aclient.post(url=_ollama_embed_url(api_base), json=data)+ response: Final = await litellm.module_level_aclient.post(url=ensure_api_route(api_base, "api/embed"), json=data)response_json: Final[OllamaEmbeddingResponse] = response.json()return _process_ollama_embedding_response(@@ -123,7 +117,7 @@ def ollama_embeddings():data: Final = _prepare_ollama_embedding_payload(model, prompts, optional_params)- response: Final = litellm.module_level_client.post(url=_ollama_embed_url(api_base), json=data)+ response: Final = litellm.module_level_client.post(url=ensure_api_route(api_base, "api/embed"), json=data)response_json: Final[OllamaEmbeddingResponse] = response.json()return _process_ollama_embedding_response(diff --git a/litellm/llms/ollama/completion/transformation.py b/litellm/llms/ollama/completion/transformation.pyindex ac41310fb61..30de22b17ff 100644--- a/litellm/llms/ollama/completion/transformation.py+++ b/litellm/llms/ollama/completion/transformation.py@@ -23,6 +23,7 @@ from litellm.litellm_core_utils.prompt_templates.image_handling import (async_inline_remote_media,inline_remote_image_urls,)+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.types.llms.base import LiteLLMBaseModel@@ -500,10 +501,7 @@ class OllamaConfig(BaseConfig):Some providers need `model` in `api_base`"""- clean_api_base: Final = (api_base or "http://localhost:11434").rstrip("/")- if clean_api_base.endswith("/api/generate"):- return clean_api_base- return f"{clean_api_base}/api/generate"+ return ensure_api_route(api_base or "http://localhost:11434", "api/generate")def get_model_response_iterator(self,diff --git a/litellm/llms/oobabooga/chat/oobabooga.py b/litellm/llms/oobabooga/chat/oobabooga.pyindex e21a165d757..8879c20c5eb 100644--- a/litellm/llms/oobabooga/chat/oobabooga.py+++ b/litellm/llms/oobabooga/chat/oobabooga.py@@ -3,6 +3,7 @@ from collections.abc import Callablefrom typing import TYPE_CHECKING, Finalimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import get_httpx_clientfrom litellm.utils import EmbeddingResponse, ModelResponse, Usage@@ -49,7 +50,7 @@ def completion()model = model- completion_url = completion_url + "/v1/chat/completions"+ completion_url = join_api_base(completion_url, "v1/chat/completions")data: Final = oobabooga_config.transform_request(model=model,messages=messages,@@ -103,7 +104,7 @@ def embedding(if model.startswith(("http://", "https://")):embeddings_url = modelelif api_base:- embeddings_url = f"{api_base}/v1/embeddings"+ embeddings_url = join_api_base(api_base, "v1/embeddings")else:raise OobaboogaError(status_code=404,diff --git a/litellm/llms/openai/chat/gpt_transformation.py b/litellm/llms/openai/chat/gpt_transformation.pyindex 4d7ef833181..b4c92848752 100644--- a/litellm/llms/openai/chat/gpt_transformation.py+++ b/litellm/llms/openai/chat/gpt_transformation.py@@ -34,6 +34,7 @@ from litellm.litellm_core_utils.prompt_templates.image_handling import (async_convert_url_to_base64,convert_url_to_base64,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.llms.base_llm.base_utils import BaseLLMModelInfofrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException@@ -807,7 +808,7 @@ class OpenAIGPTConfig(BaseLLMModelInfo, BaseConfig):if endpoint in api_base:return api_base- return f"{api_base}/{endpoint}"+ return join_api_base(api_base, endpoint)def validate_environment(self,diff --git a/litellm/llms/openai/containers/transformation.py b/litellm/llms/openai/containers/transformation.pyindex 6cb2396b85d..5d03585189d 100644--- a/litellm/llms/openai/containers/transformation.py+++ b/litellm/llms/openai/containers/transformation.py@@ -9,7 +9,7 @@ from litellm.litellm_core_utils.hidden_params import get_or_create_hidden_paramsfrom litellm.litellm_core_utils.llm_cost_calc.tool_call_cost_tracking import (StandardBuiltInToolCostTracking,)-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.containers.main import (ContainerCreateOptionalRequestParams,@@ -130,7 +130,7 @@ class OpenAIContainerConfig(BaseContainerConfig):or "https://api.openai.com/v1")- return f"{api_base.rstrip('/')}/containers"+ return join_api_base(api_base, "containers")def transform_container_create_request(self,diff --git a/litellm/llms/openai/evals/transformation.py b/litellm/llms/openai/evals/transformation.pyindex 24de7f6d479..581e5e8ad87 100644--- a/litellm/llms/openai/evals/transformation.py+++ b/litellm/llms/openai/evals/transformation.py@@ -8,7 +8,7 @@ from typing import Finalimport httpxfrom litellm._logging import verbose_logger-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.evals.transformation import (BaseEvalsAPIConfig,LiteLLMLoggingObj,@@ -67,8 +67,8 @@ class OpenAIEvalsConfig(BaseEvalsAPIConfig):if eval_id:encoded_eval_id: Final = encode_url_path_segment(eval_id, field_name="eval_id")- return f"{api_base}/v1/evals/{encoded_eval_id}"- return f"{api_base}/v1/{endpoint}"+ return join_api_base(api_base, f"v1/evals/{encoded_eval_id}")+ return join_api_base(api_base, f"v1/{endpoint}")def transform_create_eval_request(self,@@ -226,7 +226,9 @@ class OpenAIEvalsConfig(BaseEvalsAPIConfig):headers: dict,) -> tuple[str, dict, dict]:"""Transform cancel eval request for OpenAI"""- url: Final = f"{self.get_complete_url(api_base=api_base, endpoint='evals', eval_id=eval_id)}/cancel"+ url: Final = join_api_base(+ self.get_complete_url(api_base=api_base, endpoint="evals", eval_id=eval_id), "cancel"+ )# Empty body for cancel requestrequest_body: Final[dict[str, object]] = {}@@ -260,7 +262,7 @@ class OpenAIEvalsConfig(BaseEvalsAPIConfig):api_base = litellm_params.api_baseencoded_eval_id: Final = encode_url_path_segment(eval_id, field_name="eval_id")- url: Final = f"{api_base}/v1/evals/{encoded_eval_id}/runs"+ url: Final = join_api_base(api_base, f"v1/evals/{encoded_eval_id}/runs")# Build request bodyrequest_body: Final = {k: v for k, v in create_request.items() if v is not None}@@ -293,7 +295,7 @@ class OpenAIEvalsConfig(BaseEvalsAPIConfig):api_base = litellm_params.api_baseencoded_eval_id: Final = encode_url_path_segment(eval_id, field_name="eval_id")- url: Final = f"{api_base}/v1/evals/{encoded_eval_id}/runs"+ url: Final = join_api_base(api_base, f"v1/evals/{encoded_eval_id}/runs")# Build query parametersquery_params: Final[dict[str, object]] = {}@@ -335,7 +337,7 @@ class OpenAIEvalsConfig(BaseEvalsAPIConfig):"""Transform get run request for OpenAI"""encoded_eval_id: Final = encode_url_path_segment(eval_id, field_name="eval_id")encoded_run_id: Final = encode_url_path_segment(run_id, field_name="run_id")- url: Final = f"{api_base}/v1/evals/{encoded_eval_id}/runs/{encoded_run_id}"+ url: Final = join_api_base(api_base, f"v1/evals/{encoded_eval_id}/runs/{encoded_run_id}")verbose_logger.debug("Get run request - URL: %s", url)@@ -363,7 +365,7 @@ class OpenAIEvalsConfig(BaseEvalsAPIConfig):"""Transform cancel run request for OpenAI"""encoded_eval_id: Final = encode_url_path_segment(eval_id, field_name="eval_id")encoded_run_id: Final = encode_url_path_segment(run_id, field_name="run_id")- url: Final = f"{api_base}/v1/evals/{encoded_eval_id}/runs/{encoded_run_id}/cancel"+ url: Final = join_api_base(api_base, f"v1/evals/{encoded_eval_id}/runs/{encoded_run_id}/cancel")# Empty body for cancel requestrequest_body: Final[dict[str, object]] = {}@@ -394,7 +396,7 @@ class OpenAIEvalsConfig(BaseEvalsAPIConfig):"""Transform delete run request for OpenAI"""encoded_eval_id: Final = encode_url_path_segment(eval_id, field_name="eval_id")encoded_run_id: Final = encode_url_path_segment(run_id, field_name="run_id")- url: Final = f"{api_base}/v1/evals/{encoded_eval_id}/runs/{encoded_run_id}"+ url: Final = join_api_base(api_base, f"v1/evals/{encoded_eval_id}/runs/{encoded_run_id}")# Empty body for delete requestrequest_body: Final[dict[str, object]] = {}diff --git a/litellm/llms/openai/image_edit/transformation.py b/litellm/llms/openai/image_edit/transformation.pyindex a9e5b1b9f34..9def833052b 100644--- a/litellm/llms/openai/image_edit/transformation.py+++ b/litellm/llms/openai/image_edit/transformation.py@@ -6,6 +6,7 @@ from httpx._types import RequestFilesimport litellmfrom litellm.images.utils import ImageEditRequestUtils+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.images.main import (@@ -184,7 +185,4 @@ class OpenAIImageEditConfig(BaseImageEditConfig):or "https://api.openai.com/v1")- # Remove trailing slashes- api_base = api_base.rstrip("/")-- return f"{api_base}/images/edits"+ return join_api_base(api_base, "images/edits")diff --git a/litellm/llms/openai/realtime/http_transformation.py b/litellm/llms/openai/realtime/http_transformation.pyindex 61dbf20397f..49d33582c6f 100644--- a/litellm/llms/openai/realtime/http_transformation.py+++ b/litellm/llms/openai/realtime/http_transformation.py@@ -1,6 +1,7 @@"""OpenAI realtime HTTP transformation config (client_secrets + realtime_calls)."""import litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.realtime.http_transformation import BaseRealtimeHTTPConfigfrom litellm.secret_managers.main import get_secret_str@@ -15,17 +16,17 @@ class OpenAIRealtimeHTTPConfig(BaseRealtimeHTTPConfig):def get_complete_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:base = self.get_api_base(api_base).rstrip("/")base = base.removesuffix("/v1")- return f"{base}/v1/realtime/client_secrets"+ return join_api_base(base, "v1/realtime/client_secrets")def get_realtime_calls_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:base = self.get_api_base(api_base).rstrip("/")base = base.removesuffix("/v1")- return f"{base}/v1/realtime/calls"+ return join_api_base(base, "v1/realtime/calls")def get_transcription_session_url(self, api_base: str | None, model: str, api_version: str | None = None) -> str:base = self.get_api_base(api_base).rstrip("/")base = base.removesuffix("/v1")- return f"{base}/v1/realtime/transcription_sessions"+ return join_api_base(base, "v1/realtime/transcription_sessions")def validate_environment(self,diff --git a/litellm/llms/openai/responses/count_tokens/transformation.py b/litellm/llms/openai/responses/count_tokens/transformation.pyindex 6d882e0eeb1..c1ee42444c5 100644--- a/litellm/llms/openai/responses/count_tokens/transformation.py+++ b/litellm/llms/openai/responses/count_tokens/transformation.py@@ -13,6 +13,7 @@ from pydantic import JsonValue, TypeAdapter, ValidationErrorfrom typing_extensions import ReadOnly, TypedDictfrom litellm.litellm_core_utils.tool_fields import DropPolicy+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.pass_through.convertible_blocks import malformed_tool_result_text_errorfrom litellm.llms.anthropic.pass_through.messages.utils import normalize_native_messagesfrom litellm.llms.anthropic.pass_through.responses_adapters.transformation import (@@ -148,9 +149,8 @@ class OpenAICountTokensConfig:"""def get_openai_count_tokens_endpoint(self, api_base: str | None = None) -> str:- base = api_base or "https://api.openai.com/v1"- base = base.rstrip("/")- return f"{base}/responses/input_tokens"+ base: Final = api_base or "https://api.openai.com/v1"+ return join_api_base(base, "responses/input_tokens")def transform_request_to_count_tokens(self,diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.pyindex 8f29a71d63b..b90f6aa01da 100644--- a/litellm/llms/openai/responses/transformation.py+++ b/litellm/llms/openai/responses/transformation.py@@ -23,7 +23,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (with_prompt_cache_breakpoint,)from litellm.litellm_core_utils.safe_json_loads import safe_json_loads-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.responses.transformation import BaseResponsesAPIConfigfrom litellm.llms.openai.chat.gpt_5_transformation import (is_gpt_reasoning_series_name,@@ -881,10 +881,7 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig):or "https://api.openai.com/v1")- # Remove trailing slashes- api_base = api_base.rstrip("/")-- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")def transform_streaming_response(self,@@ -1111,7 +1108,7 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig):order: Literal["asc", "desc"] = "desc",) -> tuple[str, dict]:encoded_response_id: Final = encode_url_path_segment(response_id, field_name="response_id")- url: Final = f"{api_base}/{encoded_response_id}/input_items"+ url: Final = join_api_base(api_base, f"{encoded_response_id}/input_items")params: Final[dict[str, object]] = {}if after is not None:params["after"] = after@@ -1152,7 +1149,7 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig):- POST /v1/responses/{response_id}/cancel"""encoded_response_id: Final = encode_url_path_segment(response_id, field_name="response_id")- url: Final = f"{api_base}/{encoded_response_id}/cancel"+ url: Final = join_api_base(api_base, f"{encoded_response_id}/cancel")data: Final[dict] = {}return url, datadiff --git a/litellm/llms/openai/transcriptions/whisper_transformation.py b/litellm/llms/openai/transcriptions/whisper_transformation.pyindex 8f96f688189..eb4f1b6c9ce 100644--- a/litellm/llms/openai/transcriptions/whisper_transformation.py+++ b/litellm/llms/openai/transcriptions/whisper_transformation.py@@ -4,6 +4,7 @@ from typing import Finalfrom httpx import Headers, Response+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -46,9 +47,9 @@ class OpenAIWhisperAudioTranscriptionConfig(BaseAudioTranscriptionConfig):# if endswith "/v1"if api_base and api_base.endswith("/v1"):- api_base = f"{api_base}/audio/transcriptions"+ api_base = join_api_base(api_base, "audio/transcriptions")else:- api_base = f"{api_base}/v1/audio/transcriptions"+ api_base = join_api_base(api_base, "v1/audio/transcriptions")return api_base or ""diff --git a/litellm/llms/openai/vector_store_files/transformation.py b/litellm/llms/openai/vector_store_files/transformation.pyindex 7f65ade224e..bd8145bb460 100644--- a/litellm/llms/openai/vector_store_files/transformation.py+++ b/litellm/llms/openai/vector_store_files/transformation.py@@ -4,7 +4,7 @@ from typing import Final, castimport httpximport litellm-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.vector_store_files.transformation import (BaseVectorStoreFilesConfig,)@@ -92,16 +92,15 @@ class OpenAIVectorStoreFilesConfig(BaseVectorStoreFilesConfig):vector_store_id: str,litellm_params: Mapping[str, object],) -> str:- base_url = (+ base_url: Final = (api_baseor litellm.api_baseor get_secret_str("OPENAI_BASE_URL")or get_secret_str("OPENAI_API_BASE")or "https://api.openai.com/v1")- base_url = base_url.rstrip("/")encoded_vector_store_id: Final = encode_url_path_segment(vector_store_id, field_name="vector_store_id")- return f"{base_url}/vector_stores/{encoded_vector_store_id}/files"+ return join_api_base(base_url, f"vector_stores/{encoded_vector_store_id}/files")def transform_create_vector_store_file_request(self,@@ -187,7 +186,7 @@ class OpenAIVectorStoreFilesConfig(BaseVectorStoreFilesConfig):api_base: str,) -> tuple[str, dict[str, object]]:encoded_file_id: Final = encode_url_path_segment(file_id, field_name="file_id")- return f"{api_base}/{encoded_file_id}/content", {}+ return join_api_base(api_base, f"{encoded_file_id}/content"), {}def transform_retrieve_vector_store_file_content_response(self,diff --git a/litellm/llms/openai/vector_stores/transformation.py b/litellm/llms/openai/vector_stores/transformation.pyindex 46105a66be3..d108802f7f9 100644--- a/litellm/llms/openai/vector_stores/transformation.py+++ b/litellm/llms/openai/vector_stores/transformation.py@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Any, Final, castimport httpximport litellm-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.vector_store.transformation import BaseVectorStoreConfigfrom litellm.llms.openai.workload_identity import resolve_openai_bearer_tokenfrom litellm.secret_managers.main import get_secret_str@@ -88,10 +88,7 @@ class OpenAIVectorStoreConfig(BaseVectorStoreConfig):or "https://api.openai.com/v1")- # Remove trailing slashes- api_base = api_base.rstrip("/")-- return f"{api_base}/vector_stores"+ return join_api_base(api_base, "vector_stores")def transform_search_vector_store_request(self,@@ -105,7 +102,7 @@ class OpenAIVectorStoreConfig(BaseVectorStoreConfig):) -> tuple[str, dict]:encoded_vector_store_id: Final = encode_url_path_segment(vector_store_id, field_name="vector_store_id")base_url, query_separator, query_string = api_base.partition("?")- url: Final = f"{base_url}/{encoded_vector_store_id}/search{query_separator}{query_string}"+ url: Final = f"{join_api_base(base_url, f'{encoded_vector_store_id}/search')}{query_separator}{query_string}"typed_request_body: Final = VectorStoreSearchRequest(query=query,filters=vector_store_search_optional_params.get("filters", None),diff --git a/litellm/llms/openai/videos/transformation.py b/litellm/llms/openai/videos/transformation.pyindex d422e51f649..0560e1c2e3a 100644--- a/litellm/llms/openai/videos/transformation.py+++ b/litellm/llms/openai/videos/transformation.py@@ -8,7 +8,7 @@ from urllib.parse import quoteimport httpxfrom httpx._types import FileContent, FileTypes, RequestFiles-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.videos.transformation import BaseVideoConfigfrom litellm.llms.openai.image_edit.transformation import ImageEditRequestUtilsfrom litellm.llms.openai.workload_identity import resolve_openai_bearer_token@@ -98,7 +98,7 @@ class OpenAIVideoConfig(BaseVideoConfig):if api_base is None:api_base = "https://api.openai.com/v1"- return f"{api_base.rstrip('/')}/videos"+ return join_api_base(api_base, "videos")def use_multipart_form_data(self) -> bool:return True@@ -214,7 +214,7 @@ class OpenAIVideoConfig(BaseVideoConfig):encoded_video_id: Final = encode_url_path_segment(original_video_id, field_name="video_id")# Construct the URL for video content download- url = f"{api_base.rstrip('/')}/{encoded_video_id}/content"+ url = join_api_base(api_base, f"{encoded_video_id}/content")if variant is not None:# Encode the user-controlled ``variant`` so a value like# ``thumbnail&extra=1`` cannot inject additional query params@@ -246,7 +246,7 @@ class OpenAIVideoConfig(BaseVideoConfig):encoded_video_id: Final = encode_url_path_segment(original_video_id, field_name="video_id")# Construct the URL for video remix- url: Final = f"{api_base.rstrip('/')}/{encoded_video_id}/remix"+ url: Final = join_api_base(api_base, f"{encoded_video_id}/remix")# Prepare the request datadata: Final[dict[str, object]] = {"prompt": prompt}@@ -465,7 +465,7 @@ class OpenAIVideoConfig(BaseVideoConfig):litellm_params: GenericLiteLLMParams,headers: dict,) -> tuple[str, list]:- url: Final = f"{api_base.rstrip('/')}/characters"+ url: Final = join_api_base(api_base, "characters")files_list: Final[list[tuple[str, FileTypes]]] = [("name", (None, name))]self._add_video_to_files(files_list, video, "video")return url, files_list@@ -486,7 +486,7 @@ class OpenAIVideoConfig(BaseVideoConfig):) -> tuple[str, dict]:original_character_id: Final = extract_original_character_id(character_id)encoded_character_id: Final = encode_url_path_segment(original_character_id, field_name="character_id")- url: Final = f"{api_base.rstrip('/')}/characters/{encoded_character_id}"+ url: Final = join_api_base(api_base, f"characters/{encoded_character_id}")return url, {}def transform_video_get_character_response(@@ -507,7 +507,7 @@ class OpenAIVideoConfig(BaseVideoConfig):extra_body: dict[str, object] | None = None,prefetched_source_data: dict[str, object] | None = None,) -> tuple[str, Mapping[str, object], RequestFiles | None]:- url: Final = f"{api_base.rstrip('/')}/edits"+ url: Final = join_api_base(api_base, "edits")if video_file is not None:files: Final[RequestFiles] = (self._video_file_tuple(video_file, "video"),)@@ -547,7 +547,7 @@ class OpenAIVideoConfig(BaseVideoConfig):extra_body: dict[str, object] | None = None,) -> tuple[str, dict]:original_video_id: Final = extract_original_video_id(video_id)- url: Final = f"{api_base.rstrip('/')}/extensions"+ url: Final = join_api_base(api_base, "extensions")data: Final[dict[str, object]] = {"prompt": prompt,"seconds": seconds,diff --git a/litellm/llms/openai_like/common_utils.py b/litellm/llms/openai_like/common_utils.pyindex 11b85e52af5..5d3085a3901 100644--- a/litellm/llms/openai_like/common_utils.py+++ b/litellm/llms/openai_like/common_utils.py@@ -2,6 +2,8 @@ from typing import Literalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_base+class OpenAILikeError(Exception):def __init__(self, status_code, message):@@ -48,7 +50,7 @@ class OpenAILikeBase:if not custom_endpoint:if endpoint_type == "chat_completions":- api_base = f"{api_base}/chat/completions"+ api_base = join_api_base(api_base, "chat/completions")elif endpoint_type == "embeddings":- api_base = f"{api_base}/embeddings"+ api_base = join_api_base(api_base, "embeddings")return api_base, headersdiff --git a/litellm/llms/openai_like/dynamic_config.py b/litellm/llms/openai_like/dynamic_config.pyindex f0bf710b951..685561f55a5 100644--- a/litellm/llms/openai_like/dynamic_config.py+++ b/litellm/llms/openai_like/dynamic_config.py@@ -9,6 +9,7 @@ from litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.prompt_templates.common_utils import (handle_messages_with_content_list_to_str_conversion,)+from litellm.litellm_core_utils.url_utils import ensure_api_route, join_api_basefrom litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfigfrom litellm.llms.openai_like.chat.transformation import OpenAILikeChatConfigfrom litellm.secret_managers.main import get_secret_str@@ -91,13 +92,9 @@ def create_config_class(provider: SimpleProviderConfig):if not api_base:api_base = provider.base_url- if api_base is None:- raise ValueError(f"api_base is required for provider {provider.slug}")-- if not api_base.endswith("/chat/completions"):- api_base = f"{api_base}/chat/completions"-- return api_base+ if isinstance(api_base, str):+ return ensure_api_route(api_base, "chat/completions")+ raise ValueError(f"api_base is required for provider {provider.slug}")def get_supported_openai_params(self, model: str) -> list:"""Get supported OpenAI params, excluding tool-related params for models@@ -226,11 +223,9 @@ def create_responses_config_class(provider: SimpleProviderConfig) -> "type[OpenAif not api_base:api_base = provider.base_url- if api_base is None:- raise ValueError(f"api_base is required for provider {provider.slug}")-- api_base = api_base.rstrip("/")- return f"{api_base}/responses"+ if isinstance(api_base, str):+ return join_api_base(api_base, "responses")+ raise ValueError(f"api_base is required for provider {provider.slug}")def transform_responses_api_request(self,diff --git a/litellm/llms/openai_like/messages/transformation.py b/litellm/llms/openai_like/messages/transformation.pyindex ef501720e2d..12c8b1da166 100644--- a/litellm/llms/openai_like/messages/transformation.py+++ b/litellm/llms/openai_like/messages/transformation.py@@ -1,6 +1,7 @@from typing import Any, Finalimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.common_utils import normalize_cache_control_in_anthropic_payloadfrom litellm.llms.anthropic.pass_through.messages.transformation import (AnthropicMessagesConfig,@@ -113,7 +114,7 @@ class OpenAILikeAnthropicMessagesConfig(AnthropicMessagesConfig):if base.endswith("/v1/messages"):return basebase = base.removesuffix("/v1")- return f"{base}/v1/messages"+ return join_api_base(base, "v1/messages")class JSONProviderAnthropicMessagesConfig(OpenAILikeAnthropicMessagesConfig):diff --git a/litellm/llms/openai_like/responses/transformation.py b/litellm/llms/openai_like/responses/transformation.pyindex 9655772e953..68de4065b09 100644--- a/litellm/llms/openai_like/responses/transformation.py+++ b/litellm/llms/openai_like/responses/transformation.py@@ -8,6 +8,7 @@ provider-specific resolution (slug, API key env var, base URL).from typing import Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.router import GenericLiteLLMParams@@ -47,5 +48,4 @@ class OpenAILikeResponsesConfig(OpenAIResponsesAPIConfig):api_base = api_base or get_secret_str("OPENAI_LIKE_API_BASE")if not api_base:raise ValueError("api_base is required for openai_like provider")- api_base = api_base.rstrip("/")- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")diff --git a/litellm/llms/openrouter/embedding/transformation.py b/litellm/llms/openrouter/embedding/transformation.pyindex 14b0e462ea7..d1530a29515 100644--- a/litellm/llms/openrouter/embedding/transformation.py+++ b/litellm/llms/openrouter/embedding/transformation.py@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.types.llms.openai import AllEmbeddingInputValuesfrom litellm.types.utils import EmbeddingResponse@@ -84,15 +85,7 @@ class OpenrouterEmbeddingConfig(BaseEmbeddingConfig):"""Get the complete URL for OpenRouter Embedding API endpoint."""- # api_base is already set to https://openrouter.ai/api/v1 in main.py- # Remove trailing slashes- if api_base:- api_base = api_base.rstrip("/")- else:- api_base = "https://openrouter.ai/api/v1"-- # Return the embeddings endpoint- return f"{api_base}/embeddings"+ return join_api_base(api_base or "https://openrouter.ai/api/v1", "embeddings")def transform_embedding_request(self,diff --git a/litellm/llms/openrouter/image_edit/transformation.py b/litellm/llms/openrouter/image_edit/transformation.pyindex a73a2fe4192..9644ee3f280 100644--- a/litellm/llms/openrouter/image_edit/transformation.py+++ b/litellm/llms/openrouter/image_edit/transformation.py@@ -52,6 +52,7 @@ from typing_extensions import NotRequired, ReadOnly, TypedDictimport litellmfrom litellm.images.utils import ImageEditRequestUtils+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfigfrom litellm.llms.openrouter.common_utils import OpenRouterException@@ -187,11 +188,8 @@ class OpenRouterImageEditConfig(BaseImageEditConfig):api_base: str | None,litellm_params: dict,) -> str:- base_url = api_base or get_secret_str("OPENROUTER_API_BASE") or "https://openrouter.ai/api/v1"- base_url = base_url.rstrip("/")- if not base_url.endswith("/chat/completions"):- return f"{base_url}/chat/completions"- return base_url+ base_url: Final = api_base or get_secret_str("OPENROUTER_API_BASE") or "https://openrouter.ai/api/v1"+ return ensure_api_route(base_url, "chat/completions")def transform_image_edit_request(self,diff --git a/litellm/llms/openrouter/image_generation/transformation.py b/litellm/llms/openrouter/image_generation/transformation.pyindex 355df81b87a..ce6e62aaa88 100644--- a/litellm/llms/openrouter/image_generation/transformation.py+++ b/litellm/llms/openrouter/image_generation/transformation.py@@ -35,6 +35,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR, set_hidden_params+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,@@ -269,10 +270,7 @@ class OpenRouterImageGenerationConfig(BaseImageGenerationConfig):Default: https://openrouter.ai/api/v1/chat/completions"""if api_base:- if not api_base.endswith("/chat/completions"):- api_base = api_base.rstrip("/")- return f"{api_base}/chat/completions"- return api_base+ return ensure_api_route(api_base, "chat/completions")return "https://openrouter.ai/api/v1/chat/completions"diff --git a/litellm/llms/openrouter/responses/transformation.py b/litellm/llms/openrouter/responses/transformation.pyindex aeb5b09e14a..e166ccaad2d 100644--- a/litellm/llms/openrouter/responses/transformation.py+++ b/litellm/llms/openrouter/responses/transformation.py@@ -18,6 +18,7 @@ import litellmfrom litellm.litellm_core_utils.core_helpers import RESPONSE_COST_HEADERfrom litellm.litellm_core_utils.hidden_params import set_hidden_paramsfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import (@@ -138,9 +139,7 @@ class OpenRouterResponsesAPIConfig(OpenAIResponsesAPIConfig):api_base or litellm.api_base or get_secret_str("OPENROUTER_API_BASE") or "https://openrouter.ai/api/v1")- api_base = api_base.rstrip("/")-- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")def supports_native_websocket(self) -> bool:"""OpenRouter does not support native WebSocket for Responses API"""diff --git a/litellm/llms/opensandbox/sandbox/transformation.py b/litellm/llms/opensandbox/sandbox/transformation.pyindex 13c9d805638..574fbfa9e5b 100644--- a/litellm/llms/opensandbox/sandbox/transformation.py+++ b/litellm/llms/opensandbox/sandbox/transformation.py@@ -21,6 +21,7 @@ from litellm.constants import (OPEN_SANDBOX_READY_TIMEOUT,)from litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR, set_hidden_params+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.sandbox.transformation import (SANDBOX_MAX_OUTPUT_BYTES,BaseSandboxConfig,@@ -93,7 +94,7 @@ class OpenSandboxSandboxConfig(BaseSandboxConfig):)response: Final = await self._http(client).post(- url=f"{base}/sandboxes",+ url=join_api_base(base, "sandboxes"),headers=self._lifecycle_headers(key),json=body,)@@ -192,7 +193,7 @@ class OpenSandboxSandboxConfig(BaseSandboxConfig):key: Final = self._api_key(api_key=api_key, handle=handle)try:response: Final = await self._http(client).delete(- url=f"{base}/sandboxes/{handle.id}",+ url=join_api_base(base, f"sandboxes/{handle.id}"),headers=self._lifecycle_headers(key),)except httpx.HTTPStatusError as e:@@ -258,7 +259,7 @@ class OpenSandboxSandboxConfig(BaseSandboxConfig):deadline: Final = time.monotonic() + ready_timeoutwhile True:response = await self._http(client).get(- url=f"{api_base}/sandboxes/{sandbox_id}",+ url=join_api_base(api_base, f"sandboxes/{sandbox_id}"),headers=headers,)data = _JSON_VALUE.validate_python(response.json())@@ -316,7 +317,7 @@ class OpenSandboxSandboxConfig(BaseSandboxConfig):client: AsyncHTTPHandler | None,) -> tuple[str, dict[str, str]]:response: Final = await self._http(client).get(- url=f"{api_base}/sandboxes/{sandbox_id}/endpoints/{OPEN_SANDBOX_EXECD_PORT}",+ url=join_api_base(api_base, f"sandboxes/{sandbox_id}/endpoints/{OPEN_SANDBOX_EXECD_PORT}"),headers=headers,params={"use_server_proxy": use_server_proxy},)diff --git a/litellm/llms/ovhcloud/audio_transcription/transformation.py b/litellm/llms/ovhcloud/audio_transcription/transformation.pyindex 3ae5f6fa69c..dd61bfb0c13 100644--- a/litellm/llms/ovhcloud/audio_transcription/transformation.py+++ b/litellm/llms/ovhcloud/audio_transcription/transformation.py@@ -12,6 +12,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -63,9 +64,8 @@ class OVHCloudAudioTranscriptionConfig(BaseAudioTranscriptionConfig):litellm_params: dict,stream: bool | None = None,) -> str:- api_base = "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1" if api_base is None else api_base.rstrip("/")- complete_url: Final = f"{api_base}/audio/transcriptions"- return complete_url+ base_url: Final = "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1" if api_base is None else api_base+ return join_api_base(base_url, "audio/transcriptions")def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:return OVHCloudException(diff --git a/litellm/llms/ovhcloud/chat/transformation.py b/litellm/llms/ovhcloud/chat/transformation.pyindex 3d3953eed34..91b3dbe6268 100644--- a/litellm/llms/ovhcloud/chat/transformation.py+++ b/litellm/llms/ovhcloud/chat/transformation.py@@ -9,6 +9,7 @@ from typing import Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfig@@ -31,9 +32,8 @@ class OVHCloudChatConfig(OpenAIGPTConfig):litellm_params: dict,stream: bool | None = None,) -> str:- api_base = "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1" if api_base is None else api_base.rstrip("/")- complete_url: Final = f"{api_base}/chat/completions"- return complete_url+ base_url: Final = "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1" if api_base is None else api_base+ return join_api_base(base_url, "chat/completions")def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:return OVHCloudException(diff --git a/litellm/llms/ovhcloud/embedding/transformation.py b/litellm/llms/ovhcloud/embedding/transformation.pyindex cdf095e6ad1..487c40afa52 100644--- a/litellm/llms/ovhcloud/embedding/transformation.py+++ b/litellm/llms/ovhcloud/embedding/transformation.py@@ -10,6 +10,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -35,9 +36,8 @@ class OVHCloudEmbeddingConfig(BaseEmbeddingConfig):litellm_params: dict,stream: bool | None = None,) -> str:- api_base = "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1" if api_base is None else api_base.rstrip("/")- complete_url: Final = f"{api_base}/embeddings"- return complete_url+ base_url: Final = "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1" if api_base is None else api_base+ return join_api_base(base_url, "embeddings")def validate_environment(self,diff --git a/litellm/llms/perplexity/embedding/transformation.py b/litellm/llms/perplexity/embedding/transformation.pyindex 945a58d4d75..7b4da933976 100644--- a/litellm/llms/perplexity/embedding/transformation.py+++ b/litellm/llms/perplexity/embedding/transformation.py@@ -20,6 +20,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -68,7 +69,7 @@ class PerplexityEmbeddingConfig(BaseEmbeddingConfig):if api_base:clean_api_base: Final = api_base.rstrip("/")if not clean_api_base.endswith("/embeddings"):- return f"{clean_api_base}/v1/embeddings"+ return join_api_base(clean_api_base, "v1/embeddings")return clean_api_basereturn "https://api.perplexity.ai/v1/embeddings"diff --git a/litellm/llms/perplexity/responses/transformation.py b/litellm/llms/perplexity/responses/transformation.pyindex 91d40d8247f..a2b7eceea40 100644--- a/litellm/llms/perplexity/responses/transformation.py+++ b/litellm/llms/perplexity/responses/transformation.py@@ -14,6 +14,7 @@ from typing import Any, Finalimport httpxfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.secret_managers.main import get_secret_str@@ -51,7 +52,7 @@ class PerplexityResponsesConfig(OpenAIResponsesAPIConfig):def get_complete_url(self, api_base: str | None, litellm_params: dict) -> str:api_base = api_base or get_secret_str("PERPLEXITY_API_BASE") or "https://api.perplexity.ai"- return f"{api_base.rstrip('/')}/v1/responses"+ return join_api_base(api_base, "v1/responses")def _ensure_message_type(self, input: str | ResponseInputParam) -> str | ResponseInputParam:"""Ensure list input items have type='message' (required by Perplexity)."""diff --git a/litellm/llms/perplexity/search/transformation.py b/litellm/llms/perplexity/search/transformation.pyindex b46e4225309..1f3b0fe729d 100644--- a/litellm/llms/perplexity/search/transformation.py+++ b/litellm/llms/perplexity/search/transformation.py@@ -9,6 +9,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -78,11 +79,7 @@ class PerplexitySearchConfig(BaseSearchConfig):"""Get complete URL for Search endpoint."""- resolved_api_base: Final = api_base or get_secret_str("PERPLEXITY_API_BASE") or self.PERPLEXITY_API_BASE- clean_api_base: Final = resolved_api_base.rstrip("/")- if clean_api_base.endswith("/search"):- return clean_api_base- return f"{clean_api_base}/search"+ return ensure_api_route(api_base or get_secret_str("PERPLEXITY_API_BASE") or self.PERPLEXITY_API_BASE, "search")def transform_search_request(self,diff --git a/litellm/llms/pg_vector/vector_stores/transformation.py b/litellm/llms/pg_vector/vector_stores/transformation.pyindex e4b06c36bf4..eeecd02653d 100644--- a/litellm/llms/pg_vector/vector_stores/transformation.py+++ b/litellm/llms/pg_vector/vector_stores/transformation.py@@ -1,6 +1,6 @@from typing import TYPE_CHECKING, Any, Final-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.openai.vector_stores.transformation import OpenAIVectorStoreConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.router import GenericLiteLLMParams@@ -66,10 +66,7 @@ class PGVectorStoreConfig(OpenAIVectorStoreConfig):"PG Vector API base URL is required. Set PG_VECTOR_API_BASE environment variable or pass api_base in litellm_params.")- # Remove trailing slashes- api_base = api_base.rstrip("/")-- return f"{api_base}/v1/vector_stores"+ return join_api_base(api_base, "v1/vector_stores")def transform_search_vector_store_request(self,@@ -82,7 +79,7 @@ class PGVectorStoreConfig(OpenAIVectorStoreConfig):extra_body: dict[str, Any] | None = None,) -> tuple[str, dict]:encoded_vector_store_id: Final = encode_url_path_segment(vector_store_id, field_name="vector_store_id")- url: Final = f"{api_base}/{encoded_vector_store_id}/search"+ url: Final = join_api_base(api_base, f"{encoded_vector_store_id}/search")_, request_body = super().transform_search_vector_store_request(vector_store_id=vector_store_id,query=query,diff --git a/litellm/llms/predibase/chat/transformation.py b/litellm/llms/predibase/chat/transformation.pyindex 0dd7e27722e..b30713fa119 100644--- a/litellm/llms/predibase/chat/transformation.py+++ b/litellm/llms/predibase/chat/transformation.py@@ -13,6 +13,7 @@ from litellm.litellm_core_utils.prompt_templates.factory import (prompt_factory,render_custom_prompt_template,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.utils import Choices, Message, ModelResponse, Usage@@ -337,12 +338,12 @@ class PredibaseConfig(BaseConfig):elif "PREDIBASE_API_BASE" in os.environ:base_url = os.getenv("PREDIBASE_API_BASE", "")- completion_url = f"{base_url}/{tenant_id}/deployments/v2/llms/{model}"+ completion_url = join_api_base(base_url, f"{tenant_id}/deployments/v2/llms/{model}")should_stream: Final = stream if stream is not None else optional_params.get("stream", False)if should_stream is True:- completion_url += "/generate_stream"+ completion_url = join_api_base(completion_url, "generate_stream")else:- completion_url += "/generate"+ completion_url = join_api_base(completion_url, "generate")return completion_urldef get_error_class(self, error_message: str, status_code: int, headers: dict | Headers) -> BaseLLMException:diff --git a/litellm/llms/ragflow/chat/transformation.py b/litellm/llms/ragflow/chat/transformation.pyindex 97b723f7dd5..f626cd8e55f 100644--- a/litellm/llms/ragflow/chat/transformation.py+++ b/litellm/llms/ragflow/chat/transformation.py@@ -13,7 +13,7 @@ Model name format:from typing import Finalimport litellm-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.openai.openai import OpenAIConfigfrom litellm.secret_managers.main import get_secret, get_secret_strfrom litellm.types.llms.openai import AllMessageValues@@ -122,7 +122,7 @@ class RAGFlowConfig(OpenAIConfig):if not path.startswith("/"):path = "/" + path- return f"{api_base}{path}"+ return join_api_base(api_base, path)def _get_openai_compatible_provider_info(self,diff --git a/litellm/llms/ragflow/vector_stores/transformation.py b/litellm/llms/ragflow/vector_stores/transformation.pyindex 007b047cc1b..ef44b6e4c0f 100644--- a/litellm/llms/ragflow/vector_stores/transformation.py+++ b/litellm/llms/ragflow/vector_stores/transformation.py@@ -5,6 +5,7 @@ import httpxfrom pydantic import ConfigDict, RootModel, with_configfrom typing_extensions import NotRequired, ReadOnly, TypedDict+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.vector_store.transformation import BaseVectorStoreConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.router import GenericLiteLLMParams@@ -95,15 +96,16 @@ class RAGFlowVectorStoreConfig(BaseVectorStoreConfig):- api_base in litellm_params- Default: http://localhost:9380"""- api_base = (- api_base or litellm_params.get("api_base") or get_secret_str("RAGFLOW_API_BASE") or "http://localhost:9380"+ params_api_base: Final = litellm_params.get("api_base")+ resolved_api_base: Final = (+ api_base+ or (f"{params_api_base}" if params_api_base else None)+ or get_secret_str("RAGFLOW_API_BASE")+ or "http://localhost:9380")- # Remove trailing slashes- api_base = api_base.rstrip("/")-# RAGFlow datasets API endpoint- return f"{api_base}/api/v1/datasets"+ return join_api_base(resolved_api_base, "api/v1/datasets")def transform_search_vector_store_request(self,diff --git a/litellm/llms/recraft/image_edit/transformation.py b/litellm/llms/recraft/image_edit/transformation.pyindex 46905580382..74bfa52c5e0 100644--- a/litellm/llms/recraft/image_edit/transformation.py+++ b/litellm/llms/recraft/image_edit/transformation.py@@ -5,6 +5,7 @@ import httpxfrom httpx._types import RequestFilesfrom litellm.images.utils import ImageEditRequestUtils+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.images.main import ImageEditOptionalRequestParams@@ -68,11 +69,8 @@ class RecraftImageEditConfig(BaseImageEditConfig):Some providers need `model` in `api_base`"""- complete_url: str = api_base or get_secret_str("RECRAFT_API_BASE") or self.DEFAULT_BASE_URL-- complete_url = complete_url.rstrip("/")- complete_url = f"{complete_url}/{self.IMAGE_EDIT_ENDPOINT}"- return complete_url+ base_url: Final = api_base or get_secret_str("RECRAFT_API_BASE") or self.DEFAULT_BASE_URL+ return join_api_base(base_url, self.IMAGE_EDIT_ENDPOINT)def validate_environment(self,diff --git a/litellm/llms/recraft/image_generation/transformation.py b/litellm/llms/recraft/image_generation/transformation.pyindex 0fcfafb79fb..7b1a7714ec6 100644--- a/litellm/llms/recraft/image_generation/transformation.py+++ b/litellm/llms/recraft/image_generation/transformation.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -72,11 +73,8 @@ class RecraftImageGenerationConfig(BaseImageGenerationConfig):Some providers need `model` in `api_base`"""- complete_url: str = api_base or get_secret_str("RECRAFT_API_BASE") or self.DEFAULT_BASE_URL-- complete_url = complete_url.rstrip("/")- complete_url = f"{complete_url}/{self.IMAGE_GENERATION_ENDPOINT}"- return complete_url+ base_url: Final = api_base or get_secret_str("RECRAFT_API_BASE") or self.DEFAULT_BASE_URL+ return join_api_base(base_url, self.IMAGE_GENERATION_ENDPOINT)def validate_environment(self,diff --git a/litellm/llms/runwayml/image_generation/transformation.py b/litellm/llms/runwayml/image_generation/transformation.pyindex ddc1b9474ac..e721ac678a1 100644--- a/litellm/llms/runwayml/image_generation/transformation.py+++ b/litellm/llms/runwayml/image_generation/transformation.py@@ -11,6 +11,7 @@ from litellm.constants import (RUNWAYML_DEFAULT_API_VERSION,RUNWAYML_POLLING_TIMEOUT,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -62,12 +63,8 @@ class RunwayMLImageGenerationConfig(BaseImageGenerationConfig):Some providers need `model` in `api_base`"""- complete_url: str = api_base or get_secret_str("RUNWAYML_API_BASE") or self.DEFAULT_BASE_URL-- complete_url = complete_url.rstrip("/")- if self.IMAGE_GENERATION_ENDPOINT:- complete_url = f"{complete_url}/{self.IMAGE_GENERATION_ENDPOINT}"- return complete_url+ base_url: Final = api_base or get_secret_str("RUNWAYML_API_BASE") or self.DEFAULT_BASE_URL+ return join_api_base(base_url, self.IMAGE_GENERATION_ENDPOINT)def validate_environment(self,@@ -226,8 +223,7 @@ class RunwayMLImageGenerationConfig(BaseImageGenerationConfig):start_time: Final = time.time()# Build task status URL- api_base = api_base.rstrip("/")- task_url: Final = f"{api_base}/v1/tasks/{task_id}"+ task_url: Final = join_api_base(api_base, f"v1/tasks/{task_id}")verbose_logger.debug("Polling RunwayML task: %s", task_url)@@ -275,8 +271,7 @@ class RunwayMLImageGenerationConfig(BaseImageGenerationConfig):start_time: Final = time.time()# Build task status URL- api_base = api_base.rstrip("/")- task_url: Final = f"{api_base}/v1/tasks/{task_id}"+ task_url: Final = join_api_base(api_base, f"v1/tasks/{task_id}")verbose_logger.debug("Polling RunwayML task (async): %s", task_url)diff --git a/litellm/llms/runwayml/text_to_speech/transformation.py b/litellm/llms/runwayml/text_to_speech/transformation.pyindex d41ffea17dc..1f052648f13 100644--- a/litellm/llms/runwayml/text_to_speech/transformation.py+++ b/litellm/llms/runwayml/text_to_speech/transformation.py@@ -18,6 +18,7 @@ from litellm.constants import (RUNWAYML_DEFAULT_API_VERSION,RUNWAYML_POLLING_TIMEOUT,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.text_to_speech.transformation import (BaseTextToSpeechConfig,TextToSpeechRequestData,@@ -230,10 +231,8 @@ class RunwayMLTextToSpeechConfig(BaseTextToSpeechConfig):"""Get the complete URL for RunwayML TTS request"""- complete_url = api_base or get_secret_str("RUNWAYML_API_BASE") or self.DEFAULT_BASE_URL-- complete_url = complete_url.rstrip("/")- return f"{complete_url}/{self.TTS_ENDPOINT_PATH}"+ base_url: Final = api_base or get_secret_str("RUNWAYML_API_BASE") or self.DEFAULT_BASE_URL+ return join_api_base(base_url, self.TTS_ENDPOINT_PATH)@staticmethoddef _check_timeout(start_time: float, timeout_secs: float) -> None:@@ -311,8 +310,7 @@ class RunwayMLTextToSpeechConfig(BaseTextToSpeechConfig):start_time: Final = time.time()# Build task status URL- api_base = api_base.rstrip("/")- task_url: Final = f"{api_base}/v1/tasks/{task_id}"+ task_url: Final = join_api_base(api_base, f"v1/tasks/{task_id}")verbose_logger.debug("Polling RunwayML TTS task: %s", task_url)@@ -359,8 +357,7 @@ class RunwayMLTextToSpeechConfig(BaseTextToSpeechConfig):start_time: Final = time.time()# Build task status URL- api_base = api_base.rstrip("/")- task_url: Final = f"{api_base}/v1/tasks/{task_id}"+ task_url: Final = join_api_base(api_base, f"v1/tasks/{task_id}")verbose_logger.debug("Polling RunwayML TTS task (async): %s", task_url)diff --git a/litellm/llms/runwayml/videos/transformation.py b/litellm/llms/runwayml/videos/transformation.pyindex 8b765b1390b..106567f6fba 100644--- a/litellm/llms/runwayml/videos/transformation.py+++ b/litellm/llms/runwayml/videos/transformation.py@@ -9,7 +9,7 @@ from typing_extensions import ReadOnly, TypedDictimport litellmfrom litellm.constants import RUNWAYML_DEFAULT_API_VERSION-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.videos.transformation import BaseVideoConfigfrom litellm.llms.custom_httpx.http_handler import (@@ -274,7 +274,7 @@ class RunwayMLVideoConfig(BaseVideoConfig):files_list: Final[RequestFiles] = []- return request_data, files_list, f"{api_base}/{endpoint}"+ return request_data, files_list, join_api_base(api_base, endpoint)def _select_generation_endpoint(self, request_data: Mapping[str, object]) -> str:if request_data.get("promptVideo") is not None or request_data.get("videoUri") is not None:@@ -413,7 +413,7 @@ class RunwayMLVideoConfig(BaseVideoConfig):encoded_video_id: Final = encode_url_path_segment(original_video_id, field_name="video_id")# Get task status to retrieve video URL- url: Final = f"{api_base}/tasks/{encoded_video_id}"+ url: Final = join_api_base(api_base, f"tasks/{encoded_video_id}")return url, dict[str, str]()@@ -571,7 +571,7 @@ class RunwayMLVideoConfig(BaseVideoConfig):encoded_video_id: Final = encode_url_path_segment(original_video_id, field_name="video_id")# Construct the URL for task cancellation- url: Final = f"{api_base}/tasks/{encoded_video_id}/cancel"+ url: Final = join_api_base(api_base, f"tasks/{encoded_video_id}/cancel")return url, dict[str, str]()@@ -608,7 +608,7 @@ class RunwayMLVideoConfig(BaseVideoConfig):encoded_video_id: Final = encode_url_path_segment(original_video_id, field_name="video_id")# Construct the full URL for task status retrieval- url: Final = f"{api_base}/tasks/{encoded_video_id}"+ url: Final = join_api_base(api_base, f"tasks/{encoded_video_id}")# Empty dict for GET request (no body)return url, dict[str, str]()diff --git a/litellm/llms/s3_vectors/vector_stores/transformation.py b/litellm/llms/s3_vectors/vector_stores/transformation.pyindex 044315168d2..2b0de3dc9ea 100644--- a/litellm/llms/s3_vectors/vector_stores/transformation.py+++ b/litellm/llms/s3_vectors/vector_stores/transformation.py@@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.vector_store.transformation import (BaseQueryEmbeddingVectorStoreConfig,VectorStoreEmbeddingExecutor,@@ -131,7 +132,7 @@ class S3VectorsVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig, BaseAWSLLMlitellm_logging_obj: LiteLLMLoggingObj,) -> tuple[str, dict[str, object]]:litellm_logging_obj.model_call_details["query"] = query_text- return f"{api_base}/QueryVectors", {+ return join_api_base(api_base, "QueryVectors"), {"vectorBucketName": bucket_name,"indexName": index_name,"queryVector": {"float32": list(query_vector)},diff --git a/litellm/llms/sambanova/embedding/transformation.py b/litellm/llms/sambanova/embedding/transformation.pyindex cf39c0a092f..93cbb13ac47 100644--- a/litellm/llms/sambanova/embedding/transformation.py+++ b/litellm/llms/sambanova/embedding/transformation.py@@ -10,6 +10,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -37,11 +38,7 @@ class SambaNovaEmbeddingConfig(BaseEmbeddingConfig):) -> str:if api_base is None:raise ValueError("api_base is required for SambaNova embeddings")- # Remove trailing slashes and ensure clean base URL- api_base = api_base.rstrip("/")- if not api_base.endswith("/embeddings"):- api_base = f"{api_base}/embeddings"- return api_base+ return ensure_api_route(api_base, "embeddings")def validate_environment(self,diff --git a/litellm/llms/sap/chat/transformation.py b/litellm/llms/sap/chat/transformation.pyindex a82dfe72322..8a7baf18e8f 100755--- a/litellm/llms/sap/chat/transformation.py+++ b/litellm/llms/sap/chat/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import BaseModelimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.types.llms.openai import AllMessageValuesfrom litellm.types.utils import ModelResponse@@ -185,12 +186,12 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig):# Keep a short, tight client lifecycle here to avoid fd leaksclient: Final = litellm.module_level_client# with httpx.Client(timeout=30) as client:- deployments: Final = client.get(f"{self.base_url}/lm/deployments", headers=self.headers).json()+ deployments: Final = client.get(join_api_base(self.base_url, "lm/deployments"), headers=self.headers).json()valid: Final[list[tuple[str, str]]] = []for dep in deployments.get("resources", []):if dep.get("scenarioId") == "orchestration":cfg = client.get(- f"{self.base_url}/lm/configurations/{dep['configurationId']}",+ join_api_base(self.base_url, f"lm/configurations/{dep['configurationId']}"),headers=self.headers,).json()if cfg.get("executableId") == "orchestration":@@ -263,7 +264,7 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig):litellm_params: dict,stream: bool | None = None,):- api_base_: Final = f"{self.deployment_url}/v2/completion"+ api_base_: Final = join_api_base(self.deployment_url, "v2/completion")return api_base_def _build_prompt_module(diff --git a/litellm/llms/sap/credentials.py b/litellm/llms/sap/credentials.pyindex b1618f38623..2622d0fa9b6 100644--- a/litellm/llms/sap/credentials.py+++ b/litellm/llms/sap/credentials.py@@ -16,6 +16,7 @@ from typing_extensions import NotRequired, ReadOnly, TypedDictimport litellmfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.custom_httpx.http_handler import HTTPHandler, get_httpx_clientAUTH_ENDPOINT_SUFFIX: Final = "/oauth/token"@@ -112,7 +113,7 @@ CREDENTIAL_VALUES: Final[list[CredentialsValue]] = [CredentialsValue("base_url",("serviceurls", "AI_API_URL"),- transform_fn=lambda url: url.rstrip("/") + ("" if url.endswith("/v2") else "/v2"),+ transform_fn=lambda url: ensure_api_route(url, "v2"),),CredentialsValue("cert_url",diff --git a/litellm/llms/sap/embed/transformation.py b/litellm/llms/sap/embed/transformation.pyindex e91dfb91081..107fef0b355 100644--- a/litellm/llms/sap/embed/transformation.py+++ b/litellm/llms/sap/embed/transformation.py@@ -8,6 +8,7 @@ from typing import Final, Literalimport httpxfrom pydantic import Field+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.embedding.transformation import (BaseEmbeddingConfig,LiteLLMLoggingObj,@@ -101,11 +102,11 @@ class GenAIHubEmbeddingConfig(BaseEmbeddingConfig):def deployment_url(self) -> str:with httpx.Client(timeout=30) as client:valid_deployments: Final = []- deployments: Final = client.get(self.base_url + "/lm/deployments", headers=self.headers).json()+ deployments: Final = client.get(join_api_base(self.base_url, "lm/deployments"), headers=self.headers).json()for deployment in deployments.get("resources", []):if deployment["scenarioId"] == "orchestration":config_details = client.get(- self.base_url + f"/lm/configurations/{deployment['configurationId']}",+ join_api_base(self.base_url, f"lm/configurations/{deployment['configurationId']}"),headers=self.headers,).json()if config_details["executableId"] == "orchestration":@@ -144,7 +145,7 @@ class GenAIHubEmbeddingConfig(BaseEmbeddingConfig):litellm_params: dict,stream: bool | None = None,) -> str:- url: Final = self.deployment_url.rstrip("/") + "/v2/embeddings"+ url: Final = join_api_base(self.deployment_url, "v2/embeddings")return urldef transform_embedding_request(diff --git a/litellm/llms/scaledown/chat/transformation.py b/litellm/llms/scaledown/chat/transformation.pyindex 44eb4785615..3224bc97148 100644--- a/litellm/llms/scaledown/chat/transformation.py+++ b/litellm/llms/scaledown/chat/transformation.py@@ -9,6 +9,7 @@ from urllib.parse import unquoteimport httpxfrom pydantic import JsonValue, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues@@ -103,8 +104,8 @@ class ScaleDownChatConfig(BaseConfig):base: Final = _root(api_base or _trusted_root())operation: Final = _operation(model)if operation in DECISIONS_MODELS:- return f"{base}/v1/scaledown"- return f"{base}{NATIVE_PATHS[operation]}"+ return join_api_base(base, "v1/scaledown")+ return join_api_base(base, NATIVE_PATHS[operation])def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: BaseConfig signatureoperation: Final = _operation(model)diff --git a/litellm/llms/scaleway/audio_transcription/transformation.py b/litellm/llms/scaleway/audio_transcription/transformation.pyindex 7ebf4475355..8913cc362b3 100644--- a/litellm/llms/scaleway/audio_transcription/transformation.py+++ b/litellm/llms/scaleway/audio_transcription/transformation.py@@ -9,6 +9,7 @@ from typing import Finalimport httpxfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -58,8 +59,7 @@ class ScalewayAudioTranscriptionConfig(BaseAudioTranscriptionConfig):litellm_params: dict,stream: bool | None = None,) -> str:- api_base = "https://api.scaleway.ai/v1" if api_base is None else api_base.rstrip("/")- return f"{api_base}/audio/transcriptions"+ return join_api_base("https://api.scaleway.ai/v1" if api_base is None else api_base, "audio/transcriptions")def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:return ScalewayAudioTranscriptionException(diff --git a/litellm/llms/scaleway/rerank/transformation.py b/litellm/llms/scaleway/rerank/transformation.pyindex 921252d7091..1de5d71f17a 100644--- a/litellm/llms/scaleway/rerank/transformation.py+++ b/litellm/llms/scaleway/rerank/transformation.py@@ -9,6 +9,7 @@ API reference: https://www.scaleway.com/en/developers/api/generative-apis/#path-from collections.abc import Mappingfrom typing import Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.jina_ai.rerank.transformation import JinaAIRerankConfigfrom litellm.secret_managers.main import get_secret_str@@ -25,8 +26,8 @@ class ScalewayRerankConfig(JinaAIRerankConfig):model: str,optional_params: Mapping[str, object] | None = None,) -> str:- base: Final = SCALEWAY_API_BASE if api_base is None else api_base.rstrip("/")- return f"{base}/rerank"+ base: Final = SCALEWAY_API_BASE if api_base is None else api_base+ return join_api_base(base, "rerank")def validate_environment(self,diff --git a/litellm/llms/searxng/search/transformation.py b/litellm/llms/searxng/search/transformation.pyindex 265a3db72f5..b1753821cd2 100644--- a/litellm/llms/searxng/search/transformation.py+++ b/litellm/llms/searxng/search/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -101,12 +102,7 @@ class SearXNGSearchConfig(BaseSearchConfig):"or pass `api_base` parameter. Example: os.environ['SEARXNG_API_BASE'] = 'https://your-searxng-instance.com'")- # Append "/search" to the api base if it's not already there- if not api_base.endswith("/search"):- if api_base.endswith("/"):- api_base = f"{api_base}search"- else:- api_base = f"{api_base}/search"+ api_base = ensure_api_route(api_base, "search")# Build query parameters from the transformed request bodyif data and isinstance(data, dict) and "_searxng_params" in data:diff --git a/litellm/llms/serper/search/transformation.py b/litellm/llms/serper/search/transformation.pyindex 2539610ea59..e82427e4d0c 100644--- a/litellm/llms/serper/search/transformation.py+++ b/litellm/llms/serper/search/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -84,12 +85,7 @@ class SerperSearchConfig(BaseSearchConfig):Get complete URL for Search endpoint."""api_base = api_base or get_secret_str("SERPER_API_BASE") or self.SERPER_API_BASE- api_base = api_base.rstrip("/")-- if not api_base.endswith("/search"):- api_base = f"{api_base}/search"-- return api_base+ return ensure_api_route(api_base, "search")def transform_search_request(self,diff --git a/litellm/llms/snowflake/chat/transformation.py b/litellm/llms/snowflake/chat/transformation.pyindex 6a480904a3e..030413030eb 100644--- a/litellm/llms/snowflake/chat/transformation.py+++ b/litellm/llms/snowflake/chat/transformation.py@@ -25,6 +25,7 @@ from litellm.litellm_core_utils.prompt_templates.factory import (select_anthropic_content_block_type_for_file,)from litellm.litellm_core_utils.prompt_templates.image_handling import async_inline_remote_media+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.chat.handler import ModelResponseIterator as AnthropicStreamParserfrom litellm.llms.anthropic.chat.transformation import AnthropicConfigfrom litellm.llms.anthropic.common_utils import normalize_cache_control_in_anthropic_payload@@ -239,8 +240,8 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig):) -> str:api_base = self._get_api_base(api_base, optional_params)if is_snowflake_claude_model(model):- return f"{api_base}/cortex/v1/messages"- return f"{api_base}/cortex/v1/chat/completions"+ return join_api_base(api_base, "cortex/v1/messages")+ return join_api_base(api_base, "cortex/v1/chat/completions")def validate_environment(self,diff --git a/litellm/llms/snowflake/embedding/transformation.py b/litellm/llms/snowflake/embedding/transformation.pyindex ec482337e52..45bec1283a3 100644--- a/litellm/llms/snowflake/embedding/transformation.py+++ b/litellm/llms/snowflake/embedding/transformation.py@@ -5,6 +5,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.types.llms.openai import AllEmbeddingInputValues@@ -31,7 +32,7 @@ class SnowflakeEmbeddingConfig(SnowflakeBaseConfig, BaseEmbeddingConfig):) -> str:api_base = self._get_api_base(api_base, optional_params)- return f"{api_base}/cortex/inference:embed"+ return join_api_base(api_base, "cortex/inference:embed")def transform_embedding_request(self,diff --git a/litellm/llms/snowflake/utils.py b/litellm/llms/snowflake/utils.pyindex a8c14d1d7f3..7cc88ab66e1 100644--- a/litellm/llms/snowflake/utils.py+++ b/litellm/llms/snowflake/utils.py@@ -1,6 +1,7 @@import refrom typing import TYPE_CHECKING, Any, Final+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues@@ -53,7 +54,7 @@ class SnowflakeBaseConfig:optional_params[param] = valuereturn optional_params- def _get_api_base(self, api_base, optional_params):+ def _get_api_base(self, api_base: str | None, optional_params) -> str:if not api_base:if "account_id" in optional_params:account_id = optional_params.pop("account_id")@@ -65,10 +66,7 @@ class SnowflakeBaseConfig:raise ValueError("Invalid account_id format")api_base = f"https://{account_id}.snowflakecomputing.com/api/v2"- api_base = api_base.rstrip("/")- if not api_base.endswith("/api/v2"):- api_base += "/api/v2"- return api_base+ return ensure_api_route(api_base, "api/v2")def validate_environment(self,diff --git a/litellm/llms/soniox/audio_transcription/handler.py b/litellm/llms/soniox/audio_transcription/handler.pyindex f86bc142898..a48e3e3ed4c 100644--- a/litellm/llms/soniox/audio_transcription/handler.py+++ b/litellm/llms/soniox/audio_transcription/handler.py@@ -31,6 +31,7 @@ from litellm.litellm_core_utils.audio_utils.utils import (get_audio_file_name,process_audio_file,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import (AsyncHTTPHandler,HTTPHandler,@@ -329,7 +330,7 @@ class SonioxAudioTranscriptionHandler:input=None,api_key=api_key,additional_args={- "api_base": f"{api_base}/v1/transcriptions",+ "api_base": join_api_base(api_base, "v1/transcriptions"),"atranscription": True,"complete_input_dict": SonioxAudioTranscriptionHandler._redact_body_for_logging(body),},@@ -451,7 +452,7 @@ class SonioxAudioTranscriptionHandler:self._safe_log_pre_call(logging_obj, api_key, base_url, body)create_resp: Final = http_client.post(- url=f"{base_url}/v1/transcriptions",+ url=join_api_base(base_url, "v1/transcriptions"),headers=auth_headers,json=body,timeout=timeout,@@ -472,7 +473,7 @@ class SonioxAudioTranscriptionHandler:)transcript_resp: Final = http_client.get(- url=f"{base_url}/v1/transcriptions/{transcription_id}/transcript",+ url=join_api_base(base_url, f"v1/transcriptions/{transcription_id}/transcript"),headers=auth_headers,timeout=timeout,)@@ -528,7 +529,7 @@ class SonioxAudioTranscriptionHandler:# `Authorization` header is fine; httpx sets multipart Content-Type.upload_headers: Final = {"Authorization": auth_headers["Authorization"]}resp: Final = http_client.post(- url=f"{base_url}/v1/files",+ url=join_api_base(base_url, "v1/files"),headers=upload_headers,files=files,timeout=timeout,@@ -550,7 +551,7 @@ class SonioxAudioTranscriptionHandler:) -> _TranscriptionMeta:for _ in range(max_attempts):resp = http_client.get(- url=f"{base_url}/v1/transcriptions/{transcription_id}",+ url=join_api_base(base_url, f"v1/transcriptions/{transcription_id}"),headers=auth_headers,timeout=timeout,)@@ -593,7 +594,7 @@ class SonioxAudioTranscriptionHandler:if "transcription" in cleanup and transcription_id:try:http_client.delete(- url=f"{base_url}/v1/transcriptions/{transcription_id}",+ url=join_api_base(base_url, f"v1/transcriptions/{transcription_id}"),headers=auth_headers,timeout=timeout,)@@ -605,7 +606,7 @@ class SonioxAudioTranscriptionHandler:if "file" in cleanup and file_id_to_cleanup:try:http_client.delete(- url=f"{base_url}/v1/files/{file_id_to_cleanup}",+ url=join_api_base(base_url, f"v1/files/{file_id_to_cleanup}"),headers=auth_headers,timeout=timeout,)@@ -685,7 +686,7 @@ class SonioxAudioTranscriptionHandler:self._safe_log_pre_call(logging_obj, api_key, base_url, body)create_resp: Final = await http_client.post(- url=f"{base_url}/v1/transcriptions",+ url=join_api_base(base_url, "v1/transcriptions"),headers=auth_headers,json=body,timeout=timeout,@@ -706,7 +707,7 @@ class SonioxAudioTranscriptionHandler:)transcript_resp: Final = await http_client.get(- url=f"{base_url}/v1/transcriptions/{transcription_id}/transcript",+ url=join_api_base(base_url, f"v1/transcriptions/{transcription_id}/transcript"),headers=auth_headers,timeout=timeout,)@@ -761,7 +762,7 @@ class SonioxAudioTranscriptionHandler:}upload_headers: Final = {"Authorization": auth_headers["Authorization"]}resp: Final = await http_client.post(- url=f"{base_url}/v1/files",+ url=join_api_base(base_url, "v1/files"),headers=upload_headers,files=files,timeout=timeout,@@ -783,7 +784,7 @@ class SonioxAudioTranscriptionHandler:) -> _TranscriptionMeta:for _ in range(max_attempts):resp = await http_client.get(- url=f"{base_url}/v1/transcriptions/{transcription_id}",+ url=join_api_base(base_url, f"v1/transcriptions/{transcription_id}"),headers=auth_headers,timeout=timeout,)@@ -826,7 +827,7 @@ class SonioxAudioTranscriptionHandler:if "transcription" in cleanup and transcription_id:try:await http_client.delete(- url=f"{base_url}/v1/transcriptions/{transcription_id}",+ url=join_api_base(base_url, f"v1/transcriptions/{transcription_id}"),headers=auth_headers,timeout=timeout,)@@ -838,7 +839,7 @@ class SonioxAudioTranscriptionHandler:if "file" in cleanup and file_id_to_cleanup:try:await http_client.delete(- url=f"{base_url}/v1/files/{file_id_to_cleanup}",+ url=join_api_base(base_url, f"v1/files/{file_id_to_cleanup}"),headers=auth_headers,timeout=timeout,)diff --git a/litellm/llms/stability/image_generation/transformation.py b/litellm/llms/stability/image_generation/transformation.pyindex 1856523ce49..a53fd087098 100644--- a/litellm/llms/stability/image_generation/transformation.py+++ b/litellm/llms/stability/image_generation/transformation.py@@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpxfrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_generation.transformation import (BaseImageGenerationConfig,)@@ -131,11 +132,10 @@ class StabilityImageGenerationConfig(BaseImageGenerationConfig):"""Get the complete URL for the Stability AI API request."""- base_url: str = api_base or get_secret_str("STABILITY_API_BASE") or self.DEFAULT_BASE_URL- base_url = base_url.rstrip("/")+ base_url: Final = api_base or get_secret_str("STABILITY_API_BASE") or self.DEFAULT_BASE_URLendpoint: Final = self._get_model_endpoint(model)- return f"{base_url}{endpoint}"+ return join_api_base(base_url, endpoint)def validate_environment(self,diff --git a/litellm/llms/tavily/search/transformation.py b/litellm/llms/tavily/search/transformation.pyindex d0531e4c041..b46f974e0db 100644--- a/litellm/llms/tavily/search/transformation.py+++ b/litellm/llms/tavily/search/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -90,11 +91,7 @@ class TavilySearchConfig(BaseSearchConfig):"""Get complete URL for Search endpoint."""- resolved_api_base: Final = api_base or get_secret_str("TAVILY_API_BASE") or self.TAVILY_API_BASE- clean_api_base: Final = resolved_api_base.rstrip("/")- if clean_api_base.endswith("/search"):- return clean_api_base- return f"{clean_api_base}/search"+ return ensure_api_route(api_base or get_secret_str("TAVILY_API_BASE") or self.TAVILY_API_BASE, "search")def transform_search_request(self,diff --git a/litellm/llms/tencent/chat/transformation.py b/litellm/llms/tencent/chat/transformation.pyindex 7e80b0012df..1f0bcd218d6 100644--- a/litellm/llms/tencent/chat/transformation.py+++ b/litellm/llms/tencent/chat/transformation.py@@ -9,6 +9,7 @@ from typing import Final, TypedDictfrom typing_extensions import ReadOnlyimport litellm+from litellm.litellm_core_utils.url_utils import ensure_api_route, join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.utils import supports_reasoning@@ -137,7 +138,4 @@ class TencentChatConfig(OpenAIGPTConfig):if api_base.endswith("/chat/completions"):return api_base- if not api_base.endswith("/v1"):- api_base = f"{api_base}/v1"-- return f"{api_base}/chat/completions"+ return join_api_base(ensure_api_route(api_base, "v1"), "chat/completions")diff --git a/litellm/llms/tencent/messages/transformation.py b/litellm/llms/tencent/messages/transformation.pyindex c56ecaeb51c..1e52bc37d47 100644--- a/litellm/llms/tencent/messages/transformation.py+++ b/litellm/llms/tencent/messages/transformation.py@@ -8,6 +8,7 @@ alongside its standard OpenAI-compatible chat completions endpoint.from typing import Anyimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.anthropic.pass_through.messages.transformation import (AnthropicMessagesConfig,)@@ -82,4 +83,4 @@ class TencentAnthropicMessagesConfig(AnthropicMessagesConfig):elif base_url.endswith("/v1"):base_url = base_url[: -len("/v1")]- return f"{base_url}/v1/messages"+ return join_api_base(base_url, "v1/messages")diff --git a/litellm/llms/topaz/image_variations/transformation.py b/litellm/llms/topaz/image_variations/transformation.pyindex 8d03b73e94e..b3dded36f49 100644--- a/litellm/llms/topaz/image_variations/transformation.py+++ b/litellm/llms/topaz/image_variations/transformation.py@@ -7,6 +7,7 @@ from typing import TYPE_CHECKING, Finalfrom aiohttp import ClientResponsefrom httpx import Headers, Response+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import (BaseLLMException,LiteLLMLoggingObj,@@ -39,8 +40,7 @@ class TopazImageVariationConfig(TopazModelInfo, BaseImageVariationConfig):litellm_params: dict,stream: bool | None = None,) -> str:- clean_api_base: Final = (api_base or "https://api.topazlabs.com").rstrip("/")- return f"{clean_api_base}/image/v1/enhance"+ return join_api_base(api_base or "https://api.topazlabs.com", "image/v1/enhance")def map_openai_params(self,diff --git a/litellm/llms/vercel_ai_gateway/chat/transformation.py b/litellm/llms/vercel_ai_gateway/chat/transformation.pyindex 19c4c2659ca..3fc74b6acc1 100644--- a/litellm/llms/vercel_ai_gateway/chat/transformation.py+++ b/litellm/llms/vercel_ai_gateway/chat/transformation.py@@ -11,6 +11,7 @@ from typing import Finalimport httpximport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues@@ -92,7 +93,7 @@ class VercelAIGatewayConfig(OpenAIGPTConfig):if api_base is None:api_base = "https://ai-gateway.vercel.sh/v1"- models_url: Final = f"{api_base}/models"+ models_url: Final = join_api_base(api_base, "models")response: Final = litellm.module_level_client.get(url=models_url)if response.status_code != 200:diff --git a/litellm/llms/vercel_ai_gateway/embedding/transformation.py b/litellm/llms/vercel_ai_gateway/embedding/transformation.pyindex 9cfaaab89f0..01d7b7766c0 100644--- a/litellm/llms/vercel_ai_gateway/embedding/transformation.py+++ b/litellm/llms/vercel_ai_gateway/embedding/transformation.py@@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.secret_managers.main import get_secret_str@@ -77,12 +78,8 @@ class VercelAIGatewayEmbeddingConfig(BaseEmbeddingConfig):"""Get the complete URL for Vercel AI Gateway Embedding API endpoint."""- if api_base:- api_base = api_base.rstrip("/")- else:- api_base = get_secret_str("VERCEL_AI_GATEWAY_API_BASE") or "https://ai-gateway.vercel.sh/v1"-- return f"{api_base}/embeddings"+ base: Final = api_base or get_secret_str("VERCEL_AI_GATEWAY_API_BASE") or "https://ai-gateway.vercel.sh/v1"+ return join_api_base(base, "embeddings")def transform_embedding_request(self,diff --git a/litellm/llms/vertex_ai/audio_transcription/gemini_transcribe_transformation.py b/litellm/llms/vertex_ai/audio_transcription/gemini_transcribe_transformation.pyindex f4db5eb110c..39d2e9b4c49 100644--- a/litellm/llms/vertex_ai/audio_transcription/gemini_transcribe_transformation.py+++ b/litellm/llms/vertex_ai/audio_transcription/gemini_transcribe_transformation.py@@ -10,6 +10,7 @@ from litellm.litellm_core_utils.audio_utils.utils import (normalize_transcription_language_to_bcp47,process_audio_file,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -129,10 +130,10 @@ class VertexGeminiAudioTranscriptionConfig(BaseAudioTranscriptionConfig, VertexBproject_id: Final = validate_vertex_transcription_project_id(self.safe_get_vertex_ai_project(vertex_params) or self._resolve_project_id_from_credentials(vertex_params))- base_url: Final = (api_base or get_vertex_base_url(location)).rstrip("/")+ base_url: Final = api_base or get_vertex_base_url(location)bare_model: Final = model.removeprefix("vertex_ai/")model_path: Final = f"projects/{project_id}/locations/{location}/publishers/google/models/{bare_model}"- return f"{base_url}/v1/{model_path}:generateContent"+ return join_api_base(base_url, f"v1/{model_path}:generateContent")def _resolve_project_id_from_credentials(self, litellm_params: Mapping[str, object]) -> str:vertex_params: Final = dict(litellm_params)diff --git a/litellm/llms/vertex_ai/audio_transcription/transformation.py b/litellm/llms/vertex_ai/audio_transcription/transformation.pyindex 5603be81095..4fdf93a6307 100644--- a/litellm/llms/vertex_ai/audio_transcription/transformation.py+++ b/litellm/llms/vertex_ai/audio_transcription/transformation.py@@ -9,6 +9,7 @@ from litellm.litellm_core_utils.audio_utils.utils import (normalize_transcription_language_to_bcp47,process_audio_file,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.audio_transcription.transformation import (AudioTranscriptionRequestData,BaseAudioTranscriptionConfig,@@ -126,8 +127,8 @@ class VertexAIAudioTranscriptionConfig(BaseAudioTranscriptionConfig, VertexBase)project_id: Final = validate_vertex_transcription_project_id(self.safe_get_vertex_ai_project(litellm_params) or self._resolve_project_id_from_credentials(litellm_params))- base_url: Final = (api_base or f"https://{speech_to_text_host(location)}").rstrip("/")- return f"{base_url}/v2/projects/{project_id}/locations/{location}/recognizers/_:recognize"+ base_url: Final = api_base or f"https://{speech_to_text_host(location)}"+ return join_api_base(base_url, f"v2/projects/{project_id}/locations/{location}/recognizers/_:recognize")def _resolve_project_id_from_credentials(self, litellm_params: dict) -> str:_, project_id = self._ensure_access_token(diff --git a/litellm/llms/vertex_ai/batches/handler.py b/litellm/llms/vertex_ai/batches/handler.pyindex de5fe39cfc9..7c4238e3498 100644--- a/litellm/llms/vertex_ai/batches/handler.py+++ b/litellm/llms/vertex_ai/batches/handler.py@@ -10,6 +10,7 @@ import litellmfrom litellm.litellm_core_utils.url_utils import (async_safe_get,encode_url_path_segment,+ join_api_base,safe_get,)from litellm.llms.custom_httpx.http_handler import (@@ -194,7 +195,7 @@ class VertexAIBatchPrediction(VertexLLM):api_base_path: Final = urlparse(api_base).path.rstrip("/")if api_base_path in ("/v1", "/v1beta1"):return graft_default_vertex_path(api_base=api_base, default_url=default_endpoint_url)- return api_base.rstrip("/") + urlparse(default_endpoint_url).path+ return join_api_base(api_base, urlparse(default_endpoint_url).path)def _resolve_fine_tuned_endpoint_model(self,diff --git a/litellm/llms/vertex_ai/files/transformation.py b/litellm/llms/vertex_ai/files/transformation.pyindex d1e3f10fdd4..e36aba00293 100644--- a/litellm/llms/vertex_ai/files/transformation.py+++ b/litellm/llms/vertex_ai/files/transformation.py@@ -37,6 +37,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (extract_file_data,extract_file_metadata,)+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_utils import map_developer_role_to_system_rolefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.files.batch_records import responses_batch_body_to_chat_body@@ -1083,12 +1084,12 @@ class VertexAIFilesConfig(VertexBase, BaseFilesConfig):if object_prefix:object_name = f"{object_prefix}/{object_name}"encoded_object_name: Final = encode_gcs_object_name_for_url(object_name)- endpoint: Final = f"upload/storage/v1/b/{bucket_name}/o?uploadType=media&name={encoded_object_name}"+ upload_path: Final = f"upload/storage/v1/b/{bucket_name}/o"api_base = api_base or "https://storage.googleapis.com"if not api_base:raise ValueError("api_base is required")- return f"{api_base}/{endpoint}"+ return f"{join_api_base(api_base, upload_path)}?uploadType=media&name={encoded_object_name}"def get_supported_openai_params(self, model: str) -> list[OpenAICreateFileRequestOptionalParams]:return []diff --git a/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.py b/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.pyindex f916d494aad..085ff715c30 100644--- a/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.py+++ b/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.py@@ -12,6 +12,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm.constants import DEFAULT_MAX_RECURSE_DEPTH+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfigfrom litellm.llms.vertex_ai.common_utils import get_vertex_base_urlfrom litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import VertexLLM@@ -143,12 +144,12 @@ class VertexAIImagenImageEditConfig(BaseImageEditConfig, VertexLLM):if model.startswith("vertex_ai/"):model_name = model.replace("vertex_ai/", "")- if api_base:- base_url = api_base.rstrip("/")- else:- base_url = get_vertex_base_url(vertex_location)+ base_url: Final = api_base or get_vertex_base_url(vertex_location)- return f"{base_url}/v1/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model_name}:predict"+ return join_api_base(+ base_url,+ f"v1/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model_name}:predict",+ )def transform_image_edit_request(self,diff --git a/litellm/llms/vertex_ai/interactions/transformation.py b/litellm/llms/vertex_ai/interactions/transformation.pyindex 36965fe666f..b8e13059917 100644--- a/litellm/llms/vertex_ai/interactions/transformation.py+++ b/litellm/llms/vertex_ai/interactions/transformation.py@@ -2,7 +2,7 @@ from collections.abc import Callable, Mappingfrom dataclasses import dataclassfrom typing import Final-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.gemini.interactions.transformation import GoogleAIStudioInteractionsConfigfrom litellm.llms.vertex_ai.common_utils import validate_vertex_locationfrom litellm.llms.vertex_ai.vertex_llm_base import VertexBase@@ -22,9 +22,9 @@ class VertexInteractionsTarget:@propertydef collection_url(self) -> str:- return (- f"{self.base_url}/{VERTEX_INTERACTIONS_API_VERSION}"- f"/projects/{self.project_id}/locations/{self.location}/interactions"+ return join_api_base(+ self.base_url,+ f"{VERTEX_INTERACTIONS_API_VERSION}/projects/{self.project_id}/locations/{self.location}/interactions",)def interaction_url(self, interaction_id: str) -> str:diff --git a/litellm/llms/vertex_ai/realtime/transformation.py b/litellm/llms/vertex_ai/realtime/transformation.pyindex 55f1cf58cbe..13f547b3b92 100644--- a/litellm/llms/vertex_ai/realtime/transformation.py+++ b/litellm/llms/vertex_ai/realtime/transformation.py@@ -21,6 +21,7 @@ from collections.abc import Awaitable, Callablefrom typing import Finalfrom litellm import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.gemini.realtime.transformation import GeminiRealtimeConfigfrom litellm.llms.vertex_ai.audio_transcription.realtime_transformation import (VertexChirpRealtimeConfig,@@ -68,9 +69,8 @@ class VertexAIRealtimeConfig(GeminiRealtimeConfig):"""if api_base:# Allow callers to supply a fully-qualified wss:// base URL.- base = api_base.rstrip("/")- base = base.replace("https://", "wss://").replace("http://", "ws://")- return f"{base}/ws/google.cloud.aiplatform.v1.LlmBidiService/BidiGenerateContent"+ base: Final = api_base.replace("https://", "wss://").replace("http://", "ws://")+ return join_api_base(base, "ws/google.cloud.aiplatform.v1.LlmBidiService/BidiGenerateContent")host: Final = get_vertex_base_url(self._location).removeprefix("https://")return f"wss://{host}/ws/google.cloud.aiplatform.v1.LlmBidiService/BidiGenerateContent"diff --git a/litellm/llms/vertex_ai/text_to_speech/transformation.py b/litellm/llms/vertex_ai/text_to_speech/transformation.pyindex 1441d01e41e..c0082bc5ca6 100644--- a/litellm/llms/vertex_ai/text_to_speech/transformation.py+++ b/litellm/llms/vertex_ai/text_to_speech/transformation.py@@ -23,7 +23,7 @@ from litellm.litellm_core_utils.audio_utils.utils import (speech_media_type_from_audio_bytes,)from litellm.litellm_core_utils.core_helpers import as_str_mapping, drops_unsupported_params-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.text_to_speech.transformation import (BaseTextToSpeechConfig,@@ -1075,13 +1075,14 @@ class VertexAILyriaTextToSpeechConfig(VertexAITextToSpeechConfig):},)location: Final = self.safe_get_vertex_ai_location(litellm_params) or self.get_default_vertex_location()- base_url: Final = self.get_api_base(api_base=api_base, vertex_location=location).rstrip("/")+ base_url: Final = self.get_api_base(api_base=api_base, vertex_location=location)encoded_project: Final = encode_url_path_segment(project, field_name="project")encoded_location: Final = encode_url_path_segment(location, field_name="location")encoded_model: Final = encode_url_path_segment(base_model, field_name="model")- return (- f"{base_url}/v1/projects/{encoded_project}/locations/{encoded_location}"- f"/publishers/google/models/{encoded_model}:predict"+ return join_api_base(+ base_url,+ f"v1/projects/{encoded_project}/locations/{encoded_location}"+ f"/publishers/google/models/{encoded_model}:predict",)def transform_text_to_speech_request(diff --git a/litellm/llms/vertex_ai/vector_stores/rag_api/transformation.py b/litellm/llms/vertex_ai/vector_stores/rag_api/transformation.pyindex 5c250fc1a7e..440604688bb 100644--- a/litellm/llms/vertex_ai/vector_stores/rag_api/transformation.py+++ b/litellm/llms/vertex_ai/vector_stores/rag_api/transformation.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Final, Protocolimport httpxfrom typing_extensions import ReadOnly, TypedDict+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.vector_store.transformation import BaseVectorStoreConfigfrom litellm.llms.vertex_ai.common_utils import get_vertex_base_urlfrom litellm.llms.vertex_ai.vertex_llm_base import VertexBase@@ -288,7 +289,7 @@ class VertexVectorStoreConfig(BaseVectorStoreConfig, VertexBase):"""Transform create request for Vertex AI RAG Corpus"""- url: Final = f"{api_base}/ragCorpora" # Base URL for creating RAG corpus+ url: Final = join_api_base(api_base, "ragCorpora") # Base URL for creating RAG corpus# Add metadata if providedmetadata: Final = vector_store_create_optional_params.get("metadata")diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.pyindex 7100c9efde3..4be41e26326 100644--- a/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py+++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py@@ -8,6 +8,7 @@ their respective publisher-specific count-tokens endpoints.from typing import Any, Final+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import get_async_httpx_clientfrom litellm.llms.vertex_ai.common_utils import get_vertex_base_urlfrom litellm.llms.vertex_ai.vertex_llm_base import VertexBase@@ -59,16 +60,14 @@ class VertexAIPartnerModelsTokenCounter(VertexBase):publisher: Final = self._get_publisher_for_model(model)# Use custom api_base if provided, otherwise construct default- if api_base:- base_url = api_base- else:- base_url = get_vertex_base_url(vertex_location)+ base_url: Final = api_base or get_vertex_base_url(vertex_location)# Construct the count-tokens endpoint# Format: /v1/projects/{project}/locations/{location}/publishers/{publisher}/models/count-tokens:rawPredict- endpoint: Final = (- f"{base_url}/v1/projects/{project_id}/locations/{vertex_location}/"- f"publishers/{publisher}/models/count-tokens:rawPredict"+ endpoint: Final = join_api_base(+ base_url,+ f"v1/projects/{project_id}/locations/{vertex_location}/"+ f"publishers/{publisher}/models/count-tokens:rawPredict",)return endpointdiff --git a/litellm/llms/vertex_ai/vertex_llm_base.py b/litellm/llms/vertex_ai/vertex_llm_base.pyindex 77508096e4a..6cff9729189 100644--- a/litellm/llms/vertex_ai/vertex_llm_base.py+++ b/litellm/llms/vertex_ai/vertex_llm_base.py@@ -14,6 +14,7 @@ from urllib.parse import urlparseimport litellmfrom litellm._logging import verbose_loggerfrom litellm.litellm_core_utils.asyncify import asyncify+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import AsyncHTTPHandlerfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.vertex_ai import VERTEX_CREDENTIALS_TYPES, VertexPartnerProvider@@ -295,23 +296,22 @@ class VertexBase:if api_base is None:api_base = get_vertex_base_url(vertex_location)+ location_path: Final = f"projects/{vertex_project}/locations/{vertex_location}"+ raw_predict_method: Final = "streamRawPredict" if stream else "rawPredict"if partner == VertexPartnerProvider.llama:- return f"{api_base}/v1/projects/{vertex_project}/locations/{vertex_location}/endpoints/openapi/chat/completions"+ return join_api_base(api_base, f"v1/{location_path}/endpoints/openapi/chat/completions")elif partner == VertexPartnerProvider.mistralai:- if stream:- return f"{api_base}/v1/projects/{vertex_project}/locations/{vertex_location}/publishers/mistralai/models/{model}:streamRawPredict"- else:- return f"{api_base}/v1/projects/{vertex_project}/locations/{vertex_location}/publishers/mistralai/models/{model}:rawPredict"+ return join_api_base(+ api_base, f"v1/{location_path}/publishers/mistralai/models/{model}:{raw_predict_method}"+ )elif partner == VertexPartnerProvider.ai21:- if stream:- return f"{api_base}/v1beta1/projects/{vertex_project}/locations/{vertex_location}/publishers/ai21/models/{model}:streamRawPredict"- else:- return f"{api_base}/v1beta1/projects/{vertex_project}/locations/{vertex_location}/publishers/ai21/models/{model}:rawPredict"+ return join_api_base(+ api_base, f"v1beta1/{location_path}/publishers/ai21/models/{model}:{raw_predict_method}"+ )elif partner == VertexPartnerProvider.claude:- if stream:- return f"{api_base}/v1/projects/{vertex_project}/locations/{vertex_location}/publishers/anthropic/models/{model}:streamRawPredict"- else:- return f"{api_base}/v1/projects/{vertex_project}/locations/{vertex_location}/publishers/anthropic/models/{model}:rawPredict"+ return join_api_base(+ api_base, f"v1/{location_path}/publishers/anthropic/models/{model}:{raw_predict_method}"+ )def get_complete_vertex_url(self,@@ -658,7 +658,7 @@ class VertexBase:# For Gemini (Google AI Studio), construct the full path like other providersif model is None:raise ValueError("Model parameter is required for Gemini custom API base URLs")- url = f"{api_base}/models/{model}:{endpoint}"+ url = join_api_base(api_base, f"models/{model}:{endpoint}")if gemini_api_key is None:raise ValueError("Missing Gemini API key. Set the GEMINI_API_KEY or GOOGLE_API_KEY environment variable."@@ -678,20 +678,17 @@ class VertexBase:model_for_url: Final = get_vertex_base_model_name(model=model)# Format: {api_base}/v1/projects/{project}/locations/{location}/endpoints/{model}:{endpoint}version: Final = vertex_api_version or "v1"- url = "{}/{}/projects/{}/locations/{}/endpoints/{}:{}".format(- api_base.rstrip("/"),- version,- vertex_project,- vertex_location,- model_for_url,- endpoint,+ url = join_api_base(+ api_base,+ f"{version}/projects/{vertex_project}/locations/{vertex_location}"+ f"/endpoints/{model_for_url}:{endpoint}",)elif urlparse(api_base).path in ("", "/"):- url = api_base.rstrip("/") + urlparse(url).path+ url = join_api_base(api_base, urlparse(url).path)elif urlparse(api_base).path.rstrip("/") in ("/v1", "/v1beta1") and "/projects/" in urlparse(url).path:url = graft_default_vertex_path(api_base=api_base, default_url=url)else:- url = f"{api_base}:{endpoint}"+ url = f"{api_base.rstrip('/')}:{endpoint}"if stream is True:parsed_stream_url: Final = urlparse(url)stream_query: Final = f"{parsed_stream_url.query}&alt=sse" if parsed_stream_url.query else "alt=sse"diff --git a/litellm/llms/vertex_ai/videos/transformation.py b/litellm/llms/vertex_ai/videos/transformation.pyindex de368cea5ac..080899d08b0 100644--- a/litellm/llms/vertex_ai/videos/transformation.py+++ b/litellm/llms/vertex_ai/videos/transformation.py@@ -18,6 +18,7 @@ from typing_extensions import ReadOnlyimport litellmfrom litellm.constants import DEFAULT_GOOGLE_VIDEO_DURATION_SECONDSfrom litellm.images.utils import ImageEditRequestUtils+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.videos.transformation import BaseVideoConfigfrom litellm.llms.vertex_ai.common_utils import (convert_vertex_datetime_to_openai_datetime,@@ -325,13 +326,10 @@ class VertexAIVideoConfig(BaseVideoConfig, VertexBase):model_name: Final = model.replace("vertex_ai/", "")# Construct the URL- if api_base:- base_url = api_base.rstrip("/")- else:- base_url = get_vertex_base_url(vertex_location)+ base_url: Final = api_base or get_vertex_base_url(vertex_location)- url: Final = (- f"{base_url}/v1/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model_name}"+ url: Final = join_api_base(+ base_url, f"v1/projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{model_name}")return url@@ -480,7 +478,7 @@ class VertexAIVideoConfig(BaseVideoConfig, VertexBase):# Construct the full URL including model ID# URL format: https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/publishers/google/models/MODEL:fetchPredictOperation# Strip trailing slashes from api_base and append model- url: Final = f"{api_base.rstrip('/')}/{model}:fetchPredictOperation"+ url: Final = join_api_base(api_base, f"{model}:fetchPredictOperation")# Request body contains the operation nameparams: Final = {"operationName": operation_name}@@ -778,7 +776,7 @@ class VertexAIVideoConfig(BaseVideoConfig, VertexBase):if vertex_params:request_data["parameters"] = vertex_params- edit_url: Final = f"{api_base.rstrip('/')}/{model}:predictLongRunning"+ edit_url: Final = join_api_base(api_base, f"{model}:predictLongRunning")return edit_url, request_data, Nonedef transform_video_edit_response(diff --git a/litellm/llms/volcengine/embedding/transformation.py b/litellm/llms/volcengine/embedding/transformation.pyindex b298c9996f7..cc1e7d59caa 100644--- a/litellm/llms/volcengine/embedding/transformation.py+++ b/litellm/llms/volcengine/embedding/transformation.py@@ -10,6 +10,7 @@ from typing import Finalimport httpxfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_route, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.types.llms.openai import AllEmbeddingInputValues, AllMessageValues@@ -76,12 +77,8 @@ class VolcEngineEmbeddingConfig(BaseEmbeddingConfig):Returns:Complete URL for the embedding API endpoint"""- base_url: Final = get_volcengine_base_url(api_base)- # Construct the complete URL with /embeddings endpoint- if base_url.endswith("/api/v3"):- return f"{base_url}/embeddings"- else:- return f"{base_url}/api/v3/embeddings"+ versioned_base_url: Final = ensure_api_route(get_volcengine_base_url(api_base), "api/v3")+ return join_api_base(versioned_base_url, "embeddings")def map_openai_params(self,diff --git a/litellm/llms/volcengine/responses/transformation.py b/litellm/llms/volcengine/responses/transformation.pyindex 6e0aa88b6b6..74f6182bd89 100644--- a/litellm/llms/volcengine/responses/transformation.py+++ b/litellm/llms/volcengine/responses/transformation.py@@ -11,7 +11,7 @@ from litellm.litellm_core_utils.core_helpers import process_response_headersfrom litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (safe_convert_created_field,)-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import (@@ -137,8 +137,8 @@ class VolcEngineResponsesAPIConfig(OpenAIResponsesAPIConfig):if base_url.endswith("/responses"):return base_urlif base_url.endswith("/api/v3"):- return f"{base_url}/responses"- return f"{base_url}/api/v3/responses"+ return join_api_base(base_url, "responses")+ return join_api_base(base_url, "api/v3/responses")def map_openai_params(self,@@ -345,7 +345,7 @@ class VolcEngineResponsesAPIConfig(OpenAIResponsesAPIConfig):order: Literal["asc", "desc"] = "desc",) -> tuple[str, dict]:encoded_response_id: Final = encode_url_path_segment(response_id, field_name="response_id")- url: Final = f"{api_base}/{encoded_response_id}/input_items"+ url: Final = join_api_base(api_base, f"{encoded_response_id}/input_items")params: Final[dict[str, str | int]] = {}if after is not None:params["after"] = after@@ -380,7 +380,7 @@ class VolcEngineResponsesAPIConfig(OpenAIResponsesAPIConfig):headers: dict,) -> tuple[str, dict]:encoded_response_id: Final = encode_url_path_segment(response_id, field_name="response_id")- url: Final = f"{api_base}/{encoded_response_id}/cancel"+ url: Final = join_api_base(api_base, f"{encoded_response_id}/cancel")data: Final[dict] = {}return url, datadiff --git a/litellm/llms/voyage/embedding/transformation.py b/litellm/llms/voyage/embedding/transformation.pyindex 7e8425bb3d0..1ba66e5ce65 100644--- a/litellm/llms/voyage/embedding/transformation.py+++ b/litellm/llms/voyage/embedding/transformation.py@@ -5,6 +5,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.llms.voyage.common_utils import get_default_base_url, get_voyage_api_key@@ -50,12 +51,7 @@ class VoyageEmbeddingConfig(BaseEmbeddingConfig):litellm_params: dict,stream: bool | None = None,) -> str:- if api_base:- clean_api_base: Final = api_base.rstrip("/")- if not clean_api_base.endswith("/embeddings"):- return f"{clean_api_base}/embeddings"- return clean_api_base- return f"{get_default_base_url(api_key)}/embeddings"+ return ensure_api_route(api_base or get_default_base_url(api_key), "embeddings")def get_supported_openai_params(self, model: str) -> list:return [diff --git a/litellm/llms/voyage/embedding/transformation_contextual.py b/litellm/llms/voyage/embedding/transformation_contextual.pyindex 8e3802ccbc9..62c03139fbe 100644--- a/litellm/llms/voyage/embedding/transformation_contextual.py+++ b/litellm/llms/voyage/embedding/transformation_contextual.py@@ -10,6 +10,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.llms.voyage.common_utils import get_default_base_url, get_voyage_api_key@@ -58,12 +59,7 @@ class VoyageContextualEmbeddingConfig(BaseEmbeddingConfig):litellm_params: dict,stream: bool | None = None,) -> str:- if api_base:- clean_api_base: Final = api_base.rstrip("/")- if not clean_api_base.endswith("/contextualizedembeddings"):- return f"{clean_api_base}/contextualizedembeddings"- return clean_api_base- return f"{get_default_base_url(api_key)}/contextualizedembeddings"+ return ensure_api_route(api_base or get_default_base_url(api_key), "contextualizedembeddings")def get_supported_openai_params(self, model: str) -> list: # mutable-ok: base class signaturereturn ["encoding_format", "dimensions"]diff --git a/litellm/llms/voyage/embedding/transformation_multimodal.py b/litellm/llms/voyage/embedding/transformation_multimodal.pyindex 29fdb9ecfe1..c04a2e1f26b 100644--- a/litellm/llms/voyage/embedding/transformation_multimodal.py+++ b/litellm/llms/voyage/embedding/transformation_multimodal.py@@ -13,6 +13,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfigfrom litellm.llms.voyage.common_utils import get_default_base_url, get_voyage_api_key@@ -59,12 +60,7 @@ class VoyageMultimodalEmbeddingConfig(BaseEmbeddingConfig):litellm_params: dict,stream: bool | None = None,) -> str:- if api_base:- clean_api_base: Final = api_base.rstrip("/")- if not clean_api_base.endswith("/multimodalembeddings"):- return f"{clean_api_base}/multimodalembeddings"- return clean_api_base- return f"{get_default_base_url(api_key)}/multimodalembeddings"+ return ensure_api_route(api_base or get_default_base_url(api_key), "multimodalembeddings")def get_supported_openai_params(self, model: str) -> list:return ["dimensions"]diff --git a/litellm/llms/voyage/rerank/transformation.py b/litellm/llms/voyage/rerank/transformation.pyindex 121c18e82ae..7fe643870d5 100644--- a/litellm/llms/voyage/rerank/transformation.py+++ b/litellm/llms/voyage/rerank/transformation.py@@ -11,6 +11,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom litellm._uuid import uuid+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import LiteLLMLoggingObjfrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.llms.voyage.common_utils import get_default_base_url, get_voyage_api_key@@ -80,9 +81,9 @@ class VoyageRerankConfig(BaseRerankConfig):api_base = api_base.rstrip("/")if not api_base.endswith("/v1/rerank"):if api_base.endswith("/v1"):- api_base = f"{api_base}/rerank"+ api_base = join_api_base(api_base, "rerank")else:- api_base = f"{api_base}/v1/rerank"+ api_base = join_api_base(api_base, "v1/rerank")return api_basedef transform_rerank_request(diff --git a/litellm/llms/watsonx/audio_transcription/transformation.py b/litellm/llms/watsonx/audio_transcription/transformation.pyindex 77ef03b8e10..e5da7dd412f 100644--- a/litellm/llms/watsonx/audio_transcription/transformation.py+++ b/litellm/llms/watsonx/audio_transcription/transformation.py@@ -12,6 +12,7 @@ from pydantic import ConfigDict, TypeAdapterimport litellmfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.types.llms.openai import (AllMessageValues,OpenAIAudioTranscriptionOptionalParams,@@ -150,10 +151,9 @@ class IBMWatsonXAudioTranscriptionConfig(IBMWatsonXMixin, OpenAIWhisperAudioTran"""# Get base URLurl = self._get_base_url(api_base=api_base)- url = url.rstrip("/")# Add the audio transcription endpoint- url = f"{url}/ml/v1/audio/transcriptions"+ url = join_api_base(url, "ml/v1/audio/transcriptions")# Add version parameter (only version in query string, not project_id)api_version: Final = optional_params.get("api_version", None) or litellm.WATSONX_DEFAULT_API_VERSIONdiff --git a/litellm/llms/watsonx/chat/transformation.py b/litellm/llms/watsonx/chat/transformation.pyindex 4ff2212711d..071ff7108b9 100644--- a/litellm/llms/watsonx/chat/transformation.py+++ b/litellm/llms/watsonx/chat/transformation.py@@ -7,6 +7,7 @@ Docs: https://cloud.ibm.com/apidocs/watsonx-ai#text-chatfrom typing import Finalfrom litellm import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.watsonx import (WatsonXAIEndpoint,@@ -96,7 +97,7 @@ class IBMWatsonXChatConfig(IBMWatsonXMixin, OpenAIGPTConfig):endpoint = endpoint.format(deployment_id=deployment_id)else:endpoint = WatsonXAIEndpoint.CHAT_STREAM.value if stream else WatsonXAIEndpoint.CHAT.value- url = url.rstrip("/") + endpoint+ url = join_api_base(url, endpoint)## add api versionurl = self._add_api_version_to_url(url=url, api_version=optional_params.pop("api_version", None))diff --git a/litellm/llms/watsonx/completion/transformation.py b/litellm/llms/watsonx/completion/transformation.pyindex 5c0416bd6ca..9ea062ea2f8 100644--- a/litellm/llms/watsonx/completion/transformation.py+++ b/litellm/llms/watsonx/completion/transformation.py@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Any, Finalimport httpx+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.types.llms.openai import AllMessageValues, ChatCompletionUsageBlockfrom litellm.types.llms.watsonx import WatsonXAIEndpoint@@ -342,7 +343,7 @@ class IBMWatsonXAIConfig(IBMWatsonXMixin, BaseConfig):endpoint = endpoint.format(deployment_id=deployment_id)else:endpoint = WatsonXAIEndpoint.TEXT_GENERATION_STREAM if stream else WatsonXAIEndpoint.TEXT_GENERATION- url = url.rstrip("/") + endpoint+ url = join_api_base(url, endpoint)## add api versionurl = self._add_api_version_to_url(url=url, api_version=optional_params.pop("api_version", None))diff --git a/litellm/llms/watsonx/embed/transformation.py b/litellm/llms/watsonx/embed/transformation.pyindex 1b870c7f639..6ec3b50bbe9 100644--- a/litellm/llms/watsonx/embed/transformation.py+++ b/litellm/llms/watsonx/embed/transformation.py@@ -9,6 +9,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterfrom typing_extensions import ReadOnly, TypedDict+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.embedding.transformation import (BaseEmbeddingConfig,LiteLLMLoggingObj,@@ -92,7 +93,7 @@ class IBMWatsonXEmbeddingConfig(IBMWatsonXMixin, BaseEmbeddingConfig):if model.startswith("deployment/"):deployment_id: Final = "/".join(model.split("/")[1:])endpoint = endpoint.format(deployment_id=deployment_id)- url = url.rstrip("/") + endpoint+ url = join_api_base(url, endpoint)## add api versionurl = self._add_api_version_to_url(url=url, api_version=optional_params.pop("api_version", None))diff --git a/litellm/llms/watsonx/rerank/transformation.py b/litellm/llms/watsonx/rerank/transformation.pyindex 5f337603763..591ddda3cbd 100644--- a/litellm/llms/watsonx/rerank/transformation.py+++ b/litellm/llms/watsonx/rerank/transformation.py@@ -11,6 +11,7 @@ from typing import Final, castimport httpxfrom pydantic import ConfigDict, TypeAdapter+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import LiteLLMLoggingObjfrom litellm.llms.base_llm.rerank.transformation import BaseRerankConfigfrom litellm.secret_managers.main import get_secret_str@@ -45,7 +46,7 @@ class IBMWatsonXRerankConfig(IBMWatsonXMixin, BaseRerankConfig):base_url: Final = self._get_base_url(api_base=api_base)endpoint: Final = WatsonXAIEndpoint.RERANK.value- url: Final = base_url.rstrip("/") + endpoint+ url: Final = join_api_base(base_url, endpoint)params: Final = optional_params or {}diff --git a/litellm/llms/xai/audio_transcription/transformation.py b/litellm/llms/xai/audio_transcription/transformation.pyindex e34034b5ae9..e29c3d9334a 100644--- a/litellm/llms/xai/audio_transcription/transformation.py+++ b/litellm/llms/xai/audio_transcription/transformation.py@@ -10,6 +10,7 @@ from pydantic import ConfigDict, TypeAdapter, ValidationErrorimport litellmfrom litellm.litellm_core_utils.audio_utils.utils import process_audio_file+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.types.llms.base import LiteLLMBaseModelfrom litellm.types.llms.openai import (@@ -185,7 +186,7 @@ class XAIAudioTranscriptionConfig(BaseAudioTranscriptionConfig):) -> str:base: Final = (XAIModelInfo.get_api_base(api_base) or "").rstrip("/")normalized: Final = base.removesuffix("/v1")- return f"{normalized}/v1/stt"+ return join_api_base(normalized, "v1/stt")def validate_environment(self,diff --git a/litellm/llms/xai/batches/transformation.py b/litellm/llms/xai/batches/transformation.pyindex fdd9939526e..1e803057e3d 100644--- a/litellm/llms/xai/batches/transformation.py+++ b/litellm/llms/xai/batches/transformation.py@@ -20,7 +20,7 @@ from pydantic import ConfigDictfrom typing_extensions import NotRequired, ReadOnly, TypedDictfrom litellm.constants import XAI_API_BASE-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.xai.common_utils import XAIModelInfofrom litellm.secret_managers.main import get_secret_str@@ -76,10 +76,11 @@ def get_xai_auth_headers(def xai_batches_url(api_base: str | None, batch_id: str | None = None, suffix: str = "") -> str:- base: Final = f"{get_xai_api_base(api_base)}/v1/batches"+ root: Final = get_xai_api_base(api_base)if batch_id is None:- return base- return f"{base}/{encode_url_path_segment(batch_id, field_name='batch_id')}{suffix}"+ return join_api_base(root, "v1/batches")+ encoded_batch_id: Final = encode_url_path_segment(batch_id, field_name="batch_id")+ return join_api_base(root, f"v1/batches/{encoded_batch_id}{suffix}")def is_xai_batch_results_id(file_id: str) -> bool:diff --git a/litellm/llms/xai/common_utils.py b/litellm/llms/xai/common_utils.pyindex 4843caeac59..06e3f32a767 100644--- a/litellm/llms/xai/common_utils.py+++ b/litellm/llms/xai/common_utils.py@@ -5,6 +5,7 @@ import httpxfrom pydantic import ConfigDict, TypeAdapterimport litellm+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.base_utils import BaseLLMModelInfofrom litellm.secret_managers.main import get_secret_strfrom litellm.types.llms.openai import AllMessageValues@@ -91,7 +92,7 @@ class XAIModelInfo(BaseLLMModelInfo):"XAI API base or key is not set. Set XAI_API_BASE and provide an xAI API key via api_key, litellm.xai_key, or XAI_API_KEY.")response: Final = litellm.module_level_client.get(- url=f"{api_base}/v1/models",+ url=join_api_base(api_base, "v1/models"),headers={"Authorization": f"Bearer {api_key}"},)diff --git a/litellm/llms/xai/files/transformation.py b/litellm/llms/xai/files/transformation.pyindex 33baca79005..88f2fc51983 100644--- a/litellm/llms/xai/files/transformation.py+++ b/litellm/llms/xai/files/transformation.py@@ -14,7 +14,7 @@ from pydantic import ConfigDictfrom typing_extensions import ReadOnly, TypedDictfrom litellm.litellm_core_utils.prompt_templates.common_utils import extract_file_data-from litellm.litellm_core_utils.url_utils import encode_url_path_segment+from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_api_basefrom litellm.llms.base_llm.chat.transformation import BaseLLMExceptionfrom litellm.llms.base_llm.files.transformation import BaseFilesConfig, LiteLLMLoggingObjfrom litellm.types.llms.base import LiteLLMBaseModel@@ -101,11 +101,11 @@ class XAIFilesConfig(BaseFilesConfig):litellm_params: Mapping[str, object],stream: bool | None = None,) -> str:- return f"{get_xai_api_base(api_base)}/v1/files"+ return join_api_base(get_xai_api_base(api_base), "v1/files")def _file_url(self, file_id: str, litellm_params: Mapping[str, object], suffix: str = "") -> str:encoded_file_id: Final = encode_url_path_segment(file_id, field_name="file_id")- return f"{_api_base_from(litellm_params)}/v1/files/{encoded_file_id}{suffix}"+ return join_api_base(_api_base_from(litellm_params), f"v1/files/{encoded_file_id}{suffix}")def get_error_class(self, error_message: str, status_code: int, headers: Mapping[str, str] | httpx.Headers@@ -204,7 +204,7 @@ class XAIFilesConfig(BaseFilesConfig):optional_params: Mapping[str, object],litellm_params: Mapping[str, object],) -> tuple[str, dict[str, str]]: # mutable-ok: BaseFilesConfig signature- return f"{_api_base_from(litellm_params)}/v1/files", _NO_QUERY_PARAMS+ return join_api_base(_api_base_from(litellm_params), "v1/files"), _NO_QUERY_PARAMSdef transform_list_files_next_request(self,@@ -215,7 +215,7 @@ class XAIFilesConfig(BaseFilesConfig):page: Final = XAIFileList.model_validate(raw_response.json())if not page.pagination_token or not page.data:return None- return f"{_api_base_from(litellm_params)}/v1/files", {"pagination_token": page.pagination_token}+ return join_api_base(_api_base_from(litellm_params), "v1/files"), {"pagination_token": page.pagination_token}def transform_list_files_response(self,diff --git a/litellm/llms/xai/responses/transformation.py b/litellm/llms/xai/responses/transformation.pyindex 2f638da49c8..b6ea516fb79 100644--- a/litellm/llms/xai/responses/transformation.py+++ b/litellm/llms/xai/responses/transformation.py@@ -9,6 +9,7 @@ import litellmfrom litellm._logging import verbose_loggerfrom litellm.constants import XAI_API_BASEfrom litellm.exceptions import AuthenticationError+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfigfrom litellm.llms.xai.common_utils import XAIModelInfo, xai_reported_cost_in_usdfrom litellm.secret_managers.main import get_secret_str@@ -249,10 +250,7 @@ class XAIResponsesAPIConfig(OpenAIResponsesAPIConfig):else:api_base = api_base or litellm.api_base or get_secret_str("XAI_API_BASE") or XAI_API_BASE- # Remove trailing slashes- api_base = api_base.rstrip("/")-- return f"{api_base}/responses"+ return join_api_base(api_base, "responses")def transform_response_api_response(self,diff --git a/litellm/llms/you_com/search/transformation.py b/litellm/llms/you_com/search/transformation.pyindex 8b2df949a93..9228deda26b 100644--- a/litellm/llms/you_com/search/transformation.py+++ b/litellm/llms/you_com/search/transformation.py@@ -10,6 +10,7 @@ from typing import Final, TypedDictimport httpxfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.base_llm.search.transformation import (BaseSearchConfig,SearchResponse,@@ -109,7 +110,7 @@ class YouComSearchConfig(BaseSearchConfig):api_base = api_base.rstrip("/")if not api_base.endswith("/v1/search") and not api_base.endswith("/v1/agents/search"):- api_base = f"{api_base}/v1/search"+ api_base = join_api_base(api_base, "v1/search")return api_basediff --git a/litellm/main.py b/litellm/main.pyindex 079b0bcbd6c..27700cf9def 100644--- a/litellm/main.py+++ b/litellm/main.py@@ -112,6 +112,7 @@ from litellm.litellm_core_utils.request_timeout_resolver import (get_configured_request_timeout,)from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer+from litellm.litellm_core_utils.url_utils import ensure_api_routefrom litellm.llms.base_llm import BaseConfig, BaseImageGenerationConfigfrom litellm.llms.base_llm.base_model_iterator import (convert_model_response_to_streaming,@@ -230,6 +231,7 @@ from .llms.azure.azure import AzureChatCompletion, check_dynamic_azure_paramsfrom .llms.azure.chat.o_series_handler import AzureOpenAIO1ChatCompletionfrom .llms.azure.completion.handler import AzureTextCompletionfrom .llms.azure_ai.anthropic.handler import AzureAnthropicChatCompletion+from .llms.azure_ai.anthropic.transformation import azure_anthropic_messages_urlfrom .llms.azure_ai.embed import AzureAIEmbeddingfrom .llms.bedrock.chat import BedrockConverseLLMfrom .llms.bedrock.chat.converse_transformation import AmazonConverseConfig@@ -1666,16 +1668,8 @@ def _complete_azure_ai(ctx: CompletionDispatchContext) -> _CompletionDispatchRes)api_key = AzureFoundryModelInfo.get_api_key(api_key)- # Ensure the URL ends with /v1/messages for Anthropicif api_base:- api_base = api_base.rstrip("/")- if not api_base.endswith("/v1/messages"):- if "/anthropic" in api_base:- parts: Final = api_base.split("/anthropic", 1)- api_base = parts[0] + "/anthropic"- else:- api_base = api_base + "/anthropic"- api_base = api_base + "/v1/messages"+ api_base = azure_anthropic_messages_url(api_base)response = azure_anthropic_chat_completions.completion(model=model,@@ -2895,8 +2889,8 @@ def _complete_anthropic_text(# Check if we should disable automatic URL suffix appendingdisable_url_suffix: Final = get_secret_bool("LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX")- if api_base is not None and not disable_url_suffix and not api_base.endswith("/v1/complete"):- api_base += "/v1/complete"+ if api_base is not None and not disable_url_suffix:+ api_base = ensure_api_route(api_base, "v1/complete")elif disable_url_suffix:verbose_logger.debug("LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX is set, skipping /v1/complete suffix")@@ -2951,8 +2945,8 @@ def _complete_anthropic(ctx: CompletionDispatchContext) -> _CompletionDispatchRe# Check if we should disable automatic URL suffix appendingdisable_url_suffix: Final = get_secret_bool("LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX")- if api_base is not None and not disable_url_suffix and not api_base.endswith("/v1/messages"):- api_base += "/v1/messages"+ if api_base is not None and not disable_url_suffix:+ api_base = ensure_api_route(api_base, "v1/messages")elif disable_url_suffix:verbose_logger.debug("LITELLM_ANTHROPIC_DISABLE_URL_SUFFIX is set, skipping /v1/messages suffix")@@ -4090,9 +4084,7 @@ def _complete_text_completion_inception(api_base = passed_api_base or get_secret_str("INCEPTION_API_BASE") or "https://api.inceptionlabs.ai/v1"# FIM is served at `/v1/fim/completions`; the OpenAI client appends# `/completions`, so point it at the `/v1/fim` base.- api_base = api_base.rstrip("/")- if not api_base.endswith("/fim"):- api_base += "/fim"+ api_base = ensure_api_route(api_base, "fim")# Don't forward the server-managed Inception key to a caller-supplied# api_base; only resolve it for the default/server base, or when thediff --git a/litellm/rag/ingestion/gemini_ingestion.py b/litellm/rag/ingestion/gemini_ingestion.pyindex 4ee16685e03..6320e477bf2 100644--- a/litellm/rag/ingestion/gemini_ingestion.py+++ b/litellm/rag/ingestion/gemini_ingestion.py@@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Final, castfrom pydantic import ConfigDictfrom litellm._logging import verbose_logger+from litellm.litellm_core_utils.url_utils import join_api_basefrom litellm.llms.custom_httpx.http_handler import (get_async_httpx_client,header_value,@@ -107,7 +108,7 @@ class GeminiRAGIngestion(BaseRAGIngestion):raise ValueError("GEMINI_API_BASE is required")api_version: Final = "v1beta"- base_url: Final = f"{api_base}/{api_version}"+ base_url: Final = join_api_base(api_base, api_version)# Create File Search store if not providedif not vector_store_id:@@ -148,7 +149,7 @@ class GeminiRAGIngestion(BaseRAGIngestion):Returns:Store name (format: fileSearchStores/xxxxxxx)"""- url: Final = f"{base_url}/fileSearchStores"+ url: Final = join_api_base(base_url, "fileSearchStores")request_body: Final = {"displayName": display_name}@@ -236,7 +237,7 @@ class GeminiRAGIngestion(BaseRAGIngestion):# base_url is like: https://generativelanguage.googleapis.com/v1beta# We need: https://generativelanguage.googleapis.com/upload/v1beta/{store_id}:uploadToFileSearchStoreapi_base: Final = base_url.replace("/v1beta", "") # Get base without version- url: Final = f"{api_base}/upload/v1beta/{vector_store_id}:uploadToFileSearchStore"+ url: Final = join_api_base(api_base, f"upload/v1beta/{vector_store_id}:uploadToFileSearchStore")# Build request body with chunking config and metadata if providedrequest_body: Final[dict[str, object]] = {"displayName": filename}diff --git a/tests/integration/providers/_api_base_url_join_support.py b/tests/integration/providers/_api_base_url_join_support.pynew file mode 100644index 00000000000..44bd6ec4dca--- /dev/null+++ b/tests/integration/providers/_api_base_url_join_support.py@@ -0,0 +1,163 @@+import json+from collections.abc import Callable, Mapping+from types import MappingProxyType+from typing import Final+from urllib.parse import urlsplit++from integration._support.anthropic_sse import message_stream+from integration._support.openai_wire import chat_reply+from integration._support.upstream import aws_event_stream_frame+from integration._support.wire import Reply, Request+from integration.providers._api_base_trailing_slash_support import VECTOR, marker_of, reply_text, request_json+from pydantic import JsonValue++COUNTED_TOKENS: Final = 7+RELEVANCE: Final = 0.75+_USAGE: Final = {"input_tokens": 3, "output_tokens": 2}+++def _json(payload: Mapping[str, JsonValue]) -> Reply:+ return Reply(body=json.dumps(payload).encode())+++def _path(request: Request) -> str:+ return urlsplit(request.target).path+++def _wants_stream(request: Request) -> bool:+ return request_json(request).get("stream") is True+++def anthropic_message(marker: str) -> dict[str, JsonValue]:+ return {+ "id": f"msg_{marker}",+ "type": "message",+ "role": "assistant",+ "model": "scripted",+ "content": [{"type": "text", "text": reply_text(marker)}],+ "stop_reason": "end_turn",+ "stop_sequence": None,+ "usage": dict(_USAGE),+ }+++def _anthropic(request: Request) -> Reply:+ if _path(request).endswith("/count_tokens"):+ return _json({"input_tokens": COUNTED_TOKENS})+ marker: Final = marker_of(request)+ if _wants_stream(request):+ return Reply(+ content_type="text/event-stream", chunks=message_stream(f"msg_{marker}", "scripted", reply_text(marker))+ )+ return _json(anthropic_message(marker))+++def _gemini_candidate(text: str, finished: bool) -> dict[str, JsonValue]:+ finish: Final[dict[str, JsonValue]] = {"finishReason": "STOP"} if finished else {}+ return {"content": {"role": "model", "parts": [{"text": text}]}, "index": 0, **finish}+++def _gemini_frame(text: str, finished: bool) -> bytes:+ usage: Final = {"promptTokenCount": 3, "candidatesTokenCount": 2, "totalTokenCount": 5}+ frame: Final = {"candidates": [_gemini_candidate(text, finished)], "usageMetadata": usage}+ return b"data: " + json.dumps(frame).encode() + b"\r\n\r\n"+++def _gemini(request: Request) -> Reply:+ path: Final = _path(request)+ marker: Final = marker_of(request)+ if path.endswith(":batchEmbedContents"):+ return _json({"embeddings": [{"values": list(VECTOR)}]})+ if path.endswith(":streamGenerateContent"):+ return Reply(+ content_type="text/event-stream",+ chunks=(_gemini_frame("reply ", False), _gemini_frame(marker, True)),+ )+ return _json(+ {+ "candidates": [_gemini_candidate(reply_text(marker), True)],+ "usageMetadata": {"promptTokenCount": 3, "candidatesTokenCount": 2, "totalTokenCount": 5},+ }+ )+++def _embedding_list(model: str) -> Reply:+ return _json(+ {+ "object": "list",+ "data": [{"object": "embedding", "index": 0, "embedding": list(VECTOR)}],+ "model": model,+ "usage": {"prompt_tokens": 2, "total_tokens": 2},+ }+ )+++def _openai_like(model: str) -> Callable[[Request], Reply]:+ def respond(request: Request) -> Reply:+ path: Final = _path(request)+ marker: Final = marker_of(request)+ if path.endswith("/embeddings"):+ return _embedding_list(model)+ if path.endswith("/audio/transcriptions"):+ return _json({"text": reply_text(marker), "model": model})+ return chat_reply(f"chatcmpl-{marker}", model, reply_text(marker), stream=_wants_stream(request))++ return respond+++def _converse_event(kind: str, payload: Mapping[str, JsonValue]) -> bytes:+ headers: Final = {":event-type": kind, ":content-type": "application/json", ":message-type": "event"}+ return aws_event_stream_frame(headers, json.dumps(payload, separators=(",", ":")).encode())+++def _bedrock_converse(request: Request) -> Reply:+ marker: Final = marker_of(request)+ usage: Final[dict[str, JsonValue]] = {"inputTokens": 3, "outputTokens": 2, "totalTokens": 5}+ if _path(request).endswith("/converse-stream"):+ events: Final[tuple[tuple[str, Mapping[str, JsonValue]], ...]] = (+ ("messageStart", {"role": "assistant"}),+ ("contentBlockDelta", {"delta": {"text": "reply "}, "contentBlockIndex": 0}),+ ("contentBlockDelta", {"delta": {"text": marker}, "contentBlockIndex": 0}),+ ("contentBlockStop", {"contentBlockIndex": 0}),+ ("messageStop", {"stopReason": "end_turn"}),+ ("metadata", {"usage": usage, "metrics": {"latencyMs": 1}}),+ )+ return Reply(+ content_type="application/vnd.amazon.eventstream",+ chunks=tuple(_converse_event(kind, payload) for kind, payload in events),+ )+ return _json(+ {+ "output": {"message": {"role": "assistant", "content": [{"text": reply_text(marker)}]}},+ "stopReason": "end_turn",+ "usage": usage,+ "metrics": {"latencyMs": 1},+ }+ )+++def _cohere_rerank(request: Request) -> Reply:+ return _json(+ {+ "id": f"rerank-{marker_of(request)}",+ "results": [{"index": 0, "relevance_score": RELEVANCE}],+ "meta": {"billed_units": {"search_units": 1}},+ }+ )+++_BY_PREFIX: Final[Mapping[str, Callable[[Request], Reply]]] = MappingProxyType(+ {+ "an": _anthropic,+ "bi": lambda request: _json(anthropic_message(marker_of(request))),+ "bc": _bedrock_converse,+ "gm": _gemini,+ "mi": _openai_like("mistral-small-latest"),+ "db": _openai_like("databricks-meta-llama-3-3-70b-instruct"),+ "co": _cohere_rerank,+ }+)+++def join_peer(request: Request) -> Reply:+ return _BY_PREFIX[request.target.split("/")[1]](request)diff --git a/tests/integration/providers/test_api_base_url_join_chaos.py b/tests/integration/providers/test_api_base_url_join_chaos.pynew file mode 100644index 00000000000..8ebcd165f4d--- /dev/null+++ b/tests/integration/providers/test_api_base_url_join_chaos.py@@ -0,0 +1,176 @@+import asyncio+from collections.abc import Awaitable, Callable+from itertools import chain, repeat+from typing import Final, TypeAlias++import anthropic+import httpx+import openai+from integration._support.client import Gateway, Scenario, eventually+from integration._support.database import read_rows+from integration._support.wire import Reply, Request, wire_server+from integration.providers._api_base_trailing_slash_support import (+ KEY,+ VECTOR,+ assert_control_answers,+ assert_one_request,+ body_of,+ marker_of,+ new_marker,+ of_marker,+ reply_text,+)+from integration.providers._api_base_url_join_support import join_peer+from pydantic import JsonValue++_PER_KIND: Final = 3+_HAIKU: Final = "anthropic.claude-haiku-5-5"+_GENERATE: Final = "/gm/v1beta/models/gemini-3.8-flash:generateContent"+_STREAM_GENERATE: Final = "/gm/v1beta/models/gemini-3.8-flash:streamGenerateContent?alt=sse"++Answer: TypeAlias = tuple[str | None, str | None]+Call: TypeAlias = Callable[[str], Awaitable[Answer]]+++def _deploy(scenario: Scenario, model: str, api_base: str) -> str:+ if model.startswith("bedrock/"):+ return scenario.model(model=model, api_base=api_base, api_key=KEY, aws_region_name="us-east-1")+ return scenario.model(model=model, api_base=api_base, api_key=KEY)+++def _spend_api_bases(identities: tuple[str, ...]) -> dict[str, JsonValue]:+ rows: Final = eventually(+ lambda: read_rows(+ "SELECT request_id, api_base FROM \"LiteLLM_SpendLogs\" WHERE request_id = ANY(string_to_array(%s, ','))",+ (",".join(identities),),+ ),+ lambda found: len(found) == len(identities),+ seconds=70,+ )+ return {str(row["request_id"]): row["api_base"] for row in rows}+++def test_mixed_burst_on_slash_bases_lands_each_call_once_on_its_single_slash_url(gateway: Gateway) -> None:+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ claude: Final = _deploy(scenario, "anthropic/claude-haiku-5-5", f"{wire.url}/an/")+ gemini: Final = _deploy(scenario, "gemini/gemini-3.8-flash", f"{wire.url}/gm/v1beta/")+ mistral: Final = _deploy(scenario, "mistral/mistral-small-latest", f"{wire.url}/mi/v1/")+ converse: Final = _deploy(scenario, f"bedrock/converse/{_HAIKU}", f"{wire.url}/bc/")+ databricks: Final = _deploy(+ scenario, "databricks/databricks-meta-llama-3-3-70b-instruct", f"{wire.url}/db/serving-endpoints/"+ )+ embed_model: Final = _deploy(scenario, "mistral/mistral-embed", f"{wire.url}/mi/v1/")++ def openai_client() -> openai.AsyncOpenAI:+ return openai.AsyncOpenAI(base_url=str(gateway.client.base_url) + "/v1", api_key=gateway.key, max_retries=0)++ def chat(model: str) -> Call:+ async def call(marker: str) -> Answer:+ async with openai_client() as client:+ completion: Final = await client.chat.completions.create(+ model=model, messages=[{"role": "user", "content": marker}]+ )+ return completion.choices[0].message.content, completion.id++ return call++ def streamed(model: str) -> Call:+ async def call(marker: str) -> Answer:+ async with openai_client() as client:+ stream: Final = await client.chat.completions.create(+ model=model, messages=[{"role": "user", "content": marker}], stream=True+ )+ parts: Final = [chunk.choices[0].delta.content or "" async for chunk in stream if chunk.choices]+ return "".join(parts), None++ return call++ async def messages(marker: str) -> Answer:+ async with anthropic.AsyncAnthropic(+ base_url=str(gateway.client.base_url), api_key=gateway.key, max_retries=0+ ) as client:+ message: Final = await client.messages.create(+ model=claude, max_tokens=16, messages=[{"role": "user", "content": marker}]+ )+ return "".join(block.text for block in message.content if block.type == "text"), None++ async def embed(marker: str) -> Answer:+ async with httpx.AsyncClient(base_url=str(gateway.client.base_url), timeout=60, trust_env=False) as client:+ response: Final = await client.post(+ "/v1/embeddings",+ json={"model": embed_model, "input": marker},+ headers={"Authorization": f"Bearer {gateway.key}"},+ )+ data: Final = body_of(response)["data"]+ vector_matches: Final = data == [{"object": "embedding", "index": 0, "embedding": list(VECTOR)}]+ return (reply_text(marker) if vector_matches else str(data)), None++ kinds: Final[tuple[tuple[str, Call], ...]] = (+ ("/an/v1/messages", chat(claude)),+ ("/an/v1/messages", streamed(claude)),+ ("/an/v1/messages", messages),+ (_GENERATE, chat(gemini)),+ (_STREAM_GENERATE, streamed(gemini)),+ ("/mi/v1/chat/completions", chat(mistral)),+ ("/mi/v1/chat/completions", streamed(mistral)),+ (f"/bc/model/{_HAIKU}/converse", chat(converse)),+ (f"/bc/model/{_HAIKU}/converse-stream", streamed(converse)),+ ("/db/serving-endpoints/chat/completions", chat(databricks)),+ ("/mi/v1/embeddings", embed),+ )+ repeated_kinds: Final = tuple(chain.from_iterable(repeat(kind, _PER_KIND) for kind in kinds))+ calls: Final = tuple((target, call, new_marker()) for target, call in repeated_kinds)++ async def burst() -> list[Answer]:+ return await asyncio.gather(*(call(marker) for _, call, marker in calls))++ answers: Final = asyncio.run(burst())+ assert [text for text, _ in answers] == [reply_text(marker) for *_, marker in calls]+ received: Final = wire.drain()+ assert len(received) == len(calls), sorted(request.target for request in received)+ for target, _, marker in calls:+ assert_one_request(of_marker(received, marker), target, marker)+ logged: Final = {+ identity: f"{wire.url}{target}"+ for (target, _, _), (_, identity) in zip(calls, answers, strict=True)+ if identity is not None+ }+ assert _spend_api_bases(tuple(logged)) == logged+++def test_upstream_dropping_doomed_connections_mid_burst_fails_only_those_calls(gateway: Gateway) -> None:+ markers: Final = tuple(new_marker() for _ in range(16))+ doomed: Final = frozenset(markers[::2])++ def respond(request: Request) -> Reply:+ return Reply(drop_connection=True) if marker_of(request) in doomed else join_peer(request)++ with wire_server(respond) as wire, gateway.scenario() as scenario:+ claude: Final = _deploy(scenario, "anthropic/claude-haiku-5-5", f"{wire.url}/an/")+ gemini: Final = _deploy(scenario, "gemini/gemini-3.8-flash", f"{wire.url}/gm/v1beta/")+ routed: Final = {+ marker: (claude, "/an/v1/messages") if index % 4 < 2 else (gemini, _GENERATE)+ for index, marker in enumerate(markers)+ }++ async def call(marker: str) -> tuple[int, str]:+ async with httpx.AsyncClient(base_url=str(gateway.client.base_url), timeout=60, trust_env=False) as client:+ response: Final = await client.post(+ "/v1/chat/completions",+ json={"model": routed[marker][0], "messages": [{"role": "user", "content": marker}]},+ headers={"Authorization": f"Bearer {gateway.key}"},+ )+ return response.status_code, response.text++ async def burst() -> list[tuple[int, str]]:+ return await asyncio.gather(*(call(marker) for marker in markers))++ answers: Final = dict(zip(markers, asyncio.run(burst()), strict=True))+ received: Final = wire.drain()+ assert_control_answers(gateway, scenario)+ for marker in doomed:+ assert answers[marker][0] >= 500 and "error" in answers[marker][1], answers[marker]+ assert {request.target for request in of_marker(received, marker)} == {routed[marker][1]}, marker+ for marker in frozenset(markers) - doomed:+ assert answers[marker][0] == 200 and reply_text(marker) in answers[marker][1], answers[marker]+ assert_one_request(of_marker(received, marker), routed[marker][1], marker)diff --git a/tests/integration/providers/test_api_base_url_join_host_owned_proxy.py b/tests/integration/providers/test_api_base_url_join_host_owned_proxy.pynew file mode 100644index 00000000000..4a075138305--- /dev/null+++ b/tests/integration/providers/test_api_base_url_join_host_owned_proxy.py@@ -0,0 +1,140 @@+import os+from collections.abc import Callable, Iterator+from pathlib import Path+from typing import Final++import pytest+import yaml+from integration._support.client import JSON_OBJECT, Gateway, gateway_from_environment+from integration._support.forward_proxy import ForwardProxy, refusing_forward_proxy+from integration._support.process import graceful_stop_seconds, owned_proxy+from integration.providers._api_base_trailing_slash_support import KEY, new_marker+from pydantic import JsonValue++_AZURE_HOST: Final = "anthropic-prod.services.ai.azure.com"+_EMBEDDINGS: Final = "host-embeddings"+_RERANK: Final = "host-rerank"+_AZURE_CLAUDE: Final = "host-azure-claude"+_AZURE_COUNT: Final = "host-azure-count"+_EMPTY_BASE: Final = "host-empty-base"+_SCRUBBED_PREFIXES: Final = ("ANTHROPIC_", "AZURE_AI_", "INFINITY_")+_PROXY_VARIABLES: Final = frozenset({"HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY", "NO_PROXY"})+_load_yaml: Final[Callable[[str], object]] = yaml.safe_load++pytestmark: Final = pytest.mark.timeout(2 * graceful_stop_seconds() + 120)+++def _deployment(name: str, model: str, api_base: str) -> dict[str, JsonValue]:+ return {"model_name": name, "litellm_params": {"model": model, "api_base": api_base, "api_key": KEY}}+++def _config(directory: Path) -> Path:+ base: Final = JSON_OBJECT.validate_python(_load_yaml(Path("tests/integration/proxy_config.yaml").read_text()))+ configuration: Final = {+ **base,+ "model_list": [+ _deployment(_EMBEDDINGS, "infinity/BAAI/bge-small-en-v1.5", "http://embeddings"),+ _deployment(_RERANK, "infinity/BAAI/bge-reranker-base", "http://rerank"),+ _deployment(_AZURE_CLAUDE, "azure_ai/claude-haiku-5-5", f"http://{_AZURE_HOST}/anthropic"),+ _deployment(_AZURE_COUNT, "azure_ai/claude-haiku-5-5", f"http://{_AZURE_HOST}"),+ _deployment(_EMPTY_BASE, "anthropic/claude-haiku-5-5", ""),+ ],+ "router_settings": {"disable_cooldowns": True, "num_retries": 0},+ }+ path: Final = directory / "host-named-like-the-route.yaml"+ path.write_text(yaml.safe_dump(configuration))+ return path+++def _scrubbed() -> tuple[str, ...]:+ return tuple(+ name+ for name in os.environ+ if name.upper() in _PROXY_VARIABLES or name.upper().startswith(_SCRUBBED_PREFIXES)+ )++[email protected](scope="module")+def forward() -> Iterator[ForwardProxy]:+ with refusing_forward_proxy() as proxy:+ yield proxy++[email protected](scope="module")+def isolated(forward: ForwardProxy, tmp_path_factory: pytest.TempPathFactory) -> Iterator[Gateway]:+ directory: Final = tmp_path_factory.mktemp("url-join-host")+ with gateway_from_environment() as shared, owned_proxy(+ shared,+ directory,+ {+ "HTTP_PROXY": forward.url,+ "HTTPS_PROXY": forward.url,+ "NO_PROXY": "127.0.0.1,localhost",+ "PROXY_BATCH_POLLING_ENABLED": "false",+ },+ config=_config(directory),+ remove_environment=_scrubbed(),+ ) as candidate:+ yield candidate+++def _targets_after(forward: ForwardProxy, call: Callable[[], object]) -> frozenset[str]:+ forward.drain()+ call()+ targets: Final = frozenset(forward.targets())+ assert targets, "the forward proxy saw no outbound request"+ return targets+++def _chat(isolated: Gateway, model: str) -> object:+ return isolated.request(+ "POST", "/v1/chat/completions", {"model": model, "messages": [{"role": "user", "content": new_marker()}]}+ )+++def _messages(isolated: Gateway, model: str, path: str) -> object:+ return isolated.request(+ "POST",+ path,+ {"model": model, "max_tokens": 16, "messages": [{"role": "user", "content": new_marker()}]},+ headers={"anthropic-version": "2023-06-01"},+ )+++def test_embedding_host_named_embeddings_gets_the_route_appended(isolated: Gateway, forward: ForwardProxy) -> None:+ targets: Final = _targets_after(+ forward, lambda: isolated.request("POST", "/v1/embeddings", {"model": _EMBEDDINGS, "input": new_marker()})+ )+ assert targets == {"http://embeddings/embeddings"}+++def test_rerank_host_named_rerank_gets_the_route_appended(isolated: Gateway, forward: ForwardProxy) -> None:+ body: Final[dict[str, JsonValue]] = {"model": _RERANK, "query": new_marker(), "documents": ["only document"]}+ targets: Final = _targets_after(forward, lambda: isolated.request("POST", "/v1/rerank", body))+ assert targets == {"http://rerank/rerank"}+++def test_azure_claude_chat_on_a_host_starting_with_anthropic_keeps_the_host(+ isolated: Gateway, forward: ForwardProxy+) -> None:+ targets: Final = _targets_after(forward, lambda: _chat(isolated, _AZURE_CLAUDE))+ assert targets == {f"http://{_AZURE_HOST}/anthropic/v1/messages"}+++def test_azure_claude_messages_on_a_host_starting_with_anthropic_keeps_the_host(+ isolated: Gateway, forward: ForwardProxy+) -> None:+ targets: Final = _targets_after(forward, lambda: _messages(isolated, _AZURE_CLAUDE, "/v1/messages"))+ assert targets == {f"http://{_AZURE_HOST}/anthropic/v1/messages"}+++def test_azure_claude_count_tokens_on_a_bare_host_starting_with_anthropic_adds_the_anthropic_path(+ isolated: Gateway, forward: ForwardProxy+) -> None:+ targets: Final = _targets_after(forward, lambda: _messages(isolated, _AZURE_COUNT, "/v1/messages/count_tokens"))+ assert targets == {f"http://{_AZURE_HOST}/anthropic/v1/messages/count_tokens"}+++def test_empty_api_base_falls_back_to_the_provider_default_host(isolated: Gateway, forward: ForwardProxy) -> None:+ targets: Final = _targets_after(forward, lambda: _chat(isolated, _EMPTY_BASE))+ assert targets == {"api.anthropic.com:443"}diff --git a/tests/integration/providers/test_api_base_url_join_wire.py b/tests/integration/providers/test_api_base_url_join_wire.pynew file mode 100644index 00000000000..42e53ff9f95--- /dev/null+++ b/tests/integration/providers/test_api_base_url_join_wire.py@@ -0,0 +1,373 @@+import json+from typing import Final+from urllib.parse import urlsplit++import anthropic+import openai+import pytest+from integration._support.client import Gateway, Scenario, eventually, list_value, object_value, string_value+from integration._support.database import read_rows+from integration._support.wire import Reply, Request, wire_server+from integration.providers._api_base_trailing_slash_support import (+ KEY,+ TRAILING,+ TRAILING_IDS,+ VECTOR,+ Row,+ assert_control_answers,+ assert_one_request,+ body_of,+ first_item,+ new_marker,+ reply_text,+ request_json,+)+from integration.providers._api_base_url_join_support import COUNTED_TOKENS, RELEVANCE, join_peer+from pydantic import JsonValue++_HAIKU: Final = "anthropic.claude-haiku-5-5"+_ANTHROPIC: Final = Row("anthropic/claude-haiku-5-5", "/an", "/v1/messages")+_GEMINI: Final = Row("gemini/gemini-3.8-flash", "/gm/v1beta", "/models/gemini-3.8-flash:generateContent")+_MISTRAL: Final = Row("mistral/mistral-small-latest", "/mi/v1", "/chat/completions")+_BEDROCK_INVOKE: Final = Row(f"bedrock/invoke/{_HAIKU}", "/bi", f"/model/{_HAIKU}/invoke")+_CHAT_ROWS: Final = (+ _ANTHROPIC,+ _GEMINI,+ _MISTRAL,+ Row("databricks/databricks-meta-llama-3-3-70b-instruct", "/db/serving-endpoints", "/chat/completions"),+ _BEDROCK_INVOKE,+ Row(f"bedrock/converse/{_HAIKU}", "/bc", f"/model/{_HAIKU}/converse"),+)+_STREAM_ROWS: Final = (+ _ANTHROPIC,+ Row("gemini/gemini-3.8-flash", "/gm/v1beta", "/models/gemini-3.8-flash:streamGenerateContent?alt=sse"),+ _MISTRAL,+ Row(f"bedrock/converse/{_HAIKU}", "/bc", f"/model/{_HAIKU}/converse-stream"),+)+_MESSAGES_ROWS: Final = (_ANTHROPIC, _BEDROCK_INVOKE, _GEMINI, _MISTRAL)+_RESPONSES_ROWS: Final = (_ANTHROPIC, _GEMINI)+_EMBEDDING_ROWS: Final = (+ Row("mistral/mistral-embed", "/mi/v1", "/embeddings"),+ Row("gemini/gemini-embedding-2", "/gm/v1beta", "/models/gemini-embedding-2:batchEmbedContents"),+)+_TRANSCRIPTION: Final = Row("mistral/voxtral-mini-latest", "/mi/v1", "/audio/transcriptions")+_FULL_ROUTE_SUFFIXES: Final = ("", "/", "?beta=true")+_FULL_ROUTE_IDS: Final = ("exact", "slash", "query")+++def _ids(rows: tuple[Row, ...]) -> tuple[str, ...]:+ return tuple(row.model for row in rows)+++def _deploy(scenario: Scenario, row: Row, api_base: str) -> str:+ if row.model.startswith("bedrock/"):+ return scenario.model(model=row.model, api_base=api_base, api_key=KEY, aws_region_name="us-east-1")+ return scenario.model(model=row.model, api_base=api_base, api_key=KEY)+++def _openai(gateway: Gateway) -> openai.OpenAI:+ return openai.OpenAI(base_url=str(gateway.client.base_url) + "/v1", api_key=gateway.key, max_retries=0)+++def _anthropic(gateway: Gateway) -> anthropic.Anthropic:+ return anthropic.Anthropic(base_url=str(gateway.client.base_url), api_key=gateway.key, max_retries=0)+++def _chat(gateway: Gateway, model: str, marker: str) -> str | None:+ completion: Final = _openai(gateway).chat.completions.create(+ model=model, messages=[{"role": "user", "content": marker}]+ )+ return completion.choices[0].message.content+++def _message_text(gateway: Gateway, model: str, marker: str) -> list[str]:+ message: Final = _anthropic(gateway).messages.create(+ model=model, max_tokens=16, messages=[{"role": "user", "content": marker}]+ )+ return [block.text for block in message.content if block.type == "text"]+++def _spend_rows(pattern: str) -> list[dict[str, JsonValue]]:+ return read_rows(+ 'SELECT request_id, api_base, cache_hit FROM "LiteLLM_SpendLogs" WHERE request_id LIKE %s', (pattern,)+ )+++def _one_spend_row(pattern: str) -> dict[str, JsonValue]:+ (row,) = eventually(lambda: _spend_rows(pattern), lambda rows: len(rows) == 1, seconds=70)+ return row+++def _gemini_contents(marker: str) -> dict[str, JsonValue]:+ return {"contents": [{"role": "user", "parts": [{"text": marker}]}]}+++def _gemini_text(payload: dict[str, JsonValue]) -> JsonValue:+ content: Final = object_value(first_item(payload["candidates"])["content"])+ return first_item(content["parts"])["text"]++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)[email protected]("row", _CHAT_ROWS, ids=_ids(_CHAT_ROWS))+def test_chat_completion_reaches_one_single_slash_url(gateway: Gateway, row: Row, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, row, f"{wire.url}{row.base}{trailing}")+ assert _chat(gateway, model, marker) == reply_text(marker)+ assert_one_request(wire.drain(), row.target, marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)[email protected]("row", _STREAM_ROWS, ids=_ids(_STREAM_ROWS))+def test_streamed_chat_reaches_one_single_slash_url(gateway: Gateway, row: Row, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, row, f"{wire.url}{row.base}{trailing}")+ stream: Final = _openai(gateway).chat.completions.create(+ model=model, messages=[{"role": "user", "content": marker}], stream=True+ )+ text: Final = "".join(chunk.choices[0].delta.content or "" for chunk in stream if chunk.choices)+ assert text == reply_text(marker)+ assert_one_request(wire.drain(), row.target, marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)[email protected]("row", _MESSAGES_ROWS, ids=_ids(_MESSAGES_ROWS))+def test_messages_reaches_one_single_slash_url(gateway: Gateway, row: Row, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, row, f"{wire.url}{row.base}{trailing}")+ assert _message_text(gateway, model, marker) == [reply_text(marker)]+ assert_one_request(wire.drain(), row.target, marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)+def test_streamed_messages_reach_one_single_slash_url(gateway: Gateway, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an{trailing}")+ with _anthropic(gateway).messages.stream(+ model=model, max_tokens=16, messages=[{"role": "user", "content": marker}]+ ) as stream:+ text: Final = "".join(stream.text_stream)+ assert text == reply_text(marker)+ assert request_json(assert_one_request(wire.drain(), "/an/v1/messages", marker))["stream"] is True++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)[email protected]("row", _RESPONSES_ROWS, ids=_ids(_RESPONSES_ROWS))+def test_responses_bridge_reaches_one_single_slash_url(gateway: Gateway, row: Row, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, row, f"{wire.url}{row.base}{trailing}")+ assert _openai(gateway).responses.create(model=model, input=marker).output_text == reply_text(marker)+ assert_one_request(wire.drain(), row.target, marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)[email protected]("row", _EMBEDDING_ROWS, ids=_ids(_EMBEDDING_ROWS))+def test_embedding_reaches_one_single_slash_url(gateway: Gateway, row: Row, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, row, f"{wire.url}{row.base}{trailing}")+ response: Final = gateway.request("POST", "/v1/embeddings", {"model": model, "input": marker})+ assert first_item(body_of(response)["data"])["embedding"] == list(VECTOR)+ assert_one_request(wire.drain(), row.target, marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)+def test_mistral_transcription_reaches_one_single_slash_url(gateway: Gateway, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _TRANSCRIPTION, f"{wire.url}/mi/v1{trailing}")+ response: Final = gateway.request_multipart(+ "/v1/audio/transcriptions",+ {"model": model},+ {"file": ("speech.wav", f"RIFF {marker}".encode(), "audio/wav")},+ )+ assert body_of(response)["text"] == reply_text(marker)+ assert_one_request(wire.drain(), _TRANSCRIPTION.target, marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)+def test_google_genai_generate_content_reaches_one_single_slash_url(gateway: Gateway, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _GEMINI, f"{wire.url}/gm/v1beta{trailing}")+ response: Final = gateway.request(+ "POST", f"/v1beta/models/{model}:generateContent", _gemini_contents(marker)+ )+ assert _gemini_text(body_of(response)) == reply_text(marker)+ assert_one_request(wire.drain(), _GEMINI.target, marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)+def test_google_genai_stream_generate_content_reaches_one_single_slash_url(gateway: Gateway, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _GEMINI, f"{wire.url}/gm/v1beta{trailing}")+ response: Final = gateway.request(+ "POST",+ f"/v1beta/models/{model}:streamGenerateContent",+ _gemini_contents(marker),+ params={"alt": "sse"},+ )+ assert response.status_code == 200, response.text+ assert marker in response.text, response.text+ assert_one_request(+ wire.drain(), "/gm/v1beta/models/gemini-3.8-flash:streamGenerateContent?alt=sse", marker+ )++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)+def test_count_tokens_reaches_one_single_slash_url(gateway: Gateway, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an{trailing}")+ response: Final = gateway.request(+ "POST", "/v1/messages/count_tokens", {"model": model, "messages": [{"role": "user", "content": marker}]}+ )+ assert body_of(response)["input_tokens"] == COUNTED_TOKENS+ assert_one_request(wire.drain(), "/an/v1/messages/count_tokens", marker)++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)+def test_health_check_reaches_one_single_slash_url(gateway: Gateway, trailing: str) -> None:+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an{trailing}")+ report: Final = body_of(gateway.request("GET", "/health", params={"model": model}))+ assert (report["healthy_count"], report["unhealthy_count"]) == (1, 0), report+ assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/an/v1/messages")]++[email protected]("trailing", TRAILING, ids=TRAILING_IDS)+def test_spend_log_records_the_single_slash_url_it_called(gateway: Gateway, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an{trailing}")+ response: Final = gateway.request(+ "POST", "/v1/chat/completions", {"model": model, "messages": [{"role": "user", "content": marker}]}+ )+ identity: Final = string_value(body_of(response)["id"])+ assert_one_request(wire.drain(), "/an/v1/messages", marker)+ assert _one_spend_row(identity)["api_base"] == f"{wire.url}/an/v1/messages"+++def test_cache_hit_on_a_slash_base_skips_the_upstream_and_logs_its_own_row(gateway: Gateway) -> None:+ pytest.skip("BUG: a response-cache hit writes two cache_hit spend rows for one chat completion")+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an/")+ body: Final[dict[str, JsonValue]] = {"model": model, "messages": [{"role": "user", "content": marker}]}+ first: Final = body_of(gateway.request("POST", "/v1/chat/completions", body))+ second: Final = body_of(gateway.request("POST", "/v1/chat/completions", body))+ assert first_item(second["choices"])["message"] == first_item(first["choices"])["message"]+ assert_one_request(wire.drain(), "/an/v1/messages", marker)+ identity: Final = string_value(first["id"])+ assert _one_spend_row(identity)["api_base"] == f"{wire.url}/an/v1/messages"+ hit: Final = _one_spend_row(f"{identity}_cache_hit%")+ assert hit["cache_hit"] == "True", hit+ assert "//" not in urlsplit(string_value(hit["api_base"])).path, hit++[email protected]("suffix", _FULL_ROUTE_SUFFIXES, ids=_FULL_ROUTE_IDS)+def test_chat_base_that_already_names_the_route_keeps_its_path_and_query(gateway: Gateway, suffix: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an/v1/messages{suffix}")+ assert _chat(gateway, model, marker) == reply_text(marker)+ assert_one_request(wire.drain(), f"/an/v1/messages{suffix.rstrip('/')}", marker)++[email protected]("suffix", _FULL_ROUTE_SUFFIXES, ids=_FULL_ROUTE_IDS)+def test_messages_base_that_already_names_the_route_keeps_its_path_and_query(gateway: Gateway, suffix: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an/v1/messages{suffix}")+ assert _message_text(gateway, model, marker) == [reply_text(marker)]+ assert_one_request(wire.drain(), f"/an/v1/messages{suffix.rstrip('/')}", marker)++[email protected]("suffix", ("", "/", "?tier=a"), ids=("exact", "slash", "query"))+def test_rerank_base_that_already_names_the_route_keeps_its_path_and_query(gateway: Gateway, suffix: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, Row("cohere/rerank-v3.5", "/co", ""), f"{wire.url}/co/v2/rerank{suffix}")+ response: Final = gateway.request(+ "POST", "/v1/rerank", {"model": model, "query": marker, "documents": ["only document"]}+ )+ assert [object_value(result)["relevance_score"] for result in list_value(body_of(response)["results"])] == [+ RELEVANCE+ ]+ assert_one_request(wire.drain(), f"/co/v2/rerank{suffix.rstrip('/')}", marker)+++ ("row", "trailing"),+ (+ (_ANTHROPIC, "//"),+ (Row("anthropic/claude-haiku-5-5", f"/an/{'p' * 5000}", "/v1/messages"), "/"),+ (_GEMINI, "//"),+ (Row("gemini/gemini-3.8-flash", f"/gm/{'p' * 5000}", "/models/gemini-3.8-flash:generateContent"), "/"),+ ),+ ids=("anthropic-two-slashes", "anthropic-5kb-prefix", "gemini-two-slashes", "gemini-5kb-prefix"),+)+def test_base_with_repeated_slashes_or_a_long_prefix_reaches_one_url(gateway: Gateway, row: Row, trailing: str) -> None:+ marker: Final = new_marker()+ with wire_server(join_peer) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, row, f"{wire.url}{row.base}{trailing}")+ assert _chat(gateway, model, marker) == reply_text(marker)+ assert_one_request(wire.drain(), row.target, marker)+++def _anthropic_error(status: int, marker: str) -> bytes:+ return json.dumps(+ {"type": "error", "error": {"type": "authentication_error", "message": f"scripted {status} {marker}"}}+ ).encode()+++def _gemini_error(status: int, marker: str) -> bytes:+ error: Final = {"code": status, "message": f"scripted {status} {marker}", "status": "NOT_FOUND"}+ return json.dumps({"error": error}).encode()+++ ("row", "status"),+ ((_ANTHROPIC, 401), (_GEMINI, 404)),+ ids=("anthropic-401", "gemini-404"),+)+def test_upstream_error_on_a_slash_base_reaches_the_caller_from_one_url(+ gateway: Gateway, row: Row, status: int+) -> None:+ marker: Final = new_marker()+ error: Final = _anthropic_error(status, marker) if row is _ANTHROPIC else _gemini_error(status, marker)++ def respond(_: Request) -> Reply:+ return Reply(status=status, body=error)++ with wire_server(respond) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, row, f"{wire.url}{row.base}/")+ response: Final = gateway.request(+ "POST", "/v1/chat/completions", {"model": model, "messages": [{"role": "user", "content": marker}]}+ )+ assert response.status_code == status, response.text+ assert marker in response.text, response.text+ assert_one_request(wire.drain(), row.target, marker)+ assert_control_answers(gateway, scenario)+++def test_streamed_chat_upstream_error_on_a_slash_base_reaches_the_caller(gateway: Gateway) -> None:+ marker: Final = new_marker()++ def respond(_: Request) -> Reply:+ return Reply(status=401, body=_anthropic_error(401, marker))++ with wire_server(respond) as wire, gateway.scenario() as scenario:+ model: Final = _deploy(scenario, _ANTHROPIC, f"{wire.url}/an/")+ with pytest.raises(openai.AuthenticationError) as raised:+ _openai(gateway).chat.completions.create(+ model=model, messages=[{"role": "user", "content": marker}], stream=True+ )+ assert marker in str(raised.value)+ assert_one_request(wire.drain(), "/an/v1/messages", marker)+ assert_control_answers(gateway, scenario)diff --git a/tests/unit/litellm_core_utils/test_url_utils.py b/tests/unit/litellm_core_utils/test_url_utils.pyindex 46bb91959c0..9b29eac208e 100644--- a/tests/unit/litellm_core_utils/test_url_utils.py+++ b/tests/unit/litellm_core_utils/test_url_utils.py@@ -1,6 +1,8 @@import asyncioimport socketimport threading+from collections.abc import Callable+from urllib.parse import urlsplitimport httpximport pytest@@ -13,6 +15,8 @@ from litellm.litellm_core_utils.url_utils import (assert_same_origin,encode_url_path_segment,encode_url_path_segments,+ ensure_api_route,+ join_api_base,validate_url,)@@ -571,3 +575,80 @@ async def test_async_safe_get_resolves_dns_off_the_event_loop(monkeypatch):assert response.status_code == 200assert resolver_threads and all(thread is not loop_thread for thread in resolver_threads)assert released_by_the_loop and all(released_by_the_loop)+++ ("api_base", "route", "expected"),+ [+ ("https://gw.test/p/", "v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/p//", "v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/p", "v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/p", "/v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/p/", "/v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/p/v1/messages", "v1/messages", "https://gw.test/p/v1/messages/v1/messages"),+ ("https://api.test/v1/evals/cancel", "cancel", "https://api.test/v1/evals/cancel/cancel"),+ ("http://embeddings", "embeddings", "http://embeddings/embeddings"),+ ("http:/", "v1/files", "http://v1/files"),+ ("https://gw.test/p/?x=1", "v1/messages", "https://gw.test/p?x=1/v1/messages"),+ ],+ ids=(+ "trailing-slash-base",+ "repeated-trailing-slashes",+ "base-without-slash",+ "route-with-leading-slash",+ "slashes-on-both-sides",+ "base-already-ends-in-route",+ "id-equal-to-the-route",+ "host-named-like-the-route",+ "host-stripped-as-a-suffix-leaves-only-the-scheme",+ "slash-before-the-query-matches-the-base-without-it",+ ),+)+def test_join_api_base_always_appends_the_route_after_exactly_one_slash(+ api_base: str, route: str, expected: str+) -> None:+ assert join_api_base(api_base, route) == expected++[email protected]("api_base", ["https://", "http://", "https:///"])[email protected]("route", ["v1/messages", "models/gemini-flash:generateContent"])[email protected]("join", [join_api_base, ensure_api_route], ids=["join_api_base", "ensure_api_route"])+def test_a_base_that_is_only_a_scheme_never_turns_the_route_into_the_host(+ api_base: str, route: str, join: Callable[[str, str], str]+) -> None:+ assert urlsplit(join(api_base, route)).netloc == ""+++ ("api_base", "route", "expected"),+ [+ ("https://gw.test/p/v1/messages", "v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/p/v1/messages/", "/v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/x/listen", "listen", "https://gw.test/x/listen"),+ ("https://gw.test/p/", "v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/p", "/v1/messages", "https://gw.test/p/v1/messages"),+ ("https://gw.test/research", "search", "https://gw.test/research/search"),+ ("http://embeddings", "embeddings", "http://embeddings/embeddings"),+ ("http://embeddings:8080/", "embeddings", "http://embeddings:8080/embeddings"),+ ("http:/", "anthropic/v1/messages", "http://anthropic/v1/messages"),+ ("https://gw.test/p/v1/messages/?beta=true", "v1/messages", "https://gw.test/p/v1/messages?beta=true"),+ ("https://gw.test/p/v1/messages?beta=true", "v1/messages", "https://gw.test/p/v1/messages?beta=true"),+ ],+ ids=(+ "path-already-ends-in-route",+ "path-ends-in-route-before-trailing-slash",+ "last-segment-is-the-route",+ "trailing-slash-base",+ "route-with-leading-slash",+ "path-ends-in-lookalike-suffix",+ "host-named-like-the-route",+ "host-with-port-named-like-the-route",+ "host-stripped-as-a-suffix-leaves-only-the-scheme",+ "route-path-with-a-slash-before-the-query",+ "route-path-before-the-query",+ ),+)+def test_ensure_api_route_appends_the_route_only_when_the_url_path_lacks_it(+ api_base: str, route: str, expected: str+) -> None:+ assert ensure_api_route(api_base, route) == expecteddiff --git a/tests/unit/llms/azure/realtime/test_azure_realtime_handler.py b/tests/unit/llms/azure/realtime/test_azure_realtime_handler.pyindex 5c1ff1b46a3..f1db76c1e21 100644--- a/tests/unit/llms/azure/realtime/test_azure_realtime_handler.py+++ b/tests/unit/llms/azure/realtime/test_azure_realtime_handler.py@@ -740,3 +740,15 @@ async def test_arealtime_forwards_deployment_azure_ad_token(monkeypatch, no_ambi)assert mock_async_realtime.call_args.kwargs["azure_ad_token"] == "deployment-entra-token"++[email protected]("protocol", [None, "GA"])+def test_construct_url_with_a_trailing_slash_api_base_matches_the_url_without_it(protocol):+ from litellm.llms.azure.realtime.handler import AzureOpenAIRealtime++ def url_for(api_base: str) -> str:+ return AzureOpenAIRealtime().construct_url(+ api_base=api_base, model="gpt-realtime", api_version="2024-10-01-preview", realtime_protocol=protocol+ )++ assert url_for("https://gw.example.com/azure/") == url_for("https://gw.example.com/azure")diff --git a/tests/unit/llms/azure/test_azure_common_utils.py b/tests/unit/llms/azure/test_azure_common_utils.pyindex b3bd1b8743a..1dc7ff74323 100644--- a/tests/unit/llms/azure/test_azure_common_utils.py+++ b/tests/unit/llms/azure/test_azure_common_utils.py@@ -1702,6 +1702,24 @@ def test_azure_v1_api_uses_openai_client(api_version):), f"base_url should contain /openai/v1/, got {async_client.base_url}"+ "api_base",+ [+ "https://test.openai.azure.com",+ "https://test.openai.azure.com/",+ "https://test.openai.azure.com/openai/v1",+ "https://test.openai.azure.com/openai/v1/",+ ],+)[email protected]("is_async", [False, True])+def test_azure_v1_api_client_targets_openai_v1_once_whatever_the_api_base_ends_in(api_base: str, is_async: bool):+ client: Final = BaseAzureLLM().get_azure_openai_client(+ api_key="test-key", api_base=api_base, api_version="v1", _is_async=is_async+ )++ assert str(client.base_url) == "https://test.openai.azure.com/openai/v1/"++@pytest.mark.parametrize("api_version", ["v1", "latest", "preview"])def test_azure_v1_api_with_azure_ad_token_provider(api_version):"""diff --git a/tests/unit/llms/azure_ai/agents/test_handler.py b/tests/unit/llms/azure_ai/agents/test_handler.pyindex f92f5359c49..71466bb172c 100644--- a/tests/unit/llms/azure_ai/agents/test_handler.py+++ b/tests/unit/llms/azure_ai/agents/test_handler.py@@ -22,10 +22,10 @@ def _httpx_only_transport(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setenv("DISABLE_AIOHTTP_TRANSPORT", "True")-def _assistants_api(thread_id: str, answer: str) -> respx.MockRouter:- thread_path: Final = f"/api/projects/litellm-test/threads/{thread_id}"+def _assistants_api(thread_id: str, answer: str, project_path: str = "/api/projects/litellm-test") -> respx.MockRouter:+ thread_path: Final = f"{project_path}/threads/{thread_id}"router: Final = respx.mock(assert_all_called=False)- router.post(path="/api/projects/litellm-test/threads").respond(+ router.post(path=f"{project_path}/threads").respond(200, json={"id": thread_id, "object": "thread", "created_at": 1767225600, "metadata": {}})router.post(path=f"{thread_path}/messages").respond(@@ -116,6 +116,23 @@ async def test_acompletion_creates_thread_posts_message_runs_and_returns_the_repassert response._hidden_params["thread_id"] == "thread_new"+async def test_acompletion_creates_the_thread_under_a_project_named_threads() -> None:+ router: Final = _assistants_api("thread_new", "hello", project_path="/api/projects/threads")++ with router:+ await litellm.acompletion(+ model=f"azure_ai/agents/{AGENT_ID}",+ messages=[{"role": "user", "content": "hi"}],+ api_base="https://agents-test.services.ai.azure.com/api/projects/threads",+ api_key="azure-ad-token",+ stream=False,+ )+ recorded: Final = _recorded(router)++ assert recorded[0][:2] == ("POST", f"/api/projects/threads/threads?api-version={API_VERSION}")++@pytest.mark.asyncioasync def test_handler_acompletion_with_thread_id_reuses_that_thread() -> None:router: Final = _assistants_api("thread_existing", "Your name is Alice.")diff --git a/tests/unit/llms/azure_ai/claude/test_azure_anthropic_count_tokens_transformation.py b/tests/unit/llms/azure_ai/claude/test_azure_anthropic_count_tokens_transformation.pyindex 4b317cff975..8971bd31a60 100644--- a/tests/unit/llms/azure_ai/claude/test_azure_anthropic_count_tokens_transformation.py+++ b/tests/unit/llms/azure_ai/claude/test_azure_anthropic_count_tokens_transformation.py@@ -4,8 +4,9 @@ Tests for Azure AI Anthropic CountTokens transformation.Verifies that the CountTokens API uses the correct authentication headers."""+from typing import Final-+import pytestfrom litellm.llms.azure_ai.anthropic.count_tokens.transformation import (AzureAIAnthropicCountTokensConfig,@@ -105,3 +106,22 @@ class TestAzureAIAnthropicCountTokensConfig:endpoint== "https://my-resource.services.ai.azure.com/anthropic/v1/messages/count_tokens")++ def test_get_count_tokens_endpoint_for_a_resource_named_anthropic(self):+ config: Final = AzureAIAnthropicCountTokensConfig()++ endpoint: Final = config.get_count_tokens_endpoint("https://anthropic-res.services.ai.azure.com")++ assert endpoint == "https://anthropic-res.services.ai.azure.com/anthropic/v1/messages/count_tokens"++ @pytest.mark.parametrize(+ "api_base",+ [+ "https://my-resource.services.ai.azure.com/anthropic/v1/messages",+ "https://my-resource.services.ai.azure.com/anthropic/v1/messages/",+ ],+ )+ def test_get_count_tokens_endpoint_sits_next_to_the_messages_url(self, api_base: str):+ endpoint: Final = AzureAIAnthropicCountTokensConfig().get_count_tokens_endpoint(api_base)++ assert endpoint == "https://my-resource.services.ai.azure.com/anthropic/v1/messages/count_tokens"diff --git a/tests/unit/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.py b/tests/unit/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.pyindex 38483b8a1fa..5e0395f84dc 100644--- a/tests/unit/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.py+++ b/tests/unit/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.py@@ -198,6 +198,17 @@ class TestAzureAnthropicMessagesConfig:assert url == "https://test.services.ai.azure.com/anthropic/v1/messages"+ def test_get_complete_url_adds_the_anthropic_path_for_a_host_named_anthropic(self):+ url: Final = AzureAnthropicMessagesConfig().get_complete_url(+ api_base="https://anthropic-res.services.ai.azure.com",+ api_key="test-api-key",+ model="claude-sonnet-4-5",+ optional_params={},+ litellm_params={},+ )++ assert url == "https://anthropic-res.services.ai.azure.com/anthropic/v1/messages"+def test_get_complete_url_raises_error_when_api_base_missing(self):"""Test get_complete_url raises error when api_base is None"""config = AzureAnthropicMessagesConfig()diff --git a/tests/unit/llms/bedrock/chat/agentcore/test_agentcore_transformation.py b/tests/unit/llms/bedrock/chat/agentcore/test_agentcore_transformation.pyindex 053ba477212..413061a732d 100644--- a/tests/unit/llms/bedrock/chat/agentcore/test_agentcore_transformation.py+++ b/tests/unit/llms/bedrock/chat/agentcore/test_agentcore_transformation.py@@ -1269,3 +1269,17 @@ def test_agentcore_runtime_response_becomes_assistant_message(assert json.loads(route.calls[0].request.content)["prompt"] == "Explain machine learning in simple terms"assert response.choices[0].message.content == "Machine learning lets computers learn from data."assert response.choices[0].finish_reason == "stop"+++def test_get_complete_url_sends_a_non_string_runtime_endpoint_to_that_endpoint():+ vpc_endpoint: Final = "https://vpce-1.bedrock-agentcore.us-west-2.vpce.amazonaws.com"++ url: Final = AmazonAgentCoreConfig().get_complete_url(+ api_base=None,+ api_key=None,+ model="agentcore/arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/my-agent",+ optional_params={"aws_bedrock_runtime_endpoint": httpx.URL(f"{vpc_endpoint}/")},+ litellm_params={},+ )++ assert url.startswith(f"{vpc_endpoint}/runtimes/")diff --git a/tests/unit/llms/openai/evals/test_openai_evals_transformation.py b/tests/unit/llms/openai/evals/test_openai_evals_transformation.pyindex f359f214f97..ed8f86d4727 100644--- a/tests/unit/llms/openai/evals/test_openai_evals_transformation.py+++ b/tests/unit/llms/openai/evals/test_openai_evals_transformation.py@@ -264,6 +264,17 @@ def test_transform_cancel_eval_request(config: OpenAIEvalsConfig):assert request_body == {}+def test_transform_cancel_eval_request_appends_cancel_to_an_eval_id_named_cancel(config: OpenAIEvalsConfig) -> None:+ eval_url, _ = config.transform_get_eval_request(+ eval_id="cancel", api_base="https://api.openai.com", litellm_params=GenericLiteLLMParams(), headers={}+ )+ cancel_url, _, _ = config.transform_cancel_eval_request(+ eval_id="cancel", api_base="https://api.openai.com", litellm_params=GenericLiteLLMParams(), headers={}+ )++ assert cancel_url == f"{eval_url}/cancel"++def test_transform_cancel_eval_response(config: OpenAIEvalsConfig):"""Test transformation of cancel eval response"""response = httpx.Response(diff --git a/tests/unit/llms/test_api_base_trailing_slash_routes.py b/tests/unit/llms/test_api_base_trailing_slash_routes.pynew file mode 100644index 00000000000..ce8cecf11a5--- /dev/null+++ b/tests/unit/llms/test_api_base_trailing_slash_routes.py@@ -0,0 +1,119 @@+import asyncio+import contextlib+from collections.abc import Callable, Iterator, Mapping+from types import MappingProxyType+from typing import Final++import pytest+import respx++import litellm+from litellm.evals.main import cancel_run, create_run, get_run++FAKE_KEY: Final = "sk-fake"+FAKE_AWS: Final[Mapping[str, str]] = MappingProxyType(+ {"aws_access_key_id": "AKIAFAKE", "aws_secret_access_key": "fake-secret", "aws_region_name": "us-east-1"}+)+MESSAGES: Final = ({"role": "user", "content": "hi"},)+GATEWAY_PATH: Final = "/gw/"+++GATEWAY_HOST: Final = "gateway.test"+++def received_paths() -> Iterator[Callable[[], tuple[str, ...]]]:+ with respx.mock(assert_all_called=False) as router:+ route: Final = router.route(host=GATEWAY_HOST).respond(+ 404, json={"error": {"message": "not found", "code": 404}}+ )+ yield lambda: tuple(call.request.url.raw_path.decode().split("?", 1)[0] for call in route.calls)+++def _drain(result: object) -> None:+ if isinstance(result, litellm.CustomStreamWrapper):+ for _ in result:+ pass+++def _completion(model: str, stream: bool = False, **kwargs: object) -> Callable[[str], object]:+ return lambda api_base: _drain(+ litellm.completion(model=model, messages=list(MESSAGES), api_base=api_base, stream=stream, timeout=5, **kwargs)+ )+++def _embedding(model: str, **kwargs: object) -> Callable[[str], object]:+ return lambda api_base: litellm.embedding(model=model, input=["hi"], api_base=api_base, timeout=5, **kwargs)+++ROUTES: Final[Mapping[str, Callable[[str], object]]] = MappingProxyType(+ {+ "anthropic-chat": _completion("anthropic/claude-opus-5-5", api_key=FAKE_KEY),+ "bedrock-converse": _completion("bedrock/converse/anthropic.claude-sonnet-5-5", **FAKE_AWS),+ "bedrock-converse-stream": _completion("bedrock/converse/anthropic.claude-sonnet-5-5", stream=True, **FAKE_AWS),+ "gemini-chat": _completion("gemini/gemini-pro-latest", api_key=FAKE_KEY),+ "gemini-chat-stream": _completion("gemini/gemini-pro-latest", stream=True, api_key=FAKE_KEY),+ "mistral-chat": _completion("mistral/mistral-large-latest", api_key=FAKE_KEY),+ "gigachat-chat": _completion("gigachat/GigaChat-2-Max", api_key=FAKE_KEY),+ "bedrock-titan-embedding": _embedding("bedrock/amazon.titan-embed-text-v2:0", **FAKE_AWS),+ "gemini-embedding": _embedding("gemini/gemini-embedding-2", api_key=FAKE_KEY),+ "gigachat-embedding": _embedding("gigachat/Embeddings-2", api_key=FAKE_KEY),+ "volcengine-embedding": _embedding("volcengine/doubao-embedding-text-240715", api_key=FAKE_KEY),+ "ollama-embedding": _embedding("ollama/nomic-embed-text"),+ "oobabooga-chat": _completion("oobabooga/local-model"),+ "oobabooga-embedding": _embedding("oobabooga/local-model"),+ "bedrock-rerank": lambda api_base: litellm.rerank(+ model="bedrock/arn:aws:bedrock:us-west-2::foundation-model/amazon.rerank-v1:0",+ query="hi",+ documents=["a"],+ api_base=api_base,+ **FAKE_AWS,+ ),+ "bedrock-image-generation": lambda api_base: litellm.image_generation(+ model="bedrock/amazon.nova-canvas-v1:0", prompt="a cat", api_base=api_base, timeout=5, **FAKE_AWS+ ),+ "gemini-interaction-get": lambda api_base: litellm.interactions.get(+ interaction_id="interaction-1", api_base=api_base, api_key=FAKE_KEY+ ),+ "gemini-interaction-delete": lambda api_base: litellm.interactions.delete(+ interaction_id="interaction-1", api_base=api_base, api_key=FAKE_KEY+ ),+ "gemini-interaction-cancel": lambda api_base: litellm.interactions.cancel(+ interaction_id="interaction-1", api_base=api_base, api_key=FAKE_KEY+ ),+ "openai-eval-run-create": lambda api_base: create_run(+ eval_id="eval-1", data_source={"type": "jsonl"}, api_base=api_base, api_key=FAKE_KEY+ ),+ "openai-eval-run-get": lambda api_base: get_run(+ eval_id="eval-1", run_id="run-1", api_base=api_base, api_key=FAKE_KEY+ ),+ "openai-eval-run-cancel": lambda api_base: cancel_run(+ eval_id="eval-1", run_id="run-1", api_base=api_base, api_key=FAKE_KEY+ ),+ "edenai-model-list": lambda api_base: litellm.get_valid_models(+ check_provider_endpoint=True, custom_llm_provider="edenai", api_key=FAKE_KEY, api_base=api_base+ ),+ "gemini-rag-ingest": lambda api_base: asyncio.run(+ litellm.aingest(+ ingest_options={+ "vector_store": {"custom_llm_provider": "gemini", "api_base": api_base, "api_key": FAKE_KEY}+ },+ file_data=("notes.txt", b"hello", "text/plain"),+ )+ ),+ }+)++[email protected]("route", ROUTES.values(), ids=ROUTES.keys())+def test_api_base_with_a_trailing_slash_reaches_the_gateway_path_with_single_slashes(+ received_paths: Callable[[], tuple[str, ...]], route: Callable[[str], object], monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setenv("GIGACHAT_ACCESS_TOKEN", FAKE_KEY)+ monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)+ with contextlib.suppress(Exception):+ route(f"http://{GATEWAY_HOST}{GATEWAY_PATH}")++ paths: Final = received_paths()+ assert paths, "the route never reached the api_base"+ assert all(path.startswith(GATEWAY_PATH) and "//" not in path for path in paths), pathsdiff --git a/tests/unit/llms/test_get_complete_url_trailing_slash.py b/tests/unit/llms/test_get_complete_url_trailing_slash.pynew file mode 100644index 00000000000..9118498c2c2--- /dev/null+++ b/tests/unit/llms/test_get_complete_url_trailing_slash.py@@ -0,0 +1,278 @@+import copy+import importlib+import inspect+import itertools+from collections.abc import Mapping+from dataclasses import dataclass, field+from pathlib import Path+from types import MappingProxyType, ModuleType+from typing import Final+from urllib.parse import urlsplit, urlunsplit++import pytest+from _pytest.mark.structures import ParameterSet++import litellm+from litellm.llms.elevenlabs.text_to_speech.transformation import ElevenLabsTextToSpeechConfig+from litellm.llms.meta.realtime.transformation import MUSE_MODEL+from litellm.llms.openai_like.dynamic_config import create_config_class, create_responses_config_class+from litellm.llms.openai_like.json_loader import SimpleProviderConfig++API_BASE_WITH_TRAILING_SLASH: Final = "https://gw.example.com/p/"+API_BASE_WITHOUT_TRAILING_SLASH: Final = API_BASE_WITH_TRAILING_SLASH.rstrip("/")+LLMS_ROOT: Final = Path(litellm.__file__).parent / "llms"+PACKAGE_ROOT: Final = LLMS_ROOT.parent.parent++ARGUMENTS_BY_NAME: Final[Mapping[str, object]] = MappingProxyType(+ {+ "api_key": "sk-test",+ "model": "test-model",+ "optional_params": {},+ "litellm_params": {"api_key": "sk-test", "vertex_project": "project", "vertex_location": "us-central1"},+ "stream": False,+ "endpoint": "v1/messages",+ "request_query_params": None,+ "data": None,+ "api_version": "2024-01-01",+ "vector_store_id": "vs_test",+ }+)+++SAMPLE_JSON_PROVIDER: Final = SimpleProviderConfig(+ "json-provider", {"base_url": "https://json-provider.example.com", "api_key_env": "UNSET_KEY"}+)+FACTORY_BUILDERS: Final[tuple[type, ...]] = (+ create_config_class(SAMPLE_JSON_PROVIDER),+ create_responses_config_class(SAMPLE_JSON_PROVIDER),+)+++async def _resolve_access_token() -> str:+ return "token"+++@dataclass(frozen=True, slots=True)+class BuilderInputs:+ constructor: Mapping[str, object] = field(default_factory=lambda: MappingProxyType({}))+ call: Mapping[str, object] = field(default_factory=lambda: MappingProxyType({}))+ api_base_option: str | None = None+++INPUTS_BY_BUILDER: Final[Mapping[str, BuilderInputs]] = MappingProxyType(+ {+ "anthropic.skills.transformation.AnthropicSkillsConfig": BuilderInputs(call={"endpoint": "skills"}),+ "azure_ai.image_edit.transformation.AzureFoundryFluxImageEditConfig": BuilderInputs(+ call={"litellm_params": {"api_version": "2025-04-01-preview"}}+ ),+ "bedrock.chat.agentcore.transformation.AmazonAgentCoreConfig": BuilderInputs(+ call={"model": "agentcore/arn:aws:bedrock-agentcore:us-west-2:123456789012:runtime/test-runtime"},+ api_base_option="aws_bedrock_runtime_endpoint",+ ),+ "bedrock.chat.invoke_agent.transformation.AmazonInvokeAgentConfig": BuilderInputs(+ call={"model": "agent/AGENT_ID/ALIAS_ID", "optional_params": {"sessionID": "session-id"}}+ ),+ "black_forest_labs.image_edit.transformation.BlackForestLabsImageEditConfig": BuilderInputs(+ call={"model": "flux-kontext-pro"}+ ),+ "black_forest_labs.image_generation.transformation.BlackForestLabsImageGenerationConfig": BuilderInputs(+ call={"model": "flux-pro-1.1"}+ ),+ "elevenlabs.text_to_speech.transformation.ElevenLabsTextToSpeechConfig": BuilderInputs(+ call={"litellm_params": {ElevenLabsTextToSpeechConfig.ELEVENLABS_VOICE_ID_KEY: "voice-id"}}+ ),+ "meta.realtime.transformation.MetaRealtimeConfig": BuilderInputs(call={"model": MUSE_MODEL}),+ "openai.evals.transformation.OpenAIEvalsConfig": BuilderInputs(call={"endpoint": "evals"}),+ "openai_like.messages.transformation.JSONProviderAnthropicMessagesConfig": BuilderInputs(+ constructor={"provider": SAMPLE_JSON_PROVIDER}+ ),+ "predibase.chat.transformation.PredibaseConfig": BuilderInputs(+ call={"litellm_params": {"predibase_tenant_id": "tenant"}}+ ),+ "ragflow.chat.transformation.RAGFlowConfig": BuilderInputs(call={"model": "ragflow/chat/chat-id/model"}),+ "scaledown.chat.transformation.ScaleDownChatConfig": BuilderInputs(call={"model": "classify"}),+ "vertex_ai.audio_transcription.realtime_transformation.VertexChirpRealtimeConfig": BuilderInputs(+ constructor={"resolve_access_token": _resolve_access_token, "project": "project", "location": None},+ call={"model": "chirp_3"},+ ),+ "vertex_ai.interactions.transformation.VertexAIInteractionsConfig": BuilderInputs(+ constructor={"mint_access_token": lambda _credentials, _project: ("token", "project")}+ ),+ "vertex_ai.realtime.transformation.VertexAIRealtimeConfig": BuilderInputs(+ constructor={"access_token": "token", "project": "project", "location": "us-central1"}+ ),+ "vertex_ai.text_to_speech.transformation.VertexAILyriaTextToSpeechConfig": BuilderInputs(+ call={"model": "lyria-002"}+ ),+ }+)++UNCALLABLE_BUILDERS: Final[Mapping[str, str]] = MappingProxyType(+ {+ "base_llm.search.transformation.BaseSearchConfig": "raises-NotImplementedError-for-providers-to-override",+ "bedrock.image_edit.amazon_nova_canvas_image_edit_transformation.BedrockAmazonNovaCanvasImageEditConfig": (+ "raises-because-BedrockImageEdit-builds-the-url"+ ),+ "sap.chat.transformation.GenAIHubOrchestrationConfig": "url-comes-from-a-live-SAP-deployment-lookup",+ "sap.embed.transformation.GenAIHubEmbeddingConfig": "url-comes-from-a-live-SAP-deployment-lookup",+ "triton.completion.transformation.TritonConfig": "api_base-must-be-the-full-generate-or-infer-url",+ "vertex_ai.rerank.transformation.VertexAIRerankConfig": "ignores-api_base-and-needs-Vertex-credentials",+ }+)+++API_BASE_IS_THE_FULL_URL: Final[Mapping[str, str]] = MappingProxyType(+ {+ builder_id: "sends-the-configured-api_base-as-the-request-url"+ for builder_id in (+ "base_llm.audio_transcription.transformation.BaseAudioTranscriptionConfig",+ "base_llm.chat.transformation.BaseConfig",+ "base_llm.completion.transformation.BaseTextCompletionConfig",+ "base_llm.embedding.transformation.BaseEmbeddingConfig",+ "base_llm.image_generation.transformation.BaseImageGenerationConfig",+ "base_llm.image_variations.transformation.BaseImageVariationConfig",+ "base_llm.vector_store.transformation.BaseDirectVectorStoreConfig",+ "bedrock.realtime.transformation.BedrockRealtimeConfig",+ "bedrock.search.transformation.AgentCoreSearchConfig",+ "brave.search.transformation.BraveSearchConfig",+ "cohere.chat.v2_transformation.CohereV2ChatConfig",+ "cohere.embed.transformation.CohereEmbeddingConfig",+ "dashscope.image_generation.transformation.DashScopeImageGenerationConfig",+ "dataforseo.search.transformation.DataForSEOSearchConfig",+ "datarobot.chat.transformation.DataRobotConfig",+ "duckduckgo.search.transformation.DuckDuckGoSearchConfig",+ "google_pse.search.transformation.GooglePSESearchConfig",+ "searchapi.search.transformation.SearchAPIConfig",+ "tinyfish.search.transformation.TinyfishSearchConfig",+ "vertex_ai.text_to_speech.transformation.VertexAITextToSpeechConfig",+ "vertex_ai.vertex_ai_partner_models.anthropic.experimental_pass_through.transformation."+ "VertexAIPartnerModelsAnthropicMessagesConfig",+ )+ }+)+++HOST_NAMED_LIKE_A_STRIPPED_SUFFIX_IS_NOT_KEPT: Final[Mapping[str, str]] = MappingProxyType(+ {+ "deepinfra.rerank.transformation.DeepinfraRerankConfig": "removes-every-openai-substring-on-main-too",+ "minimax.videos.transformation.MinimaxVideoConfig": "strips-surface-suffixes-to-the-scheme-on-main-too",+ "volcengine.videos.transformation.VolcEngineVideoConfig": "strips-surface-suffixes-to-the-scheme-on-main-too",+ "vertex_ai.audio_transcription.realtime_transformation.VertexChirpRealtimeConfig": "returns-a-grpc-host",+ "azure.realtime.http_transformation.AzureRealtimeHTTPConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ "bedrock.responses.transformation.BedrockOpenAIResponsesConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ "bedrock_mantle.chat.claude_transformation.BedrockMantleClaudeChatConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ "bedrock_mantle.messages.transformation.BedrockMantleAnthropicMessagesConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ "bedrock_mantle.responses.transformation.BedrockMantleResponsesAPIConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ "deepseek.messages.transformation.DeepSeekAnthropicMessagesConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ "ragflow.chat.transformation.RAGFlowConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ "scaledown.chat.transformation.ScaleDownChatConfig": "replaces-the-host-with-a-route-segment-on-main-too",+ }+)+++def _builder_id(cls: type) -> str:+ return f"{cls.__module__.removeprefix('litellm.llms.')}.{cls.__qualname__}"+++def _module_name(path: Path) -> str:+ return ".".join(path.relative_to(PACKAGE_ROOT).with_suffix("").parts)+++def _builder_modules() -> tuple[ModuleType, ...]:+ builder_paths: Final = tuple(path for path in LLMS_ROOT.rglob("*.py") if "def get_complete_url" in path.read_text())+ return tuple(importlib.import_module(_module_name(path)) for path in sorted(builder_paths))+++def _defined_builders(module: ModuleType) -> tuple[type, ...]:+ classes: Final = (*(cls for _, cls in inspect.getmembers(module, inspect.isclass)), *FACTORY_BUILDERS)+ return tuple(cls for cls in classes if cls.__module__ == module.__name__ and "get_complete_url" in vars(cls))+++def _all_builders() -> tuple[type, ...]:+ builders: Final = itertools.chain.from_iterable(_defined_builders(module) for module in _builder_modules())+ return tuple(sorted(builders, key=_builder_id))+++def _skip_reason(cls: type) -> str | None:+ if getattr(vars(cls)["get_complete_url"], "__isabstractmethod__", False):+ return "abstract-get_complete_url"+ return UNCALLABLE_BUILDERS.get(_builder_id(cls))+++def _builder_param(cls: type) -> ParameterSet:+ reason: Final = _skip_reason(cls)+ if reason is None:+ return pytest.param(cls, id=_builder_id(cls))+ return pytest.param(cls, id=f"{_builder_id(cls)}-skipped-{reason}", marks=pytest.mark.skip(reason=reason))+++def _unimplemented(*_args: object, **_kwargs: object) -> object:+ raise NotImplementedError+++def _instantiable(cls: type) -> type:+ abstract_methods: Final[frozenset[str]] = getattr(cls, "__abstractmethods__", frozenset())+ if not abstract_methods:+ return cls+ return type(cls.__name__, (cls,), dict.fromkeys(abstract_methods, _unimplemented))+++def _call_arguments(builder: object, inputs: BuilderInputs, api_base: str) -> dict[str, object]:+ parameters: Final = inspect.signature(getattr(builder, "get_complete_url")).parameters.values()+ named: Final = tuple(p for p in parameters if p.kind not in (p.VAR_POSITIONAL, p.VAR_KEYWORD))+ api_base_options: Final = (+ {} if inputs.api_base_option is None else {"optional_params": {inputs.api_base_option: api_base}}+ )+ values: Final = {**ARGUMENTS_BY_NAME, "api_base": api_base, **inputs.call, **api_base_options}+ missing: Final = tuple(p.name for p in named if p.default is p.empty and p.name not in values)+ assert not missing, f"add a value for {missing} to ARGUMENTS_BY_NAME or INPUTS_BY_BUILDER"+ return {p.name: copy.deepcopy(values[p.name]) for p in named if p.name in values}+++def _built_url(cls: type, api_base: str) -> str:+ inputs: Final = INPUTS_BY_BUILDER.get(_builder_id(cls), BuilderInputs())+ builder: Final = _instantiable(cls)(**inputs.constructor)+ result: Final = getattr(builder, "get_complete_url")(**_call_arguments(builder, inputs, api_base))+ return str(result[0] if isinstance(result, tuple) else result)+++def _without_one_trailing_path_slash(url: str) -> str:+ parts: Final = urlsplit(url)+ return urlunsplit(parts._replace(path=parts.path.removesuffix("/")))++[email protected]("builder_class", [_builder_param(cls) for cls in _all_builders()])+def test_get_complete_url_keeps_one_slash_after_a_trailing_slash_api_base(builder_class: type) -> None:+ url: Final = _built_url(builder_class, API_BASE_WITH_TRAILING_SLASH)++ assert "//" not in urlsplit(url).path, url++[email protected]("builder_class", [_builder_param(cls) for cls in _all_builders()])+def test_get_complete_url_sends_the_same_url_with_or_without_a_trailing_slash(builder_class: type) -> None:+ with_slash: Final = _built_url(builder_class, API_BASE_WITH_TRAILING_SLASH)+ without_slash: Final = _built_url(builder_class, API_BASE_WITHOUT_TRAILING_SLASH)++ if _builder_id(builder_class) in API_BASE_IS_THE_FULL_URL:+ assert _without_one_trailing_path_slash(with_slash) == _without_one_trailing_path_slash(without_slash)+ else:+ assert with_slash == without_slash+++def _host_preserving_builder_param(cls: type) -> ParameterSet:+ reason: Final = HOST_NAMED_LIKE_A_STRIPPED_SUFFIX_IS_NOT_KEPT.get(_builder_id(cls))+ if reason is None:+ return _builder_param(cls)+ return pytest.param(cls, id=f"{_builder_id(cls)}-skipped-{reason}", marks=pytest.mark.skip(reason=reason))++[email protected]("api_base", ["https://v1", "https://api/v1", "https://openai/v1/", "https://anthropic/v1"])[email protected]("builder_class", [_host_preserving_builder_param(cls) for cls in _all_builders()])+def test_get_complete_url_keeps_a_host_named_like_the_suffix_it_strips(builder_class: type, api_base: str) -> None:+ url: Final = _built_url(builder_class, api_base)+ follows_the_api_base_host: Final = (+ urlsplit(_built_url(builder_class, "https://gateway.test/v1")).netloc == "gateway.test"+ )++ assert urlsplit(url).netloc, url+ assert not follows_the_api_base_host or urlsplit(url).netloc == urlsplit(api_base).netloc, urldiff --git a/tests/unit/llms/test_predibase_transformation.py b/tests/unit/llms/test_predibase_transformation.pyindex 20d547c3b63..a02840ed4ef 100644--- a/tests/unit/llms/test_predibase_transformation.py+++ b/tests/unit/llms/test_predibase_transformation.py@@ -1,3 +1,4 @@+from typing import Finalfrom unittest.mock import AsyncMock, Mockimport httpx@@ -84,6 +85,19 @@ def test_predibase_get_complete_url_stream_and_non_stream():assert stream_url.endswith("/generate_stream")[email protected](("model", "stream"), [("generate", False), ("generate_stream", True)])+def test_predibase_get_complete_url_appends_the_route_to_a_model_named_like_it(model: str, stream: bool) -> None:+ url: Final = PredibaseConfig().get_complete_url(+ api_base="https://serving.example.com",+ api_key="test-key",+ model=model,+ optional_params={"stream": stream},+ litellm_params={"predibase_tenant_id": "tenant-123"},+ )++ assert url.endswith(f"/llms/{model}/{model}")++def test_predibase_get_complete_url_missing_tenant_id():config = PredibaseConfig()diff --git a/tests/unit/llms/vertex_ai/test_vertex_llm_base.py b/tests/unit/llms/vertex_ai/test_vertex_llm_base.pyindex 17a92b31284..dd2b86b6f81 100644--- a/tests/unit/llms/vertex_ai/test_vertex_llm_base.py+++ b/tests/unit/llms/vertex_ai/test_vertex_llm_base.py@@ -977,6 +977,22 @@ class TestVertexBase:assert result_url == f"{gateway_api_base}:embedContent"+ def test_check_custom_proxy_vertex_api_base_with_path_and_trailing_slash_appends_endpoint_once(self):+ gateway_api_base: Final = "https://gateway.example.com/vertex/v1/projects/p/locations/us-central1/endpoints/123"++ _, result_url = VertexBase()._check_custom_proxy(+ api_base=f"{gateway_api_base}/",+ custom_llm_provider="vertex_ai",+ gemini_api_key=None,+ endpoint="predict",+ stream=None,+ auth_header="Bearer token123",+ url="https://us-central1-aiplatform.googleapis.com/v1/projects/p/locations/us-central1/endpoints/123:predict",+ model="123",+ )++ assert result_url == f"{gateway_api_base}:predict"+def test_check_custom_proxy_vertex_api_base_with_version_path_grafts_default_path(self):vertex_base = VertexBase()