mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-26 22:30:11 +00:00
* test: migrate local_testing SDK and provider tests out of build_and_test * test: cover sagemaker region, hf template and nova paths end to end Region rows now go through litellm.completion on the sagemaker/ handler that the legacy tests exercised, the prompt template test mocks the Hugging Face tokenizer config instead of switching to custom_prompt_dict, streaming tests assert role, finish reason and body, and Nova single turn and streaming run through litellm.completion with a recorded request body * test: tighten migrated integration tests for local OpenAI routes, timeouts and air-gapped boot Split the catch-all providers migration file by behavior. The fake endpoint route tests now hit the owned upstream directly, as the legacy harness tests did. LM Studio asserts the outbound key, path and stripped model. The embedding timeout test asserts the proxy maps a deployment timeout to a 408 and forwards the read timeout to the provider client, instead of timing out the test client The Azure streaming tool-call test also asserts the second identical stream is served from the Redis cache with the same reassembled tool call The Docker network test always skipped in CI because the security job has no litellm/litellm image and no egress to pull one, and it tested a published image rather than the checkout. It now boots the checkout proxy with every outbound HTTP connection refused and asserts readiness, the bundled cost map fallback and that the cost map host is the only egress attempted * test: tighten the migrated e2e rows for local_testing SDK and provider tests Drop the duplicated Anthropic covers marker and the Anthropic cell wrongly claimed by the Bedrock and Vertex cache tests. Keep the file-wide Anthropic model as it is on main. Send the 2023-06-01 version header to Anthropic through the deployment's extra_headers so the header actually reaches the provider, and mark the creation-token case 1h then 5m like the legacy test did Reuse the Bedrock file's own registration for the pydantic response test, swap the hand-built PNGs for the shared cat.jpg fixture, assert the SageMaker Nova image answer names the cat and that top_logprobs=3 is honored, and register the SageMaker Nova opt-in markers the same way as the other opt-in markers * test: restore lost assertions in migrated test_main tests Restore the role alternation check in test_completion_anthropic_hanging, the embedding and streaming response-header checks, the logit_bias drop check and the gemini system and safety settings checks. Add an azure 429 retry-after test for test_exception_with_headers, trigger the router fallback with mock_timeout like the legacy test, and stop the mock timeout tests sleeping 3s * test: restore lost legacy assertions in migrated vertex ai unit tests * test: tighten migrated cost, streaming and embedding tests and scope the hosted mock guard * test: strengthen migrated anthropic, azure, bedrock and cohere provider tests Assert full request bodies and usage instead of presence checks, restore the dummy-tool and custom_llm_provider assertions the legacy tests made, add the async and per-model variants the legacy parametrization covered, split streamed tool arguments across deltas, and move the Azure chat streaming tests to the azure.py mirror file * test: tighten migrated llms provider tests and cover legacy intents lost in the move Strengthen the migrated fireworks, gemini, mistral, together, openai text completion, openrouter, perplexity and openai error tests so they assert what the legacy tests proved (cost, provider tag, usage-only-on-last-chunk, function_call rebuild, reasoning param, sync/async/stream error fields). Replace the huggingface and jina tests that doubled litellm code with respx end-to-end tests. Add the tests the pre-existing mapped tests never covered: cohere 400 mapping, token-array text completion, hosted_vllm/llamafile/lm_studio env api base embeddings, watsonx embedding auth, ollama ssl_verify client and ollama vision prompt * test: cover residual legacy paths for anthropic tool streams, watsonx async stream, ollama http images and caller async embedding clients * test: restore single request assertions in the deep_infra and azure_ai embedding tests * test: drop a no-op streaming cost flag from the openai streaming cost test * test: cover residual legacy-test lines in caching, main, utils, replicate, vertex and bedrock invoke * test: cover streamed audio assembly, choice membership, untranslated responses events and the stream log guard * test: pin cost rates in unit tests, use sdk for cohere e2e, drop vertex llama e2e * test: send the vertex maps e2e request through the openai sdk * test: drop the local_testing jobs that select no tests after the migrations * test: wait for the streamed cache entry and keep custom Mistral rates test-local * test: keep untouched Mistral test lines as they were --------- Co-authored-by: yuneng <yuneng@berri.ai>
795 lines
27 KiB
Python
795 lines
27 KiB
Python
"""Tests for litellm/llms/azure/azure.py AzureChatCompletion handler behaviour."""
|
|
|
|
import asyncio
|
|
import json
|
|
import os
|
|
import time
|
|
from collections.abc import Mapping
|
|
from typing import Final
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
from httpx import Client, Headers
|
|
from openai import AsyncAzureOpenAI, AzureOpenAI
|
|
|
|
import litellm
|
|
from litellm import completion
|
|
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
|
|
from litellm.llms.azure.azure import AzureChatCompletion
|
|
from litellm.llms.azure.common_utils import process_azure_headers
|
|
|
|
|
|
class _FakeRawResponse:
|
|
headers: Final = {"x-ms-is-spilled-over": "true"}
|
|
|
|
def parse(self):
|
|
return iter(())
|
|
|
|
|
|
class _FakeRawCompletions:
|
|
def create(self, **kwargs):
|
|
return _FakeRawResponse()
|
|
|
|
|
|
def test_sync_streaming_stamps_response_headers_on_the_logging_obj() -> None:
|
|
"""Sync streaming must mirror async_streaming and record the provider response
|
|
headers on model_call_details, or downstream consumers (spillover-aware cost
|
|
calculation) cannot see them."""
|
|
client = AzureOpenAI(api_key="fake", api_version="2024-02-01", azure_endpoint="https://fake.openai.azure.com")
|
|
client.chat.completions.with_raw_response = _FakeRawCompletions()
|
|
|
|
logging_obj = LiteLLMLoggingObj(
|
|
model="azure/gpt-4o-spill-test",
|
|
messages=[{"role": "user", "content": "Hi"}],
|
|
stream=True,
|
|
call_type="completion",
|
|
start_time=time.time(),
|
|
litellm_call_id="spill-sync-1",
|
|
function_id="f",
|
|
)
|
|
|
|
AzureChatCompletion().streaming(
|
|
logging_obj=logging_obj,
|
|
api_base="https://fake.openai.azure.com",
|
|
api_key="fake",
|
|
api_version="2024-02-01",
|
|
dynamic_params=False,
|
|
data={"messages": [{"role": "user", "content": "Hi"}], "stream": True},
|
|
model="gpt-4o-spill-test",
|
|
timeout=30.0,
|
|
max_retries=0,
|
|
client=client,
|
|
)
|
|
|
|
assert logging_obj.model_call_details["response_headers"] == {"x-ms-is-spilled-over": "true"}
|
|
|
|
|
|
class _CancelledRawCompletions:
|
|
async def create(self, **kwargs):
|
|
raise asyncio.CancelledError()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_propagates_cancelled_error() -> None:
|
|
client = AsyncAzureOpenAI(
|
|
api_key="fake-key",
|
|
api_version="2024-02-01",
|
|
azure_endpoint="https://fake-resource.openai.azure.com",
|
|
)
|
|
client.chat.completions.with_raw_response = _CancelledRawCompletions()
|
|
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await litellm.acompletion(
|
|
model="azure/fake-deployment",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
client=client,
|
|
)
|
|
|
|
|
|
def test_process_azure_headers_empty():
|
|
result = process_azure_headers({})
|
|
assert result == {}, "Expected empty dictionary for no input"
|
|
|
|
|
|
def test_process_azure_headers_with_all_headers():
|
|
input_headers = Headers(
|
|
{
|
|
"x-ratelimit-limit-requests": "100",
|
|
"x-ratelimit-remaining-requests": "90",
|
|
"x-ratelimit-limit-tokens": "10000",
|
|
"x-ratelimit-remaining-tokens": "9000",
|
|
"other-header": "value",
|
|
}
|
|
)
|
|
|
|
expected_output = {
|
|
"x-ratelimit-limit-requests": "100",
|
|
"x-ratelimit-remaining-requests": "90",
|
|
"x-ratelimit-limit-tokens": "10000",
|
|
"x-ratelimit-remaining-tokens": "9000",
|
|
"llm_provider-x-ratelimit-limit-requests": "100",
|
|
"llm_provider-x-ratelimit-remaining-requests": "90",
|
|
"llm_provider-x-ratelimit-limit-tokens": "10000",
|
|
"llm_provider-x-ratelimit-remaining-tokens": "9000",
|
|
"llm_provider-other-header": "value",
|
|
}
|
|
|
|
result = process_azure_headers(input_headers)
|
|
assert result == expected_output, "Unexpected output for all Azure headers"
|
|
|
|
|
|
def test_process_azure_headers_with_partial_headers():
|
|
input_headers = Headers(
|
|
{
|
|
"x-ratelimit-limit-requests": "100",
|
|
"x-ratelimit-remaining-tokens": "9000",
|
|
"other-header": "value",
|
|
}
|
|
)
|
|
|
|
expected_output = {
|
|
"x-ratelimit-limit-requests": "100",
|
|
"x-ratelimit-remaining-tokens": "9000",
|
|
"llm_provider-x-ratelimit-limit-requests": "100",
|
|
"llm_provider-x-ratelimit-remaining-tokens": "9000",
|
|
"llm_provider-other-header": "value",
|
|
}
|
|
|
|
result = process_azure_headers(input_headers)
|
|
assert result == expected_output, "Unexpected output for partial Azure headers"
|
|
|
|
|
|
def test_process_azure_headers_with_no_matching_headers():
|
|
input_headers = Headers({"unrelated-header-1": "value1", "unrelated-header-2": "value2"})
|
|
|
|
expected_output = {
|
|
"llm_provider-unrelated-header-1": "value1",
|
|
"llm_provider-unrelated-header-2": "value2",
|
|
}
|
|
|
|
result = process_azure_headers(input_headers)
|
|
assert result == expected_output, "Unexpected output for non-matching headers"
|
|
|
|
|
|
def test_process_azure_headers_with_dict_input():
|
|
input_headers = {
|
|
"x-ratelimit-limit-requests": "100",
|
|
"x-ratelimit-remaining-requests": "90",
|
|
"other-header": "value",
|
|
}
|
|
|
|
expected_output = {
|
|
"x-ratelimit-limit-requests": "100",
|
|
"x-ratelimit-remaining-requests": "90",
|
|
"llm_provider-x-ratelimit-limit-requests": "100",
|
|
"llm_provider-x-ratelimit-remaining-requests": "90",
|
|
"llm_provider-other-header": "value",
|
|
}
|
|
|
|
result = process_azure_headers(input_headers)
|
|
assert result == expected_output, "Unexpected output for dict input"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"input, call_type",
|
|
[
|
|
({"messages": [{"role": "user", "content": "Hello world"}]}, "completion"),
|
|
({"input": "Hello world"}, "embedding"),
|
|
({"prompt": "Hello world"}, "image_generation"),
|
|
],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
"header_value",
|
|
[
|
|
"headers",
|
|
"extra_headers",
|
|
],
|
|
)
|
|
def test_azure_extra_headers(input, call_type, header_value):
|
|
from litellm import embedding, image_generation
|
|
|
|
# Clear the LLM clients cache to ensure the new http_client is used
|
|
litellm.in_memory_llm_clients_cache.flush_cache()
|
|
|
|
http_client = Client()
|
|
|
|
messages = [{"role": "user", "content": "Hello world"}]
|
|
with patch.object(http_client, "send", new=MagicMock()) as mock_client:
|
|
litellm.client_session = http_client
|
|
try:
|
|
if call_type == "completion":
|
|
func = completion
|
|
elif call_type == "embedding":
|
|
func = embedding
|
|
elif call_type == "image_generation":
|
|
func = image_generation
|
|
|
|
data = {
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_base": "https://openai-gpt-4-test-v-1.openai.azure.com",
|
|
"api_version": "2023-07-01-preview",
|
|
"api_key": "my-azure-api-key",
|
|
header_value: {
|
|
"Authorization": "my-bad-key",
|
|
"Ocp-Apim-Subscription-Key": "hello-world-testing",
|
|
},
|
|
**input,
|
|
}
|
|
response = func(**data)
|
|
print(response)
|
|
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
mock_client.assert_called()
|
|
|
|
print(f"mock_client.call_args: {mock_client.call_args}")
|
|
request = mock_client.call_args[0][0]
|
|
print(request.method) # This will print 'POST'
|
|
print(request.url) # This will print the full URL
|
|
print(request.headers) # This will print the full URL
|
|
auth_header = request.headers.get("Authorization")
|
|
apim_key = request.headers.get("Ocp-Apim-Subscription-Key")
|
|
print(auth_header)
|
|
assert auth_header == "my-bad-key"
|
|
assert apim_key == "hello-world-testing"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"api_base, model, expected_endpoint",
|
|
[
|
|
(
|
|
"https://fake-azure-endpoint.invalid",
|
|
"dall-e-3-test",
|
|
"https://fake-azure-endpoint.invalid/openai/deployments/dall-e-3-test/images/generations?api-version=2023-12-01-preview",
|
|
),
|
|
(
|
|
"https://fake-azure-endpoint.invalid/openai/deployments/my-custom-deployment",
|
|
"dall-e-3",
|
|
"https://fake-azure-endpoint.invalid/openai/deployments/my-custom-deployment/images/generations?api-version=2023-12-01-preview",
|
|
),
|
|
],
|
|
)
|
|
def test_process_azure_endpoint_url(api_base, model, expected_endpoint):
|
|
from litellm.llms.azure.azure import AzureChatCompletion
|
|
|
|
azure_chat_completion = AzureChatCompletion()
|
|
input_args = {
|
|
"azure_client_params": {
|
|
"api_version": "2023-12-01-preview",
|
|
"azure_endpoint": api_base,
|
|
"azure_deployment": model,
|
|
"max_retries": 2,
|
|
"timeout": 600,
|
|
"api_key": "sk-test-mock-key-505",
|
|
},
|
|
"model": model,
|
|
}
|
|
result = azure_chat_completion.create_azure_base_url(**input_args)
|
|
assert result == expected_endpoint, "Unexpected endpoint"
|
|
|
|
|
|
@patch("azure.identity.UsernamePasswordCredential")
|
|
@patch("azure.identity.get_bearer_token_provider")
|
|
def test_get_azure_ad_token_from_username_password(mock_get_bearer_token_provider, mock_credential):
|
|
from litellm.llms.azure.common_utils import (
|
|
get_azure_ad_token_from_username_password,
|
|
)
|
|
|
|
client_id = "test-client-id"
|
|
username = "test-username"
|
|
password = "test-password"
|
|
|
|
mock_token_provider = lambda: "mock-token"
|
|
mock_get_bearer_token_provider.return_value = mock_token_provider
|
|
|
|
result = get_azure_ad_token_from_username_password(
|
|
client_id=client_id, azure_username=username, azure_password=password
|
|
)
|
|
|
|
mock_credential.assert_called_once_with(client_id=client_id, username=username, password=password)
|
|
|
|
mock_get_bearer_token_provider.assert_called_once_with(
|
|
mock_credential.return_value, "https://cognitiveservices.azure.com/.default"
|
|
)
|
|
|
|
assert result == mock_token_provider
|
|
|
|
|
|
def test_azure_openai_gpt_4o_naming(monkeypatch):
|
|
from pydantic import BaseModel, Field
|
|
|
|
monkeypatch.setenv("AZURE_API_VERSION", "2024-10-21")
|
|
|
|
client = AzureOpenAI(
|
|
api_key="test-api-key",
|
|
base_url="https://fake-azure-endpoint.invalid",
|
|
api_version="2023-12-01-preview",
|
|
)
|
|
|
|
class ResponseFormat(BaseModel):
|
|
number: str = Field(description="total number of days in a week")
|
|
days: list[str] = Field(description="name of days in a week")
|
|
|
|
with patch.object(client.chat.completions.with_raw_response, "create") as mock_post:
|
|
try:
|
|
completion(
|
|
model="azure/gpt4o",
|
|
messages=[{"role": "user", "content": "Hello world"}],
|
|
response_format=ResponseFormat,
|
|
client=client,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
mock_post.assert_called_once()
|
|
|
|
print(mock_post.call_args.kwargs)
|
|
|
|
assert "tool_calls" not in mock_post.call_args.kwargs
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"api_version",
|
|
[
|
|
"2024-10-21",
|
|
# "2024-02-15-preview",
|
|
],
|
|
)
|
|
def test_azure_gpt_4o_with_tool_call_and_response_format(api_version):
|
|
from litellm import completion
|
|
from typing import Optional
|
|
from pydantic import BaseModel
|
|
import litellm
|
|
|
|
|
|
client = AzureOpenAI(
|
|
api_key="fake-key",
|
|
base_url="https://fake-azure.openai.azure.com",
|
|
api_version=api_version,
|
|
)
|
|
|
|
class InvestigationOutput(BaseModel):
|
|
alert_explanation: Optional[str] = None
|
|
investigation: Optional[str] = None
|
|
conclusions_and_possible_root_causes: Optional[str] = None
|
|
next_steps: Optional[str] = None
|
|
related_logs: Optional[str] = None
|
|
app_or_infra: Optional[str] = None
|
|
external_links: Optional[str] = None
|
|
|
|
tools = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_current_time",
|
|
"description": "Returns the current date and time",
|
|
"strict": True,
|
|
"parameters": {
|
|
"properties": {
|
|
"timezone": {
|
|
"type": "string",
|
|
"description": "The timezone to get the current time for (e.g., 'UTC', 'America/New_York')",
|
|
}
|
|
},
|
|
"required": ["timezone"],
|
|
"type": "object",
|
|
"additionalProperties": False,
|
|
},
|
|
},
|
|
}
|
|
]
|
|
|
|
with patch.object(client.chat.completions.with_raw_response, "create") as mock_post:
|
|
mock_post.return_value.headers = {}
|
|
mock_post.return_value.parse.return_value = litellm.ModelResponse(
|
|
choices=[{"message": {"role": "assistant", "content": InvestigationOutput().model_dump_json()}}]
|
|
)
|
|
response = litellm.completion(
|
|
model="azure/gpt-4.1-mini",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": "You are a tool-calling AI assist provided with common devops and IT tools that you can use to troubleshoot problems or answer questions.\nWhenever possible you MUST first use tools to investigate then answer the question.",
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": "What is the current date and time in NYC?",
|
|
},
|
|
],
|
|
drop_params=True,
|
|
temperature=0.00000001,
|
|
tools=tools,
|
|
tool_choice="auto",
|
|
response_format=InvestigationOutput, # commenting this line will cause the output to be correct
|
|
api_version=api_version,
|
|
client=client,
|
|
)
|
|
|
|
mock_post.assert_called_once()
|
|
|
|
if api_version == "2024-10-21":
|
|
assert "response_format" in mock_post.call_args.kwargs
|
|
else:
|
|
assert "response_format" not in mock_post.call_args.kwargs
|
|
assert response.choices[0].message.content == InvestigationOutput().model_dump_json()
|
|
|
|
|
|
def test_map_openai_params():
|
|
"""
|
|
Ensure response_format does not override tools
|
|
"""
|
|
from litellm.llms.azure.chat.gpt_transformation import AzureOpenAIConfig
|
|
|
|
azure_openai_config = AzureOpenAIConfig()
|
|
tools = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_current_time",
|
|
"description": "Returns the current date and time",
|
|
"strict": True,
|
|
"parameters": {
|
|
"properties": {
|
|
"timezone": {
|
|
"type": "string",
|
|
"description": "The timezone to get the current time for (e.g., 'UTC', 'America/New_York')",
|
|
}
|
|
},
|
|
"required": ["timezone"],
|
|
"type": "object",
|
|
"additionalProperties": False,
|
|
},
|
|
},
|
|
}
|
|
]
|
|
received_args = {
|
|
"non_default_params": {
|
|
"temperature": 1e-08,
|
|
"response_format": {
|
|
"type": "json_schema",
|
|
"json_schema": {
|
|
"schema": {
|
|
"properties": {
|
|
"alert_explanation": {
|
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
"title": "Alert Explanation",
|
|
},
|
|
"investigation": {
|
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
"title": "Investigation",
|
|
},
|
|
"conclusions_and_possible_root_causes": {
|
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
"title": "Conclusions And Possible Root Causes",
|
|
},
|
|
"next_steps": {
|
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
"title": "Next Steps",
|
|
},
|
|
"related_logs": {
|
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
"title": "Related Logs",
|
|
},
|
|
"app_or_infra": {
|
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
"title": "App Or Infra",
|
|
},
|
|
"external_links": {
|
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
|
"title": "External Links",
|
|
},
|
|
},
|
|
"title": "InvestigationOutput",
|
|
"type": "object",
|
|
"additionalProperties": False,
|
|
"required": [
|
|
"alert_explanation",
|
|
"investigation",
|
|
"conclusions_and_possible_root_causes",
|
|
"next_steps",
|
|
"related_logs",
|
|
"app_or_infra",
|
|
"external_links",
|
|
],
|
|
},
|
|
"name": "InvestigationOutput",
|
|
"strict": True,
|
|
},
|
|
},
|
|
"tools": tools,
|
|
"tool_choice": "auto",
|
|
},
|
|
"optional_params": {},
|
|
"model": "gpt-4o",
|
|
"drop_params": True,
|
|
"api_version": "2024-02-15-preview",
|
|
}
|
|
optional_params = azure_openai_config.map_openai_params(**received_args)
|
|
assert "tools" in optional_params
|
|
assert len(optional_params["tools"]) > 1
|
|
|
|
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
@pytest.mark.parametrize("max_retries", [0, 4])
|
|
@pytest.mark.parametrize("stream", [True, False])
|
|
@patch(
|
|
"litellm.main.azure_chat_completions.make_sync_azure_openai_chat_completion_request"
|
|
)
|
|
def test_azure_max_retries_0(
|
|
mock_make_sync_azure_openai_chat_completion_request, max_retries, stream
|
|
):
|
|
import litellm
|
|
from litellm import completion
|
|
|
|
# Clear the LLM clients cache to ensure max_retries is set correctly
|
|
litellm.in_memory_llm_clients_cache.flush_cache()
|
|
|
|
try:
|
|
completion(
|
|
model="azure/gpt-4.1-mini",
|
|
messages=[{"role": "user", "content": "Hello world"}],
|
|
max_retries=max_retries,
|
|
stream=stream,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
mock_make_sync_azure_openai_chat_completion_request.assert_called_once()
|
|
assert (
|
|
mock_make_sync_azure_openai_chat_completion_request.call_args.kwargs[
|
|
"azure_client"
|
|
].max_retries
|
|
== max_retries
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
@pytest.mark.parametrize("max_retries", [0, 4])
|
|
@pytest.mark.parametrize("stream", [True, False])
|
|
@patch("litellm.main.azure_chat_completions.make_azure_openai_chat_completion_request")
|
|
@pytest.mark.asyncio
|
|
async def test_async_azure_max_retries_0(
|
|
make_azure_openai_chat_completion_request, max_retries, stream
|
|
):
|
|
import litellm
|
|
from litellm import acompletion
|
|
|
|
# Clear the LLM clients cache to ensure max_retries is set correctly
|
|
litellm.in_memory_llm_clients_cache.flush_cache()
|
|
|
|
try:
|
|
await acompletion(
|
|
model="azure/gpt-4.1-mini",
|
|
messages=[{"role": "user", "content": "Hello world"}],
|
|
max_retries=max_retries,
|
|
stream=stream,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
make_azure_openai_chat_completion_request.assert_called_once()
|
|
assert (
|
|
make_azure_openai_chat_completion_request.call_args.kwargs[
|
|
"azure_client"
|
|
].max_retries
|
|
== max_retries
|
|
)
|
|
|
|
|
|
def test_azure_openai_responses_bridge():
|
|
from litellm import completion
|
|
import litellm
|
|
|
|
litellm.turn_on_debug()
|
|
|
|
with patch.object(litellm, "responses") as mock_responses:
|
|
try:
|
|
response = completion(
|
|
model="azure/responses/test-azure-computer-use-preview",
|
|
messages=[{"role": "user", "content": "Hello world"}],
|
|
api_base=os.getenv("AZURE_COMPUTER_USE_API_BASE"),
|
|
api_version="2025-04-01-preview",
|
|
api_key=os.getenv("AZURE_COMPUTER_USE_API_KEY"),
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
mock_responses.assert_called_once()
|
|
assert (
|
|
mock_responses.call_args.kwargs["model"]
|
|
== "azure/test-azure-computer-use-preview"
|
|
)
|
|
assert mock_responses.call_args.kwargs["custom_llm_provider"] == "azure"
|
|
|
|
|
|
def test_azure_with_content_safety_error():
|
|
"""
|
|
Verify user can access innererror from the Azure OpenAI exception
|
|
"""
|
|
from litellm import completion
|
|
from litellm.exceptions import ContentPolicyViolationError
|
|
from litellm.litellm_core_utils.exception_mapping_utils import exception_type
|
|
from unittest.mock import MagicMock
|
|
|
|
mock_exception = Exception(
|
|
"The response was filtered due to the prompt triggering Azure OpenAI's content management policy"
|
|
)
|
|
mock_exception.body = {
|
|
"innererror": {
|
|
"code": "ResponsibleAIPolicyViolation",
|
|
"content_filter_result": {
|
|
"hate": {"filtered": False, "severity": "safe"},
|
|
"jailbreak": {"filtered": False, "detected": False},
|
|
"self_harm": {"filtered": False, "severity": "safe"},
|
|
"sexual": {"filtered": False, "severity": "safe"},
|
|
"violence": {"filtered": True, "severity": "high"},
|
|
},
|
|
}
|
|
}
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 400
|
|
mock_exception.response = mock_response
|
|
|
|
with pytest.raises(ContentPolicyViolationError) as exc_info:
|
|
exception_type(
|
|
model="azure/gpt-4o-new-test",
|
|
original_exception=mock_exception,
|
|
custom_llm_provider="azure",
|
|
)
|
|
|
|
e = exc_info.value
|
|
print("got exception=", e)
|
|
assert e.provider_specific_fields is not None
|
|
print("got provider_specific_fields=", e.provider_specific_fields)
|
|
assert e.provider_specific_fields.get("innererror") is not None
|
|
assert (
|
|
e.provider_specific_fields["innererror"]["code"]
|
|
== "ResponsibleAIPolicyViolation"
|
|
)
|
|
assert (
|
|
e.provider_specific_fields["innererror"]["content_filter_result"]["violence"][
|
|
"filtered"
|
|
]
|
|
is True
|
|
)
|
|
assert (
|
|
e.provider_specific_fields["innererror"]["content_filter_result"]["violence"][
|
|
"severity"
|
|
]
|
|
== "high"
|
|
)
|
|
|
|
|
|
def _azure_sse_body(chunks: tuple[Mapping[str, object], ...]) -> str:
|
|
return "".join(f"data: {json.dumps(chunk)}\n\n" for chunk in chunks) + "data: [DONE]\n\n"
|
|
|
|
|
|
@respx.mock
|
|
def test_azure_chat_stream_preserves_text_and_special_characters():
|
|
response_chunks: Final = (
|
|
{
|
|
"id": "chatcmpl-azure",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1,
|
|
"model": "gpt-4.1-mini",
|
|
"choices": [{"index": 0, "delta": {"content": "<xml>café ☃</xml>"}, "finish_reason": None}],
|
|
},
|
|
{
|
|
"id": "chatcmpl-azure",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1,
|
|
"model": "gpt-4.1-mini",
|
|
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
|
|
},
|
|
)
|
|
route: Final = respx.post(
|
|
url__regex=r"https://azure-stream\.openai\.azure\.com/.*/chat/completions.*"
|
|
).mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
text=_azure_sse_body(response_chunks),
|
|
headers={"content-type": "text/event-stream"},
|
|
)
|
|
)
|
|
|
|
stream: Final = litellm.completion(
|
|
model="azure/gpt-4.1-mini",
|
|
messages=[{"role": "user", "content": "Respond with the <xml> tag only"}],
|
|
api_base="https://azure-stream.openai.azure.com",
|
|
api_version="2024-02-15-preview",
|
|
api_key="test-azure-key",
|
|
max_retries=0,
|
|
stream=True,
|
|
)
|
|
chunks: Final = list(stream)
|
|
|
|
assert route.call_count == 1
|
|
assert [chunk._hidden_params["custom_llm_provider"] for chunk in chunks] == ["azure"] * len(chunks)
|
|
assert "".join(chunk.choices[0].delta.content or "" for chunk in chunks) == "<xml>café ☃</xml>"
|
|
assert [chunk.choices[0].finish_reason for chunk in chunks if chunk.choices[0].finish_reason] == ["stop"]
|
|
|
|
|
|
@respx.mock
|
|
def test_azure_chat_stream_reassembles_tool_call_arguments():
|
|
response_chunks: Final = (
|
|
{
|
|
"id": "chatcmpl-azure-tools",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1,
|
|
"model": "gpt-4.1-mini",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"delta": {
|
|
"tool_calls": [
|
|
{
|
|
"index": 0,
|
|
"id": "call-weather",
|
|
"type": "function",
|
|
"function": {"name": "get_current_weather", "arguments": '{"city":'},
|
|
}
|
|
]
|
|
},
|
|
"finish_reason": None,
|
|
}
|
|
],
|
|
},
|
|
{
|
|
"id": "chatcmpl-azure-tools",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1,
|
|
"model": "gpt-4.1-mini",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"delta": {"tool_calls": [{"index": 0, "function": {"arguments": '"Boston"}'}}]},
|
|
"finish_reason": "tool_calls",
|
|
}
|
|
],
|
|
},
|
|
)
|
|
route: Final = respx.post(
|
|
url__regex=r"https://azure-tool-stream\.openai\.azure\.com/.*/chat/completions.*"
|
|
).mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
text=_azure_sse_body(response_chunks),
|
|
headers={"content-type": "text/event-stream"},
|
|
)
|
|
)
|
|
messages: Final = [{"role": "user", "content": "What is the weather in Boston?"}]
|
|
stream: Final = litellm.completion(
|
|
model="azure/gpt-4.1-mini",
|
|
messages=messages,
|
|
tools=[
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_current_weather",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {"city": {"type": "string"}},
|
|
"required": ["city"],
|
|
},
|
|
},
|
|
}
|
|
],
|
|
tool_choice="auto",
|
|
api_base="https://azure-tool-stream.openai.azure.com",
|
|
api_version="2024-02-15-preview",
|
|
api_key="test-azure-key",
|
|
max_retries=0,
|
|
stream=True,
|
|
)
|
|
chunks: Final = list(stream)
|
|
response: Final = litellm.stream_chunk_builder(chunks, messages=messages)
|
|
tool_call: Final = response.choices[0].message.tool_calls[0]
|
|
|
|
assert route.call_count == 1
|
|
assert tool_call.function.name == "get_current_weather"
|
|
assert json.loads(tool_call.function.arguments) == {"city": "Boston"}
|
|
assert response.choices[0].finish_reason == "tool_calls"
|