fix(azure_ai): drop the api_base path segments a relay already repeats

This commit is contained in:
mateo-berri 2026-09-09 18:32:13 -07:00
parent cc0c6087e3
commit a25eccafc9
2 changed files with 35 additions and 1 deletions

View file

@ -62,6 +62,22 @@ def foundry_root(api_base: str) -> str:
return str(url.copy_with(path="/" + "/".join(root_segments), query=None)).rstrip("/")
def without_repeated_native_prefix(root: str, native_endpoint: str) -> str:
url: Final = httpx.URL(root)
root_segments: Final = tuple(segment for segment in url.path.split("/") if segment)
native_segments: Final = tuple(segment.casefold() for segment in native_endpoint.split("/") if segment)
overlap: Final = next(
(
length
for length in range(min(len(root_segments), len(native_segments)), 0, -1)
if tuple(segment.casefold() for segment in root_segments[-length:]) == native_segments[:length]
),
0,
)
kept_segments: Final = root_segments[: len(root_segments) - overlap]
return str(url.copy_with(path="/" + "/".join(kept_segments), query=None)).rstrip("/")
def relay_query_params(
request_query_params: Mapping[str, object] | None,
deployment_api_version: str | None,
@ -111,7 +127,7 @@ class AzureAIPassthroughConfig(AzureFoundryModelInfo, BasePassthroughConfig):
raise ValueError("Azure AI api base not found: set `api_base` on the deployment or AZURE_AI_API_BASE")
native_endpoint: Final = strip_leading_model_segment(endpoint, (model, model_group_from(litellm_params)))
root: Final = foundry_root(base_target_url).removesuffix(f"/{native_endpoint.strip('/')}")
root: Final = without_repeated_native_prefix(foundry_root(base_target_url), native_endpoint)
query_params: Final = relay_query_params(
request_query_params, api_version_from(litellm_params), base_target_url
)

View file

@ -135,6 +135,24 @@ def test_full_url_api_base_that_already_ends_with_the_native_path_is_not_doubled
assert base == "https://my-resource.cognitiveservices.azure.com/openai/deployments/model-router"
@pytest.mark.parametrize("relayed_deployment", ["gpt-4o", "GPT-4o"])
def test_deployment_root_api_base_is_not_repeated_when_the_relay_carries_the_deployment_path(relayed_deployment):
url, base = AzureAIPassthroughConfig().get_complete_url(
api_base="https://my-resource.openai.azure.com/openai/deployments/gpt-4o",
api_key="key",
model="gpt-4o",
endpoint=f"aoai-gpt-4o/openai/deployments/{relayed_deployment}/chat/completions",
request_query_params={"api-version": "2024-10-21"},
litellm_params={"litellm_metadata": {"model_group": "aoai-gpt-4o"}},
)
assert str(url) == (
f"https://my-resource.openai.azure.com/openai/deployments/{relayed_deployment}/chat/completions"
"?api-version=2024-10-21"
)
assert base == "https://my-resource.openai.azure.com"
def test_parse_relay_under_a_models_api_base_targets_the_foundry_root():
url, _ = AzureAIPassthroughConfig().get_complete_url(
api_base=f"{FOUNDRY_BASE}/models",