litellm/tests/unit/llms/azure/test_azure_embedding.py
devin-ai-integration[bot] e0398455d0
test: migrate tests/llm_translation into unit, integration and e2e tiers (#45759)
* test: migrate selected llm translation contracts

* test: migrate anthropic completion translation tests

* test: migrate bedrock and cohere translation tests

* test: tighten bedrock translation migration

* test: migrate azure and openai model translation tests

* test: migrate openai and small-provider translation tests

* test: migrate gemini and vertex translation tests

* test: migrate optional params and rerank translation tests

* test: migrate huggingface, json and openrouter translation tests

* test: fix openrouter integration expectations

* test: migrate databricks, xai, cometapi and fireworks translation tests

* test: migrate ovhcloud, prompt and small provider translation tests

* test: split reasoning effort grid into unit and e2e

* test: migrate router and realtime guardrail translation tests

* test: migrate google interactions translation tests

* test: migrate a2a, evals, skills and hosted vllm translation tests

* test: remove remaining llm translation suite

* test: tighten migrated llm translation unit tests and land missing rows

Restores rows the first pass dropped or weakened (bedrock agentcore and openwebui thinking stream, anthropic prefix, citations and tool_choice, azure agents handler, huggingface bare api_base, optional params num_retries, alternating roles, rerank response type), replaces truthiness and call-count checks with whole-value assertions, and moves tests to the mirror file of the code they cover

* test: move cohere, databricks, fireworks and gemini rows to mirror files over mocked HTTP

Restores three TestCohereV2Transform methods the first pass nested out of collection, replaces direct transform calls with respx-backed completion calls, and drops the databricks and fireworks files that sat outside their mirror paths

* test: close lost-intent gaps found in review of the llm translation migration

Points the hosted-mock guard at the triton test's new integration path, restores exact rerank result assertions for infinity, drops the reasoning e2e fallback that passed for any multi-line answer, and adds the mixtral invoke system prompt variants, eval deletion, and async pdf url download that the legacy suite exercised

* test: assert provider acceptance in the reasoning effort e2e

The live half of the split grid proves each route accepts reasoning_effort. claude-fable-5-1 on the direct route answers without surfacing reasoning content, so a reasoning-presence check either fails on head or needs a fallback that passes for any answer. Wire shape stays covered by the unit grid

* test: cover async and stream-end paths the legacy translation suite exercised

Adds async variants for bedrock converse file and thinking content, bedrock embedding timeout and region resolution, interaction stream termination and success logging, xai reasoning token folding, and bytes chunks with a prefix before data:. Also scopes the vertex safety settings global in test_vertex_safety_settings with monkeypatch so it stops leaking into gemini request bodies on the same worker

* test: type the new llm translation e2e calls and mark moved fixture dirs as packages

* test: name the reasoning effort e2e models as module constants

* test: assert reasoning content, register the new gemini coverage ids and give the guardrail config e2e a unique name

* test: block the prompt the shared guardrail denies and drop the reasoning presence check

* test: drop the duplicate litellm import left by the main sync

---------

Co-authored-by: yuneng <yuneng@berri.ai>
2026-10-11 07:58:53 +00:00

207 lines
7.1 KiB
Python

import json
import os
import sys
from typing import Final
from unittest.mock import MagicMock, patch
import httpx
import pytest
import respx
from httpx import Response
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
import litellm
from litellm.llms.azure.azure import AzureChatCompletion
from litellm.types.utils import EmbeddingResponse, Usage
def _make_embedding_response() -> EmbeddingResponse:
return EmbeddingResponse(
model="text-embedding-3-large",
usage=Usage(prompt_tokens=3, completion_tokens=0, total_tokens=3),
data=[{"embedding": [0.1, 0.2, 0.3], "index": 0, "object": "embedding"}],
)
def _make_logging_obj() -> MagicMock:
return MagicMock()
class TestAzureV1AsyncEmbedding:
def test_aembedding_receives_api_version(self):
"""Regression: api_version must be forwarded to aembedding() when aembedding=True.
Without the fix, it was silently dropped, causing AsyncAzureOpenAI to be used
instead of AsyncOpenAI for Azure AI Foundry (v1) endpoints. Fixes #24848."""
handler = AzureChatCompletion()
with patch.object(handler, "aembedding") as mock_aembedding:
handler.embedding(
model="text-embedding-3-large",
input=["hello world"],
api_base="https://my-endpoint.openai.azure.com",
api_version="v1",
timeout=60.0,
logging_obj=_make_logging_obj(),
model_response=_make_embedding_response(),
optional_params={},
api_key="fake-key",
aembedding=True,
litellm_params={},
)
mock_aembedding.assert_called_once()
_, kwargs = mock_aembedding.call_args
assert kwargs.get("api_version") == "v1"
def test_get_azure_openai_client_returns_async_openai_for_v1(self):
from openai import AsyncAzureOpenAI, AsyncOpenAI
handler = AzureChatCompletion()
client = handler.get_azure_openai_client(
api_key="fake-key",
api_base="https://my-endpoint.openai.azure.com",
api_version="v1",
_is_async=True,
litellm_params={},
)
assert isinstance(client, AsyncOpenAI)
assert not isinstance(client, AsyncAzureOpenAI)
def test_get_azure_openai_client_uses_v1_base_url(self):
handler = AzureChatCompletion()
client = handler.get_azure_openai_client(
api_key="fake-key",
api_base="https://my-endpoint.openai.azure.com",
api_version="v1",
_is_async=True,
litellm_params={},
)
assert client is not None
assert "/openai/v1/" in str(client.base_url)
@pytest.mark.parametrize("api_version", ["v1", "latest", "preview"])
def test_all_v1_variants_use_openai_client(self, api_version: str):
from openai import AsyncOpenAI
handler = AzureChatCompletion()
client = handler.get_azure_openai_client(
api_key="fake-key",
api_base="https://my-endpoint.openai.azure.com",
api_version=api_version,
_is_async=True,
litellm_params={},
)
assert isinstance(client, AsyncOpenAI)
@respx.mock
def test_azure_embedding_forwards_optional_args_in_the_request_body():
route = respx.post(url__regex=r"https://azure-optional-args\.openai\.azure\.com/.*/embeddings.*").mock(
return_value=httpx.Response(
200,
json={
"object": "list",
"data": [{"object": "embedding", "index": 0, "embedding": [0.1, 0.2, 0.3]}],
"model": "text-embedding-ada-002",
"usage": {"prompt_tokens": 1, "total_tokens": 1},
},
)
)
response = litellm.embedding(
model="azure/text-embedding-ada-002",
input=["hello"],
api_base="https://azure-optional-args.openai.azure.com",
api_version="2023-05-15",
azure_ad_token="test-token",
encoding_format="float",
)
request = route.calls.last.request
assert json.loads(request.read()) == {
"model": "text-embedding-ada-002",
"input": ["hello"],
"encoding_format": "float",
"azure_ad_token": "test-token",
}
assert request.headers["authorization"] == "Bearer test-token"
assert response.data[0]["embedding"] == [0.1, 0.2, 0.3]
@pytest.mark.asyncio
async def test_aembedding_azure_returns_an_azure_embedding_response(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
route = respx_mock.post(
url__regex=r"https://azure-async-embedding\.openai\.azure\.com/.*/embeddings.*"
).mock(
return_value=httpx.Response(
200,
json={
"object": "list",
"data": [{"object": "embedding", "index": 0, "embedding": [0.4, 0.5, 0.6]}],
"model": "text-embedding-ada-002",
"usage": {"prompt_tokens": 2, "total_tokens": 2},
},
)
)
response = await litellm.aembedding(
model="azure/text-embedding-ada-002",
input=["good morning", "another item"],
api_base="https://azure-async-embedding.openai.azure.com",
api_version="2023-05-15",
api_key="test-key",
)
assert response._hidden_params["custom_llm_provider"] == "azure"
assert (response.usage.prompt_tokens, response.usage.total_tokens) == (2, 2)
assert response.data[0]["embedding"] == [0.4, 0.5, 0.6]
assert len(route.calls) == 1
def test_azure_embedding_maps_a_scripted_request_timeout(respx_mock: respx.MockRouter) -> None:
route = respx_mock.post(
url__regex=r"https://azure-timeout\.openai\.azure\.com/.*/embeddings.*"
).mock(side_effect=httpx.ReadTimeout("scripted timeout"))
with pytest.raises(litellm.Timeout):
litellm.embedding(
model="azure/text-embedding-ada-002",
input=["hello"],
api_base="https://azure-timeout.openai.azure.com",
api_version="2023-05-15",
api_key="test-key",
timeout=0.5,
max_retries=0,
)
assert route.call_count == 1
@pytest.mark.respx(assert_all_called=True)
def test_azure_embedding_max_retries_zero_sends_one_request(respx_mock: respx.MockRouter) -> None:
url: Final = "https://example-resource.openai.azure.com/openai/deployments/text-embedding-ada-002/embeddings"
route: Final = respx_mock.post(url__startswith=url).mock(
return_value=Response(500, json={"error": {"message": "temporary failure"}})
)
litellm.in_memory_llm_clients_cache.flush_cache()
with pytest.raises(litellm.APIError):
litellm.embedding(
model="azure/text-embedding-ada-002",
input=["hello"],
api_base="https://example-resource.openai.azure.com",
api_key="azure-test-key",
api_version="2024-02-01",
max_retries=0,
)
assert route.call_count == 1