litellm/tests/unit/llms/azure/test_audio_transcriptions.py
devin-ai-integration[bot] 1dbde0845e
test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758)
* test: move guardrails, image, audio, batches, search legacy tests to unit

* test: restore the custom guardrail and remaining chunk 1 unit rows

* test: move the ocr matrix to e2e

* test: drop comments from moved tests and fix vertex ocr credentials

* test: move litellm utils and unified google legacy tests to their tiers

* test: move logging callback and pass-through unit legacy tests to their tiers

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: move store_model_in_db legacy tests and start the admin ui migration

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: finish the proxy job rows and drop their CircleCI jobs

Moves the remaining multi-instance, openai endpoint, otel, pass-through and
top-level proxy tests into tests/integration with scripted upstreams, rewrites
the weak replacements to assert the outbound wire and exact responses, and
removes the build_and_test job group this PR owns from CircleCI

* test: tighten the key generation and error log assertions, rename migrated unit files to their mapped names

* test: shard the pagerduty unit tests and record the rust harness file on the coverage allowlist

* test: drive the SSO sign-in tests through a scripted IdP and isolate the cost center stand-in

* test: type the e2e beta header, prompt cache and secret manager values

* test: assert the missing model error message on the OpenAI routes

* test: drop the deleted otel base class import and restore mcp redaction and exact langfuse usage checks

* test: make the langfuse fixture directory a package for the layout check

* test: restore collected no-match cases, real langfuse and gcs payload checks, and gate the aws secret lane

* test: keep the ocr e2e pdf fixture next to the test

* test: put meta below covers on the streaming tool search e2e

* test: run the OCR matrix against a scripted upstream and fix two runner-only e2e failures

* test: count only OCR calls in the OCR matrix recorder

* test: pin OCR parse bodies and billed cost, group new e2e cases into classes

* ci: restore the python image anchor dropped in the main sync

---------

Co-authored-by: yuneng <yuneng@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-11 00:17:21 -07:00

162 lines
5.6 KiB
Python

import json
from pathlib import Path
from typing import Final
import httpx
import pytest
from openai import AsyncAzureOpenAI, AzureOpenAI
import litellm
from litellm.cost_calculator import completion_cost
from litellm.litellm_core_utils.audio_utils.utils import calculate_request_duration
AUDIO_FILE: Final = Path(__file__).parents[3] / "gettysburg.wav"
WHISPER_COST_PER_SECOND: Final = 0.0001
def _audio_file() -> tuple[str, bytes, str]:
return ("gettysburg.wav", AUDIO_FILE.read_bytes(), "audio/wav")
def _transcription_client() -> AzureOpenAI:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json={"text": "Four score and seven years ago"})
return AzureOpenAI(
api_key="test-key",
api_version="2024-06-01",
azure_endpoint="https://example.cognitiveservices.azure.com",
http_client=httpx.Client(transport=httpx.MockTransport(handler)),
)
def test_azure_transcription_keeps_the_azure_provider():
with AUDIO_FILE.open("rb") as audio:
response = litellm.transcription(
model="azure/whisper-1",
file=audio,
api_base="https://example.openai.azure.com",
api_key="test-key",
api_version="2024-06-01",
client=_transcription_client(),
)
assert response._hidden_params["custom_llm_provider"] == "azure"
assert json.loads(response.model_dump_json())["text"] == "Four score and seven years ago"
@pytest.mark.asyncio
async def test_azure_transcribe_model_mapping():
"""
Test that Azure transcription models are correctly mapped and not hardcoded to whisper-1.
This test validates that the request body contains the correct model parameter.
"""
from unittest.mock import AsyncMock, patch, MagicMock
from openai import AsyncAzureOpenAI
from pydantic import BaseModel as PydanticBaseModel
class MockTranscriptionResponse(PydanticBaseModel):
text: str
mock_transcription_response = MockTranscriptionResponse(
text="This is a test transcription"
)
mock_raw_response = MagicMock()
mock_raw_response.headers = {"content-type": "application/json"}
mock_raw_response.parse = MagicMock(return_value=mock_transcription_response)
mock_azure_client = MagicMock(spec=AsyncAzureOpenAI)
mock_azure_client.audio.transcriptions.with_raw_response.create = AsyncMock(
return_value=mock_raw_response
)
mock_azure_client.api_key = "test-api-key"
mock_azure_client._base_url = MagicMock()
mock_azure_client._base_url._uri_reference = (
"https://my-endpoint-europe-berri-992.openai.azure.com/"
)
with patch(
"litellm.llms.azure.audio_transcriptions.AzureAudioTranscription.get_azure_openai_client",
return_value=mock_azure_client,
):
response = await litellm.atranscription(
model="azure/whisper-1",
file=_audio_file(),
response_format="json",
api_key="test-api-key",
api_base="https://my-endpoint-europe-berri-992.openai.azure.com/",
api_version="2024-02-15-preview",
drop_params=True,
)
mock_azure_client.audio.transcriptions.with_raw_response.create.assert_called_once()
call_kwargs = (
mock_azure_client.audio.transcriptions.with_raw_response.create.call_args.kwargs
)
assert (
call_kwargs["model"] == "whisper-1"
), f"Expected model 'whisper-1', got {call_kwargs['model']}"
assert "file" in call_kwargs
assert call_kwargs["response_format"] == "json"
assert response._hidden_params is not None
assert response._hidden_params["model"] == "whisper-1"
assert response._hidden_params["custom_llm_provider"] == "azure"
assert response.text is not None
@pytest.mark.asyncio
@pytest.mark.parametrize(
("response_format", "body", "content_type", "expected_text"),
[
("json", b'{"text":"Four score and seven years ago"}', "application/json", "Four score and seven years ago"),
(
"vtt",
b"WEBVTT\n\n00:00:00.000 --> 00:00:02.000\nFour score and seven years ago",
"text/vtt",
"WEBVTT\n\n00:00:00.000 --> 00:00:02.000\nFour score and seven years ago",
),
(
"verbose_json",
b'{"task":"transcribe","language":"English","duration":2.0,"text":"Four score and seven years ago"}',
"application/json",
"Four score and seven years ago",
),
],
)
async def test_azure_transcription_parses_response_formats(
response_format: str,
body: bytes,
content_type: str,
expected_text: str,
) -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
status_code=200,
content=body,
headers={"content-type": content_type},
)
async with AsyncAzureOpenAI(
api_key="test-key",
api_version="2024-06-01",
azure_endpoint="https://example.cognitiveservices.azure.com",
http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
) as client:
response = await litellm.atranscription(
model="azure/whisper-1",
file=_audio_file(),
api_base="https://example.openai.azure.com",
api_key="test-key",
api_version="2024-06-01",
response_format=response_format,
timestamp_granularities=["word"] if response_format == "verbose_json" else None,
client=client,
drop_params=True,
)
assert response.text == expected_text