mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-03 01:38:46 +00:00
* test: move the unit half of 126 mixed legacy files into tests/unit * test: restore litellm globals that moved tests set * test: finalize migration test cleanup * test: restore original bodies of moved legacy tests The move into tests/unit had rewritten 612 test bodies, and some of the rewrites dropped assertions. Each moved test now carries its original body from the legacy file, with only the imports, helpers, fake provider credentials and monkeypatched env it needs to run under tests/unit test_timeout_streaming goes back to tests/local_testing because it needs the fake OpenAI endpoint server. The image payload fixture moves with its only user, and two tests that leaked global state (a registered model cost entry and queued logging tasks) are now isolated * test: drop module imports shadowed by restored local imports * test: assert on LiteLLM output in no-assertion moved tests and isolate leaks Twenty no-assertion candidates get one assertion on the value LiteLLM returns, with the original lines unchanged. Four tests go back to their legacy files because they only check types or imports, write into the working directory, or cannot assert without a body change Two moved tests leaked globals into later tests in the same worker, so monkeypatch fixtures now restore the retry-after header parser and the end user cost tracking flags * test: drain queued logging tasks before the Phoenix span test The moved Phoenix test counted spans from logging tasks that earlier tests had queued, so the drain fixture moves to tests/unit/conftest.py and both it and the Datadog batch test use it. test_factory_function goes back to its legacy file because its returned wrapper calls the real Assistants API and cannot be asserted on without a body change --------- Co-authored-by: yuneng <yuneng@berri.ai>
342 lines
12 KiB
Python
342 lines
12 KiB
Python
# What is this?
|
|
## Unit tests for Azure AI integration
|
|
|
|
import asyncio
|
|
import os
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
import litellm.types
|
|
import litellm.types.utils
|
|
from litellm.llms.anthropic.chat import ModelResponseIterator
|
|
import httpx
|
|
|
|
# from base_rerank_unit_tests import BaseLLMRerankTest
|
|
|
|
load_dotenv()
|
|
import io
|
|
|
|
from typing import Optional
|
|
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm import completion
|
|
from litellm.integrations.custom_logger import CustomLogger
|
|
from litellm.types.utils import StandardLoggingPayload
|
|
|
|
AZURE_AI_API_BASE = os.getenv("AZURE_AI_API_BASE")
|
|
|
|
|
|
# skipping due to cohere rbac issues
|
|
# class TestAzureAIRerank(BaseLLMRerankTest):
|
|
# def get_custom_llm_provider(self) -> litellm.LlmProviders:
|
|
# return litellm.LlmProviders.AZURE_AI
|
|
|
|
# def get_base_rerank_call_args(self) -> dict:
|
|
# return {
|
|
# "model": "azure_ai/cohere-rerank-v3-english",
|
|
# "api_base": os.getenv("AZURE_AI_COHERE_API_BASE"),
|
|
# "api_key": os.getenv("AZURE_AI_COHERE_API_KEY"),
|
|
# }
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_azure_ai_request_format():
|
|
"""
|
|
Test that Azure AI requests are formatted correctly with the proper endpoint and parameters
|
|
for both synchronous and asynchronous calls
|
|
"""
|
|
from openai import AsyncAzureOpenAI, AzureOpenAI
|
|
|
|
litellm.turn_on_debug()
|
|
|
|
# Set up the test parameters
|
|
api_key = os.getenv("AZURE_AI_API_KEY")
|
|
api_base = os.getenv("AZURE_AI_API_BASE")
|
|
model = "azure_ai/gpt-4.1-mini"
|
|
messages = [
|
|
{"role": "user", "content": "hi"},
|
|
{"role": "assistant", "content": "Hello! How can I assist you today?"},
|
|
{"role": "user", "content": "hi"},
|
|
]
|
|
|
|
await litellm.acompletion(
|
|
custom_llm_provider="azure_ai",
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
model=model,
|
|
messages=messages,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("model", ["azure/gpt5_series/gpt-5-mini"])
|
|
async def test_azure_gpt5_reasoning(model):
|
|
litellm.turn_on_debug()
|
|
response = await litellm.acompletion(
|
|
model=model,
|
|
messages=[{"role": "user", "content": "What is the capital of France?"}],
|
|
reasoning_effort="minimal",
|
|
max_tokens=10,
|
|
api_base=os.getenv("AZURE_AI_API_BASE"),
|
|
api_key=os.getenv("AZURE_AI_API_KEY"),
|
|
)
|
|
print("response: ", response)
|
|
assert response.choices[0].message.content is not None
|
|
|
|
|
|
def test_completion_azure():
|
|
try:
|
|
from litellm import completion_cost
|
|
|
|
litellm.set_verbose = False
|
|
## Test azure call
|
|
response = completion(
|
|
model="azure_ai/gpt-4.1-mini",
|
|
api_base=os.getenv("AZURE_AI_API_BASE"),
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": "Hello, how are you?",
|
|
}
|
|
],
|
|
api_key=os.getenv("AZURE_AI_API_KEY"),
|
|
)
|
|
print(f"response: {response}")
|
|
print(f"response hidden params: {response._hidden_params}")
|
|
print(response)
|
|
|
|
cost = completion_cost(completion_response=response)
|
|
assert cost > 0.0
|
|
print("Cost for azure completion request", cost)
|
|
except Exception as e:
|
|
pytest.fail(f"Error occurred: {e}")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"api_base",
|
|
[
|
|
AZURE_AI_API_BASE,
|
|
f"{AZURE_AI_API_BASE}/openai/deployments/gpt-4.1-mini/chat/completions?api-version=2023-03-15-preview",
|
|
],
|
|
)
|
|
def test_completion_azure_ai_gpt_4o_with_flexible_api_base(api_base):
|
|
try:
|
|
litellm.set_verbose = True
|
|
|
|
response = completion(
|
|
model="azure_ai/gpt-4.1-mini",
|
|
api_base=api_base,
|
|
api_key=os.getenv("AZURE_AI_API_KEY"),
|
|
messages=[{"role": "user", "content": "What is the meaning of life?"}],
|
|
)
|
|
|
|
print(response)
|
|
except litellm.Timeout as e:
|
|
pass
|
|
except Exception as e:
|
|
pytest.fail(f"Error occurred: {e}")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_azure_ai_model_router():
|
|
"""
|
|
Test Azure AI model router non-streaming response cost tracking.
|
|
Verifies that the flat cost of $0.14 per M input tokens is applied.
|
|
|
|
Tests the pattern: azure_ai/model_router/<deployment-name>
|
|
Where deployment-name is the Azure deployment (e.g., "azure-model-router").
|
|
The model_router prefix is stripped before sending to Azure API.
|
|
"""
|
|
from litellm.llms.azure_ai.cost_calculator import (
|
|
calculate_azure_model_router_flat_cost,
|
|
)
|
|
|
|
litellm.turn_on_debug()
|
|
response = await litellm.acompletion(
|
|
model="azure_ai/model_router/azure-model-router",
|
|
messages=[{"role": "user", "content": "hi who is this"}],
|
|
api_base=os.getenv("AZURE_MODEL_ROUTER_API_BASE"),
|
|
api_key=os.getenv("AZURE_MODEL_ROUTER_API_KEY"),
|
|
)
|
|
print("response: ", response)
|
|
|
|
# Check response cost
|
|
tracked_cost = response._hidden_params["response_cost"]
|
|
assert tracked_cost > 0
|
|
print("Tracked cost: ", tracked_cost)
|
|
|
|
# Verify flat cost is included using the helper function
|
|
usage = response.usage
|
|
if usage and usage.prompt_tokens:
|
|
expected_flat_cost = calculate_azure_model_router_flat_cost(
|
|
model="model_router/azure-model-router", prompt_tokens=usage.prompt_tokens
|
|
)
|
|
print(f"Prompt tokens: {usage.prompt_tokens}")
|
|
print(f"Expected flat cost: ${expected_flat_cost:.9f}")
|
|
print(f"Total tracked cost: ${tracked_cost:.9f}")
|
|
|
|
# Total cost should be at least the flat cost
|
|
assert (
|
|
tracked_cost >= expected_flat_cost
|
|
), f"Cost ${tracked_cost:.9f} should be >= flat cost ${expected_flat_cost:.9f}"
|
|
|
|
# Verify the flat cost is non-zero
|
|
assert expected_flat_cost > 0, "Flat cost should be greater than 0"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_azure_ai_model_router_streaming_model_in_chunk():
|
|
"""
|
|
Test that Azure AI model router streaming returns the actual model in each chunk.
|
|
The response should contain the actual model used (e.g., gpt-4.1-nano) not the request model (azure-model-router).
|
|
"""
|
|
litellm.turn_on_debug()
|
|
response = await litellm.acompletion(
|
|
model="azure_ai/azure-model-router",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_base=os.getenv("AZURE_MODEL_ROUTER_API_BASE"),
|
|
api_key=os.getenv("AZURE_MODEL_ROUTER_API_KEY"),
|
|
stream=True,
|
|
)
|
|
|
|
# Collect chunks and check model field
|
|
chunks_with_model = []
|
|
async for chunk in response:
|
|
print(f"Chunk model: {chunk.model}")
|
|
if chunk.model and chunk.model.strip():
|
|
chunks_with_model.append(chunk.model)
|
|
|
|
print(f"All chunk models: {chunks_with_model}")
|
|
|
|
# At least some chunks should have a model
|
|
assert len(chunks_with_model) > 0, "No chunks had a model field set"
|
|
|
|
# The model should NOT be azure-model-router (the request model)
|
|
# It should be the actual model from the response (e.g., gpt-4.1-nano, gpt-5-nano, etc.)
|
|
for model in chunks_with_model:
|
|
assert (
|
|
model != "azure-model-router"
|
|
), f"Chunk model should be actual model, not request model. Got: {model}"
|
|
# The actual model should be a real model name like gpt-4.1-nano, gpt-5-nano, etc.
|
|
print(f"Verified chunk has actual model: {model}")
|
|
|
|
|
|
class AzureModelRouterStreamingCallback(
|
|
litellm.integrations.custom_logger.CustomLogger
|
|
):
|
|
"""
|
|
Custom callback to capture streaming cost tracking for Azure Model Router.
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.standard_logging_payload = None
|
|
self.response_cost = None
|
|
self.async_success_called = False
|
|
self.complete_streaming_response = None
|
|
super().__init__()
|
|
|
|
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
|
|
print(f"async_log_success_event called")
|
|
self.async_success_called = True
|
|
self.standard_logging_payload = kwargs.get("standard_logging_object")
|
|
self.complete_streaming_response = kwargs.get("complete_streaming_response")
|
|
|
|
if self.standard_logging_payload:
|
|
self.response_cost = self.standard_logging_payload.get("response_cost")
|
|
print(
|
|
f"standard_logging_payload model: {self.standard_logging_payload.get('model')}"
|
|
)
|
|
print(f"standard_logging_payload response_cost: {self.response_cost}")
|
|
|
|
if self.complete_streaming_response:
|
|
print(
|
|
f"complete_streaming_response model: {self.complete_streaming_response.model}"
|
|
)
|
|
print(
|
|
f"complete_streaming_response usage: {self.complete_streaming_response.usage}"
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_azure_ai_model_router_streaming_cost_with_stream_options():
|
|
"""
|
|
Test Azure AI model router streaming cost tracking with stream_options include_usage=True.
|
|
This tests the specific case where cost tracking fails with stream_options.
|
|
"""
|
|
litellm.logging_callback_manager._reset_all_callbacks()
|
|
test_callback = AzureModelRouterStreamingCallback()
|
|
litellm.callbacks = [test_callback]
|
|
|
|
try:
|
|
litellm.turn_on_debug()
|
|
response = await litellm.acompletion(
|
|
model="azure_ai/azure-model-router",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_base=os.getenv("AZURE_MODEL_ROUTER_API_BASE"),
|
|
api_key=os.getenv("AZURE_MODEL_ROUTER_API_KEY"),
|
|
stream=True,
|
|
stream_options={"include_usage": True},
|
|
)
|
|
|
|
# Consume the stream and check chunks
|
|
full_response = ""
|
|
chunks_with_model = []
|
|
async for chunk in response:
|
|
print(
|
|
f"Chunk: model={chunk.model}, choices={len(chunk.choices) if chunk.choices else 0}"
|
|
)
|
|
if chunk.model:
|
|
chunks_with_model.append(chunk.model)
|
|
if (
|
|
chunk.choices
|
|
and chunk.choices[0].delta
|
|
and chunk.choices[0].delta.content
|
|
):
|
|
full_response += chunk.choices[0].delta.content
|
|
|
|
print(f"Full streamed response: {full_response}")
|
|
print(f"Chunks with model: {chunks_with_model}")
|
|
|
|
# Give async logging time to complete
|
|
import asyncio
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
# Verify callback was called
|
|
assert (
|
|
test_callback.async_success_called is True
|
|
), "async_log_success_event was not called"
|
|
assert (
|
|
test_callback.standard_logging_payload is not None
|
|
), "standard_logging_payload is None"
|
|
|
|
# Check response cost
|
|
print(f"Final response_cost: {test_callback.response_cost}")
|
|
|
|
# The first chunk may have the request model (azure-model-router) because it's created
|
|
# before the API response is received. Subsequent chunks should have the actual model.
|
|
# At least some chunks should have the actual model (not azure-model-router)
|
|
actual_model_chunks = [
|
|
m for m in chunks_with_model if m != "azure-model-router"
|
|
]
|
|
assert (
|
|
len(actual_model_chunks) > 0
|
|
), "No chunks had the actual model from the API response"
|
|
print(f"Chunks with actual model: {actual_model_chunks}")
|
|
|
|
# Verify response cost is tracked - this is the main goal of this test
|
|
assert (
|
|
test_callback.response_cost is not None
|
|
), "response_cost is None with stream_options"
|
|
assert (
|
|
test_callback.response_cost > 0
|
|
), f"response_cost should be > 0, got {test_callback.response_cost}"
|
|
print(
|
|
f"Streaming cost tracking with stream_options passed. Cost: {test_callback.response_cost}"
|
|
)
|
|
|
|
finally:
|
|
litellm.logging_callback_manager._reset_all_callbacks()
|
|
litellm.callbacks = []
|