mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-24 00:52:24 +00:00
test: migrate phase 14 wave 2 provider tests to tests/unit
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
9e39c751ed
commit
a69ca90ea5
31 changed files with 176 additions and 1044 deletions
|
|
@ -1,337 +0,0 @@
|
|||
"""
|
||||
Tests for IBM WatsonX Audio Transcription.
|
||||
|
||||
Validates that litellm.transcription transforms requests correctly for WatsonX.
|
||||
"""
|
||||
|
||||
import json
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
import litellm
|
||||
from litellm.llms.watsonx.audio_transcription.transformation import (
|
||||
IBMWatsonXAudioTranscriptionConfig,
|
||||
)
|
||||
from litellm.types.utils import TranscriptionResponse
|
||||
|
||||
|
||||
class TestWatsonXAudioTranscription:
|
||||
"""Tests for WatsonX audio transcription via litellm.transcription."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watsonx_transcription_url_and_headers(self):
|
||||
"""
|
||||
Test that litellm.transcription sends request to correct WatsonX URL with proper headers.
|
||||
"""
|
||||
captured_request = {}
|
||||
|
||||
async def mock_post(*args, **kwargs):
|
||||
captured_request["url"] = str(kwargs.get("url", args[0] if args else None))
|
||||
captured_request["headers"] = kwargs.get("headers", {})
|
||||
captured_request["data"] = kwargs.get("data", {})
|
||||
captured_request["files"] = kwargs.get("files", {})
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "test transcription",
|
||||
"duration": 1.0,
|
||||
}
|
||||
mock_response.status_code = 200
|
||||
return mock_response
|
||||
|
||||
with patch(
|
||||
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
||||
new=mock_post,
|
||||
):
|
||||
try:
|
||||
await litellm.atranscription(
|
||||
model="watsonx/whisper-large-v3-turbo",
|
||||
file=b"fake_audio_data",
|
||||
api_base="https://us-south.ml.cloud.ibm.com",
|
||||
api_key="test-api-key",
|
||||
project_id="test-project-123",
|
||||
token="test-bearer-token",
|
||||
)
|
||||
except Exception:
|
||||
pass # We just want to capture the request
|
||||
|
||||
# Validate URL contains WatsonX audio transcription endpoint
|
||||
assert "/ml/v1/audio/transcriptions" in captured_request["url"]
|
||||
assert "version=" in captured_request["url"]
|
||||
# project_id should NOT be in URL (it should be in form data instead)
|
||||
assert "project_id=test-project-123" not in captured_request["url"]
|
||||
|
||||
# Validate headers contain WatsonX auth
|
||||
assert "Authorization" in captured_request["headers"]
|
||||
assert (
|
||||
"Bearer test-bearer-token" in captured_request["headers"]["Authorization"]
|
||||
)
|
||||
|
||||
# Validate Content-Type is NOT set (httpx sets multipart/form-data automatically)
|
||||
assert "Content-Type" not in captured_request["headers"]
|
||||
|
||||
# Validate project_id is in form data, not URL
|
||||
assert captured_request["data"].get("project_id") == "test-project-123"
|
||||
|
||||
# Validate file is in files dict
|
||||
assert "file" in captured_request["files"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watsonx_transcription_request_body(self):
|
||||
"""
|
||||
Test that litellm.transcription sends correct request body for WatsonX.
|
||||
|
||||
Validates that:
|
||||
- Request uses multipart/form-data (data + files)
|
||||
- Model name has watsonx/ prefix removed
|
||||
- project_id is in form data, not URL
|
||||
- Audio file is in files dict
|
||||
- OpenAI params are included in form data
|
||||
"""
|
||||
captured_request = {}
|
||||
|
||||
async def mock_post(*args, **kwargs):
|
||||
captured_request["data"] = kwargs.get("data", {})
|
||||
captured_request["files"] = kwargs.get("files", {})
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "test transcription",
|
||||
"duration": 1.0,
|
||||
}
|
||||
mock_response.status_code = 200
|
||||
return mock_response
|
||||
|
||||
with patch(
|
||||
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
||||
new=mock_post,
|
||||
):
|
||||
try:
|
||||
await litellm.atranscription(
|
||||
model="watsonx/whisper-large-v3-turbo",
|
||||
file=b"fake_audio_data",
|
||||
api_base="https://us-south.ml.cloud.ibm.com",
|
||||
api_key="test-api-key",
|
||||
project_id="test-project-123",
|
||||
token="test-bearer-token",
|
||||
language="en",
|
||||
temperature=0.5,
|
||||
)
|
||||
except Exception:
|
||||
pass # We just want to capture the request
|
||||
|
||||
# Validate form data contains expected fields
|
||||
data = captured_request.get("data", {})
|
||||
|
||||
print("JSON DUMPS captured_request:")
|
||||
print(json.dumps(captured_request, indent=4, default=str))
|
||||
|
||||
# Model name should NOT have watsonx/ prefix
|
||||
assert data.get("model") == "whisper-large-v3-turbo"
|
||||
|
||||
# project_id should be in form data
|
||||
assert data.get("project_id") == "test-project-123"
|
||||
|
||||
# OpenAI params should be in form data
|
||||
assert data.get("language") == "en"
|
||||
assert data.get("temperature") == 0.5
|
||||
# response_format should NOT be set by default - only send what user specifies
|
||||
assert "response_format" not in data
|
||||
|
||||
# Validate file is in files dict (multipart/form-data)
|
||||
files = captured_request.get("files", {})
|
||||
assert "file" in files
|
||||
assert isinstance(
|
||||
files["file"], tuple
|
||||
) # Should be (filename, content, content_type)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watsonx_transcription_only_user_params_sent_with_project_id(self):
|
||||
"""
|
||||
Test that only user-specified params are sent in request body to WatsonX.
|
||||
|
||||
LiteLLM should NOT add extra params like response_format if user didn't specify them.
|
||||
"""
|
||||
captured_request = {}
|
||||
|
||||
async def mock_post(*args, **kwargs):
|
||||
captured_request["data"] = kwargs.get("data", {})
|
||||
captured_request["files"] = kwargs.get("files", {})
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "test transcription",
|
||||
"duration": 1.0,
|
||||
}
|
||||
mock_response.status_code = 200
|
||||
return mock_response
|
||||
|
||||
with patch(
|
||||
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
||||
new=mock_post,
|
||||
):
|
||||
try:
|
||||
# Minimal request - only required params
|
||||
await litellm.atranscription(
|
||||
model="watsonx/whisper-large-v3-turbo",
|
||||
file=b"fake_audio_data",
|
||||
api_base="https://us-south.ml.cloud.ibm.com",
|
||||
api_key="test-api-key",
|
||||
project_id="test-project-123",
|
||||
token="test-bearer-token",
|
||||
)
|
||||
except Exception:
|
||||
pass # We just want to capture the request
|
||||
|
||||
data = captured_request.get("data", {})
|
||||
|
||||
# These are the ONLY keys that should be in data
|
||||
expected_keys = {"model", "project_id"}
|
||||
actual_keys = set(data.keys())
|
||||
|
||||
assert actual_keys == expected_keys, (
|
||||
f"Request body should only contain {expected_keys}, "
|
||||
f"but got {actual_keys}. "
|
||||
f"Extra keys: {actual_keys - expected_keys}"
|
||||
)
|
||||
|
||||
# Specifically verify response_format is NOT added
|
||||
assert (
|
||||
"response_format" not in data
|
||||
), "response_format should NOT be added by default"
|
||||
|
||||
# Verify file is sent separately
|
||||
files = captured_request.get("files", {})
|
||||
assert "file" in files
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_watsonx_transcription_only_user_params_sent_with_space_id(self):
|
||||
"""
|
||||
Test that only user-specified params are sent in request body to WatsonX.
|
||||
|
||||
LiteLLM should NOT add extra params like response_format if user didn't specify them.
|
||||
"""
|
||||
captured_request = {}
|
||||
|
||||
async def mock_post(*args, **kwargs):
|
||||
captured_request["data"] = kwargs.get("data", {})
|
||||
captured_request["files"] = kwargs.get("files", {})
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "test transcription",
|
||||
"duration": 1.0,
|
||||
}
|
||||
mock_response.status_code = 200
|
||||
return mock_response
|
||||
|
||||
with patch(
|
||||
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
||||
new=mock_post,
|
||||
):
|
||||
try:
|
||||
# Minimal request - only required params
|
||||
await litellm.atranscription(
|
||||
model="watsonx/whisper-large-v3-turbo",
|
||||
file=b"fake_audio_data",
|
||||
api_base="https://us-south.ml.cloud.ibm.com",
|
||||
api_key="test-api-key",
|
||||
space_id="test-space_id-123",
|
||||
token="test-bearer-token",
|
||||
)
|
||||
except Exception:
|
||||
pass # We just want to capture the request
|
||||
|
||||
data = captured_request.get("data", {})
|
||||
|
||||
# These are the ONLY keys that should be in data
|
||||
expected_keys = {"model", "space_id"}
|
||||
actual_keys = set(data.keys())
|
||||
|
||||
assert actual_keys == expected_keys, (
|
||||
f"Request body should only contain {expected_keys}, "
|
||||
f"but got {actual_keys}. "
|
||||
f"Extra keys: {actual_keys - expected_keys}"
|
||||
)
|
||||
|
||||
# Specifically verify response_format is NOT added
|
||||
assert (
|
||||
"response_format" not in data
|
||||
), "response_format should NOT be added by default"
|
||||
|
||||
# Verify file is sent separately
|
||||
files = captured_request.get("files", {})
|
||||
assert "file" in files
|
||||
|
||||
def test_transform_audio_transcription_response_removes_model_field(self):
|
||||
"""
|
||||
Test that transform_audio_transcription_response removes the 'model' field
|
||||
from WatsonX response before creating TranscriptionResponse.
|
||||
|
||||
This test ensures that when WatsonX returns a response with a 'model' field,
|
||||
it is removed before creating the TranscriptionResponse object, since
|
||||
TranscriptionResponse doesn't accept a 'model' parameter.
|
||||
"""
|
||||
handler = IBMWatsonXAudioTranscriptionConfig()
|
||||
|
||||
# Mock response with 'model' field (as WatsonX may return)
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "Hello, this is a test transcription.",
|
||||
"model": "whisper-large-v3-turbo", # This field should be removed
|
||||
"duration": 5.5,
|
||||
}
|
||||
mock_response.text = '{"text": "Hello, this is a test transcription.", "model": "whisper-large-v3-turbo", "duration": 5.5}'
|
||||
|
||||
# This should not raise a TypeError - model field should be removed
|
||||
result = handler.transform_audio_transcription_response(mock_response)
|
||||
|
||||
# Verify the result is a TranscriptionResponse
|
||||
assert isinstance(result, TranscriptionResponse)
|
||||
|
||||
# Verify the text is correct
|
||||
assert result.text == "Hello, this is a test transcription."
|
||||
|
||||
# Verify duration is set via dictionary assignment
|
||||
assert result["duration"] == 5.5
|
||||
|
||||
# Verify the model field is NOT in the serialized result
|
||||
# Check via model_dump() or dict() to ensure it's not in the output
|
||||
try:
|
||||
result_dict = result.model_dump()
|
||||
except AttributeError:
|
||||
# Fallback for pydantic v1
|
||||
result_dict = result.dict()
|
||||
|
||||
# The 'model' field should not be in the result
|
||||
assert "model" not in result_dict, "Model field should be removed from response"
|
||||
|
||||
def test_transform_audio_transcription_response_without_model_field(self):
|
||||
"""
|
||||
Test that transform_audio_transcription_response works correctly
|
||||
when WatsonX response doesn't include a 'model' field.
|
||||
"""
|
||||
handler = IBMWatsonXAudioTranscriptionConfig()
|
||||
|
||||
# Mock response without 'model' field
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "Hello, this is a test transcription.",
|
||||
"duration": 5.5,
|
||||
}
|
||||
mock_response.text = (
|
||||
'{"text": "Hello, this is a test transcription.", "duration": 5.5}'
|
||||
)
|
||||
|
||||
result = handler.transform_audio_transcription_response(mock_response)
|
||||
|
||||
# Verify the result is a TranscriptionResponse
|
||||
assert isinstance(result, TranscriptionResponse)
|
||||
|
||||
# Verify the text is correct
|
||||
assert result.text == "Hello, this is a test transcription."
|
||||
|
||||
# Verify duration is set via dictionary assignment
|
||||
assert result["duration"] == 5.5
|
||||
|
|
@ -1,577 +0,0 @@
|
|||
import json
|
||||
|
||||
from typing import Optional
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm import completion
|
||||
from litellm.llms.custom_httpx.http_handler import HTTPHandler
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def watsonx_chat_completion_call():
|
||||
def _call(
|
||||
model="watsonx/my-test-model",
|
||||
messages=None,
|
||||
api_key="test_api_key",
|
||||
space_id: Optional[str] = None,
|
||||
headers=None,
|
||||
client=None,
|
||||
patch_token_call=True,
|
||||
):
|
||||
if messages is None:
|
||||
messages = [{"role": "user", "content": "Hello, how are you?"}]
|
||||
if client is None:
|
||||
client = HTTPHandler()
|
||||
|
||||
if patch_token_call:
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {
|
||||
"access_token": "mock_access_token",
|
||||
"expires_in": 3600,
|
||||
}
|
||||
mock_response.raise_for_status = Mock() # No-op to simulate no exception
|
||||
|
||||
with (
|
||||
patch.object(client, "post") as mock_post,
|
||||
patch.object(
|
||||
litellm.module_level_client, "post", return_value=mock_response
|
||||
) as mock_get,
|
||||
):
|
||||
try:
|
||||
completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
api_key=api_key,
|
||||
headers=headers or {},
|
||||
client=client,
|
||||
space_id=space_id,
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
return mock_post, mock_get
|
||||
else:
|
||||
with patch.object(client, "post") as mock_post:
|
||||
try:
|
||||
completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
api_key=api_key,
|
||||
headers=headers or {},
|
||||
client=client,
|
||||
space_id=space_id,
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return mock_post, None
|
||||
|
||||
return _call
|
||||
|
||||
|
||||
def test_watsonx_deployment_model_id_not_in_payload(
|
||||
monkeypatch, watsonx_chat_completion_call
|
||||
):
|
||||
"""Test that deployment models do not include 'model_id' in the request payload"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
model = "watsonx/deployment/test-deployment-id"
|
||||
messages = [{"role": "user", "content": "Test message"}]
|
||||
|
||||
mock_post, _ = watsonx_chat_completion_call(model=model, messages=messages)
|
||||
|
||||
assert mock_post.call_count == 1
|
||||
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
||||
# Ensure model_id is not in the payload for deployment models
|
||||
assert "model_id" not in json_data or json_data["model_id"] is None
|
||||
# Ensure project_id is also not in the payload for deployment models
|
||||
assert "project_id" not in json_data or json_data["project_id"] is None
|
||||
|
||||
|
||||
def test_watsonx_regular_model_includes_model_id(
|
||||
monkeypatch, watsonx_chat_completion_call
|
||||
):
|
||||
"""Test that regular models include 'model_id' in the request payload"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
model = "watsonx/regular-model"
|
||||
messages = [{"role": "user", "content": "Test message"}]
|
||||
|
||||
mock_post, _ = watsonx_chat_completion_call(model=model, messages=messages)
|
||||
|
||||
assert mock_post.call_count == 1
|
||||
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
||||
# Ensure model_id is included in the payload for regular models
|
||||
assert "model_id" in json_data
|
||||
assert json_data["model_id"] == "regular-model" # Provider prefix is stripped
|
||||
# Ensure project_id is also included for regular models
|
||||
assert "project_id" in json_data
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def watsonx_completion_call():
|
||||
def _call(
|
||||
model="watsonx_text/my-test-model",
|
||||
prompt="Hello, how are you?",
|
||||
api_key="test_api_key",
|
||||
space_id: Optional[str] = None,
|
||||
headers=None,
|
||||
client=None,
|
||||
patch_token_call=True,
|
||||
):
|
||||
if client is None:
|
||||
client = HTTPHandler()
|
||||
|
||||
if patch_token_call:
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {
|
||||
"access_token": "mock_access_token",
|
||||
"expires_in": 3600,
|
||||
}
|
||||
mock_response.raise_for_status = Mock()
|
||||
|
||||
with (
|
||||
patch.object(client, "post") as mock_post,
|
||||
patch.object(
|
||||
litellm.module_level_client, "post", return_value=mock_response
|
||||
) as mock_get,
|
||||
):
|
||||
try:
|
||||
litellm.text_completion(
|
||||
model=model,
|
||||
prompt=prompt,
|
||||
api_key=api_key,
|
||||
headers=headers or {},
|
||||
client=client,
|
||||
space_id=space_id,
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
return mock_post, mock_get
|
||||
else:
|
||||
with patch.object(client, "post") as mock_post:
|
||||
try:
|
||||
litellm.text_completion(
|
||||
model=model,
|
||||
prompt=prompt,
|
||||
api_key=api_key,
|
||||
headers=headers or {},
|
||||
client=client,
|
||||
space_id=space_id,
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return mock_post, None
|
||||
|
||||
return _call
|
||||
|
||||
|
||||
def test_watsonx_completion_deployment_model_id_not_in_payload(
|
||||
monkeypatch, watsonx_completion_call
|
||||
):
|
||||
"""Test that deployment models do not include 'model_id' in completion request payload"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
model = "watsonx_text/deployment/test-deployment-id"
|
||||
prompt = "Test prompt"
|
||||
|
||||
mock_post, _ = watsonx_completion_call(model=model, prompt=prompt)
|
||||
|
||||
assert mock_post.call_count == 1
|
||||
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
||||
# Ensure model_id is not in the payload for deployment models
|
||||
assert "model_id" not in json_data
|
||||
# Ensure project_id is also not in the payload for deployment models
|
||||
assert "project_id" not in json_data
|
||||
|
||||
|
||||
def test_watsonx_completion_regular_model_includes_model_id(
|
||||
monkeypatch, watsonx_completion_call
|
||||
):
|
||||
"""Test that regular models include 'model_id' in completion request payload"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
model = "watsonx_text/regular-model"
|
||||
prompt = "Test prompt"
|
||||
|
||||
mock_post, _ = watsonx_completion_call(model=model, prompt=prompt)
|
||||
|
||||
assert mock_post.call_count == 1
|
||||
json_data = json.loads(mock_post.call_args.kwargs["data"])
|
||||
# Ensure model_id is included in the payload for regular models
|
||||
assert "model_id" in json_data
|
||||
assert json_data["model_id"] == "regular-model" # Provider prefix is stripped
|
||||
# Ensure project_id is also included for regular models
|
||||
assert "project_id" in json_data
|
||||
|
||||
|
||||
def test_watsonx_gpt_oss_prompt_transformation(monkeypatch):
|
||||
"""
|
||||
Test that gpt-oss-120b model transforms messages to proper format instead of simple concatenation.
|
||||
|
||||
This test calls litellm.completion (sync) and verifies what gets sent in the final POST request body.
|
||||
Input messages should be transformed using the HuggingFace chat template from openai/gpt-oss-120b,
|
||||
not just concatenated as "You are chatgpt Hi there".
|
||||
"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
|
||||
# Test with gpt-oss model using watsonx_text provider (text generation endpoint)
|
||||
model = "watsonx_text/openai/gpt-oss-120b"
|
||||
|
||||
# Input messages
|
||||
messages = [
|
||||
{"role": "system", "content": "You are chatgpt"},
|
||||
{"role": "user", "content": "Hi there"},
|
||||
]
|
||||
|
||||
client = HTTPHandler()
|
||||
|
||||
# Mock HuggingFace template fetch to make test deterministic and avoid network flakiness.
|
||||
# The test verifies that prompt transformation occurs (not simple concatenation), not the exact
|
||||
# HuggingFace template format. Using a mock template that produces the correct format is sufficient.
|
||||
#
|
||||
# Mock template that produces gpt-oss-120b-like format.
|
||||
# Note: This is a simplified version of the actual template. The real template is more complex
|
||||
# (adds metadata, handles tools, thinking messages, etc.), but this captures the key aspects:
|
||||
# - Converts system role to developer (matching real template behavior)
|
||||
# - Uses the same tag structure (<|start|>, <|message|>, <|end|>)
|
||||
# - Preserves message content
|
||||
mock_tokenizer_config = {
|
||||
"status": "success",
|
||||
"tokenizer": {
|
||||
"chat_template": "{% for message in messages %}{% if message['role'] == 'system' %}<|start|>developer<|message|>{% else %}<|start|>{{ message['role'] }}<|message|>{% endif %}{{ message['content'] }}<|end|>{% endfor %}",
|
||||
"bos_token": None,
|
||||
"eos_token": None,
|
||||
},
|
||||
}
|
||||
|
||||
# Isolate known_tokenizer_config so parallel tests don't interfere.
|
||||
# monkeypatch.setitem restores the original value on teardown.
|
||||
hf_model = "openai/gpt-oss-120b"
|
||||
monkeypatch.setitem(litellm.known_tokenizer_config, hf_model, mock_tokenizer_config)
|
||||
|
||||
# Mock IAM token generation to avoid real HTTP calls.
|
||||
mock_token_response = Mock()
|
||||
mock_token_response.json.return_value = {
|
||||
"access_token": "mock_access_token",
|
||||
"expires_in": 3600,
|
||||
}
|
||||
mock_token_response.raise_for_status = Mock()
|
||||
|
||||
with (
|
||||
patch.object(client, "post") as mock_post,
|
||||
patch.object(
|
||||
litellm.module_level_client, "post", return_value=mock_token_response
|
||||
),
|
||||
):
|
||||
try:
|
||||
completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
api_key="test_api_key",
|
||||
client=client,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Caught expected exception: {e}")
|
||||
|
||||
# Verify the POST was called
|
||||
assert (
|
||||
mock_post.call_count == 1
|
||||
), f"POST should have been called exactly once, got {mock_post.call_count}"
|
||||
|
||||
# Get the request body
|
||||
call_args = mock_post.call_args
|
||||
assert "data" in call_args.kwargs, "call_args.kwargs should contain 'data'"
|
||||
json_data = json.loads(call_args.kwargs["data"])
|
||||
|
||||
# Verify the transformed input is in the request
|
||||
assert "input" in json_data, "Request should have 'input' field"
|
||||
transformed_prompt = json_data["input"]
|
||||
|
||||
# Verify it's NOT simple concatenation
|
||||
simple_concat = "You are chatgpt Hi there"
|
||||
assert transformed_prompt != simple_concat, (
|
||||
f"Prompt should not be simple concatenation.\n"
|
||||
f"Expected: Chat template with <|start|> tags\n"
|
||||
f"Got: {transformed_prompt}"
|
||||
)
|
||||
|
||||
# Verify it contains proper chat template formatting
|
||||
assert "<|start|>" in transformed_prompt, "Prompt should contain <|start|> tag"
|
||||
assert "<|message|>" in transformed_prompt, "Prompt should contain <|message|> tag"
|
||||
assert "<|end|>" in transformed_prompt, "Prompt should contain <|end|> tag"
|
||||
assert (
|
||||
"You are chatgpt" in transformed_prompt
|
||||
), "Prompt should contain system message content"
|
||||
assert (
|
||||
"Hi there" in transformed_prompt
|
||||
), "Prompt should contain user message content"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.xdist_group("watsonx_heavy")
|
||||
async def test_watsonx_gpt_oss_uses_async_http_handler():
|
||||
"""
|
||||
Test that verifies async HTTP client is used when fetching HuggingFace templates.
|
||||
"""
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from litellm.litellm_core_utils.prompt_templates.huggingface_template_handler import (
|
||||
_aget_chat_template_file,
|
||||
)
|
||||
|
||||
# Mock the async HTTP client
|
||||
mock_async_client = MagicMock()
|
||||
mock_get = AsyncMock()
|
||||
mock_async_client.get = mock_get
|
||||
|
||||
# Create mock response for chat template file
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.content = b"test template content"
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
# Test the async function directly
|
||||
with patch(
|
||||
"litellm.litellm_core_utils.prompt_templates.huggingface_template_handler.get_async_httpx_client",
|
||||
return_value=mock_async_client,
|
||||
):
|
||||
result = await _aget_chat_template_file(hf_model_name="test/model")
|
||||
|
||||
# Verify async HTTP client was called
|
||||
assert mock_get.called, "Async HTTP client's get method should be called"
|
||||
assert mock_get.await_count > 0, "Async HTTP client's get should be awaited"
|
||||
|
||||
# Verify it was called with HuggingFace URL
|
||||
call_args = mock_get.call_args
|
||||
assert call_args is not None, "get should have been called with arguments"
|
||||
called_url = call_args.kwargs.get("url", "")
|
||||
assert (
|
||||
"huggingface.co/test/model" in called_url
|
||||
), f"Should call HuggingFace API for test/model, got: {called_url}"
|
||||
assert result["status"] == "success", "Should return success status"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tokenizer_config_cached", [False, True], ids=["tokenizer_config", "cached_config_jinja"])
|
||||
async def test_watsonx_text_gpt_oss_async_completion_fetches_hf_template_off_the_event_loop(
|
||||
monkeypatch, tokenizer_config_cached
|
||||
):
|
||||
import httpx
|
||||
|
||||
from litellm._uuid import uuid
|
||||
from litellm.litellm_core_utils.prompt_templates import huggingface_template_handler
|
||||
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
||||
|
||||
hf_model = f"openai/gpt-oss-{uuid.uuid4()}"
|
||||
chat_template = "{% for m in messages %}<|{{ m['role'] }}|>{{ m['content'] }}{% endfor %}"
|
||||
if tokenizer_config_cached:
|
||||
cached_config = {"status": "success", "tokenizer": {"bos_token": None, "eos_token": None}}
|
||||
monkeypatch.setattr(litellm, "known_tokenizer_config", {hf_model: cached_config})
|
||||
expected_fetch = f"https://huggingface.co/{hf_model}/raw/main/chat_template.jinja"
|
||||
else:
|
||||
monkeypatch.setattr(litellm, "known_tokenizer_config", {})
|
||||
expected_fetch = f"https://huggingface.co/{hf_model}/raw/main/tokenizer_config.json"
|
||||
hf_fetched = []
|
||||
captured = {}
|
||||
|
||||
def forbid_sync_client():
|
||||
raise AssertionError("sync HuggingFace fetch ran on the request path")
|
||||
|
||||
async def serve_hf_file(url, **kwargs):
|
||||
hf_fetched.append(url)
|
||||
if url.endswith(".jinja"):
|
||||
return httpx.Response(200, content=chat_template.encode())
|
||||
return httpx.Response(200, json={"chat_template": chat_template, "bos_token": None, "eos_token": None})
|
||||
|
||||
monkeypatch.setattr(huggingface_template_handler, "_get_httpx_client", forbid_sync_client)
|
||||
monkeypatch.setattr(huggingface_template_handler, "get_async_httpx_client", lambda **kwargs: Mock(get=serve_hf_file))
|
||||
|
||||
def handle(request):
|
||||
captured["body"] = json.loads(request.content)
|
||||
return httpx.Response(
|
||||
200,
|
||||
json={
|
||||
"model_id": hf_model,
|
||||
"results": [
|
||||
{
|
||||
"generated_text": "Hi",
|
||||
"generated_token_count": 1,
|
||||
"input_token_count": 1,
|
||||
"stop_reason": "eos_token",
|
||||
}
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
client = AsyncHTTPHandler()
|
||||
client.client = httpx.AsyncClient(transport=httpx.MockTransport(handle))
|
||||
|
||||
response = await litellm.acompletion(
|
||||
model=f"watsonx_text/{hf_model}",
|
||||
messages=[{"role": "user", "content": "Hi there"}],
|
||||
api_base="https://test-api.watsonx.ai",
|
||||
project_id="test-project-id",
|
||||
token="test-token",
|
||||
client=client,
|
||||
)
|
||||
|
||||
assert response.choices[0].message.content == "Hi"
|
||||
assert hf_fetched == [expected_fetch]
|
||||
assert captured["body"]["input"] == "<|user|>Hi there"
|
||||
|
||||
|
||||
def test_watsonx_chat_completion_with_reasoning_effort(monkeypatch):
|
||||
"""
|
||||
Test that 'reasoning_effort' is correctly passed through to the WatsonX API payload.
|
||||
"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
|
||||
model = "watsonx/openai/gpt-oss-120b"
|
||||
messages = [{"role": "user", "content": "Test message"}]
|
||||
|
||||
client = HTTPHandler()
|
||||
|
||||
# Mock the token generation call
|
||||
mock_token_response = Mock()
|
||||
mock_token_response.json.return_value = {
|
||||
"access_token": "mock_access_token",
|
||||
"expires_in": 3600,
|
||||
}
|
||||
mock_token_response.raise_for_status = Mock()
|
||||
|
||||
# Call litellm.completion with the new parameter
|
||||
with (
|
||||
patch.object(client, "post") as mock_post,
|
||||
patch.object(
|
||||
litellm.module_level_client, "post", return_value=mock_token_response
|
||||
),
|
||||
):
|
||||
try:
|
||||
completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
api_key="test_api_key",
|
||||
client=client,
|
||||
reasoning_effort="low",
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Caught expected exception: {e}")
|
||||
|
||||
# Verify the parameter is in the final request payload
|
||||
assert (
|
||||
mock_post.call_count == 1
|
||||
), "The completion endpoint should have been called once."
|
||||
|
||||
# Get the JSON data sent in the POST request
|
||||
request_kwargs = mock_post.call_args.kwargs
|
||||
json_data = json.loads(request_kwargs["data"])
|
||||
|
||||
print("\nRequest payload sent to WatsonX API:")
|
||||
print(json.dumps(json_data, indent=2))
|
||||
|
||||
# Check for the parameter at the top level of the payload
|
||||
assert (
|
||||
"reasoning_effort" in json_data
|
||||
), "'reasoning_effort' should be at the top level of the payload."
|
||||
assert (
|
||||
json_data["reasoning_effort"] == "low"
|
||||
), "The value of 'reasoning_effort' should be 'low'."
|
||||
|
||||
|
||||
def test_watsonx_zen_api_key_from_client(monkeypatch, watsonx_chat_completion_call):
|
||||
"""
|
||||
Test that zen_api_key can be passed from client code and is used in Authorization header.
|
||||
"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
|
||||
model = "watsonx/ibm/granite-3-3-8b-instruct"
|
||||
messages = [{"role": "user", "content": "What is your favorite color?"}]
|
||||
|
||||
client = HTTPHandler()
|
||||
|
||||
zen_api_key = "U1ZDLWQo="
|
||||
|
||||
# No need to patch token call since zen_api_key should skip token generation
|
||||
with patch.object(client, "post") as mock_post:
|
||||
try:
|
||||
completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
api_key="test_api_key",
|
||||
client=client,
|
||||
zen_api_key=zen_api_key,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Caught expected exception: {e}")
|
||||
|
||||
# Verify the request was made
|
||||
assert (
|
||||
mock_post.call_count == 1
|
||||
), "The completion endpoint should have been called once."
|
||||
|
||||
# Get the headers sent in the POST request
|
||||
request_kwargs = mock_post.call_args.kwargs
|
||||
headers = request_kwargs["headers"]
|
||||
|
||||
print("\nHeaders sent to WatsonX API:")
|
||||
print(json.dumps(dict(headers), indent=2))
|
||||
|
||||
# Verify Authorization header uses ZenApiKey format
|
||||
assert "Authorization" in headers, "Authorization header should be present."
|
||||
assert headers["Authorization"] == f"ZenApiKey {zen_api_key}", (
|
||||
f"Authorization header should use ZenApiKey format. "
|
||||
f"Expected: 'ZenApiKey {zen_api_key}', Got: '{headers['Authorization']}'"
|
||||
)
|
||||
|
||||
|
||||
def test_watsonx_zen_api_key_from_env(monkeypatch, watsonx_chat_completion_call):
|
||||
"""
|
||||
Test that zen_api_key from environment variable is used in Authorization header.
|
||||
"""
|
||||
monkeypatch.setenv("WATSONX_PROJECT_ID", "test-project-id")
|
||||
monkeypatch.setenv("WATSONX_API_BASE", "https://test-api.watsonx.ai")
|
||||
|
||||
zen_api_key = "U1ZDLWxpdG--==="
|
||||
monkeypatch.setenv("WATSONX_ZENAPIKEY", zen_api_key)
|
||||
|
||||
model = "watsonx/ibm/granite-3-3-8b-instruct"
|
||||
messages = [{"role": "user", "content": "What is your favorite color?"}]
|
||||
|
||||
client = HTTPHandler()
|
||||
|
||||
# No need to patch token call since zen_api_key should skip token generation
|
||||
with patch.object(client, "post") as mock_post:
|
||||
try:
|
||||
completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
api_key="test_api_key",
|
||||
client=client,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Caught expected exception: {e}")
|
||||
|
||||
# Verify the request was made
|
||||
assert (
|
||||
mock_post.call_count == 1
|
||||
), "The completion endpoint should have been called once."
|
||||
|
||||
# Get the headers sent in the POST request
|
||||
request_kwargs = mock_post.call_args.kwargs
|
||||
headers = request_kwargs["headers"]
|
||||
|
||||
print("\nHeaders sent to WatsonX API:")
|
||||
print(json.dumps(dict(headers), indent=2))
|
||||
|
||||
# Verify Authorization header uses ZenApiKey format
|
||||
assert "Authorization" in headers, "Authorization header should be present."
|
||||
assert headers["Authorization"] == f"ZenApiKey {zen_api_key}", (
|
||||
f"Authorization header should use ZenApiKey format. "
|
||||
f"Expected: 'ZenApiKey {zen_api_key}', Got: '{headers['Authorization']}'"
|
||||
)
|
||||
|
|
@ -1 +0,0 @@
|
|||
# XAI Responses API tests
|
||||
|
|
@ -1,105 +0,0 @@
|
|||
"""
|
||||
Tests for XAI Responses API transformation
|
||||
|
||||
Tests the XAIResponsesAPIConfig class that handles XAI-specific
|
||||
transformations for the Responses API.
|
||||
|
||||
Source: litellm/llms/xai/responses/transformation.py
|
||||
"""
|
||||
|
||||
|
||||
|
||||
import pytest
|
||||
from litellm.types.utils import LlmProviders
|
||||
from litellm.utils import ProviderConfigManager
|
||||
from litellm.llms.xai.responses.transformation import XAIResponsesAPIConfig
|
||||
from litellm.types.llms.openai import ResponsesAPIOptionalRequestParams
|
||||
|
||||
|
||||
class TestXAIResponsesAPITransformation:
|
||||
"""Test XAI Responses API configuration and transformations"""
|
||||
|
||||
def test_xai_provider_config_registration(self):
|
||||
"""Test that XAI provider returns XAIResponsesAPIConfig"""
|
||||
config = ProviderConfigManager.get_provider_responses_api_config(
|
||||
model="xai/grok-4-fast",
|
||||
provider=LlmProviders.XAI,
|
||||
)
|
||||
|
||||
assert config is not None, "Config should not be None for XAI provider"
|
||||
assert isinstance(
|
||||
config, XAIResponsesAPIConfig
|
||||
), f"Expected XAIResponsesAPIConfig, got {type(config)}"
|
||||
assert (
|
||||
config.custom_llm_provider == LlmProviders.XAI
|
||||
), "custom_llm_provider should be XAI"
|
||||
|
||||
def test_code_interpreter_container_field_removed(self):
|
||||
"""Test that container field is removed from code_interpreter tools"""
|
||||
config = XAIResponsesAPIConfig()
|
||||
|
||||
params = ResponsesAPIOptionalRequestParams(
|
||||
tools=[{"type": "code_interpreter", "container": {"type": "auto"}}]
|
||||
)
|
||||
|
||||
result = config.map_openai_params(
|
||||
response_api_optional_params=params, model="grok-4-fast", drop_params=False
|
||||
)
|
||||
|
||||
assert "tools" in result
|
||||
assert len(result["tools"]) == 1
|
||||
assert result["tools"][0]["type"] == "code_interpreter"
|
||||
assert (
|
||||
"container" not in result["tools"][0]
|
||||
), "Container field should be removed"
|
||||
|
||||
def test_instructions_parameter_forwarded(self):
|
||||
"""xAI supports 'instructions' on /v1/responses, so it must survive param mapping"""
|
||||
config = XAIResponsesAPIConfig()
|
||||
|
||||
params = ResponsesAPIOptionalRequestParams(
|
||||
instructions="You are a helpful assistant.", temperature=0.7
|
||||
)
|
||||
|
||||
result = config.map_openai_params(
|
||||
response_api_optional_params=params, model="grok-4-fast", drop_params=False
|
||||
)
|
||||
|
||||
assert result.get("instructions") == "You are a helpful assistant."
|
||||
assert result.get("temperature") == 0.7, "Other params should be preserved"
|
||||
|
||||
def test_supported_params_includes_instructions(self):
|
||||
"""A system message bridged to 'instructions' must not be rejected for xAI"""
|
||||
config = XAIResponsesAPIConfig()
|
||||
supported = config.get_supported_openai_params("grok-4-fast")
|
||||
|
||||
assert "instructions" in supported, "instructions should be supported"
|
||||
assert "tools" in supported, "tools should be supported"
|
||||
assert "temperature" in supported, "temperature should be supported"
|
||||
assert "model" in supported, "model should be supported"
|
||||
|
||||
def test_xai_responses_endpoint_url(self):
|
||||
"""Test that get_complete_url returns correct XAI endpoint"""
|
||||
config = XAIResponsesAPIConfig()
|
||||
|
||||
# Test with default XAI API base
|
||||
url = config.get_complete_url(api_base=None, litellm_params={})
|
||||
assert (
|
||||
url == "https://api.x.ai/v1/responses"
|
||||
), f"Expected XAI responses endpoint, got {url}"
|
||||
|
||||
# Test with custom api_base
|
||||
custom_url = config.get_complete_url(
|
||||
api_base="https://custom.x.ai/v1", litellm_params={}
|
||||
)
|
||||
assert (
|
||||
custom_url == "https://custom.x.ai/v1/responses"
|
||||
), f"Expected custom endpoint, got {custom_url}"
|
||||
|
||||
# Test with trailing slash
|
||||
url_with_slash = config.get_complete_url(
|
||||
api_base="https://api.x.ai/v1/", litellm_params={}
|
||||
)
|
||||
assert (
|
||||
url_with_slash == "https://api.x.ai/v1/responses"
|
||||
), "Should handle trailing slash"
|
||||
|
|
@ -98,6 +98,8 @@ class TestCountTokensLocationResolution:
|
|||
self, counter, monkeypatch
|
||||
):
|
||||
"""Claude models without any location should default to us-east5."""
|
||||
monkeypatch.delenv("VERTEXAI_LOCATION", raising=False)
|
||||
monkeypatch.delenv("VERTEX_LOCATION", raising=False)
|
||||
captured = {}
|
||||
|
||||
async def fake_ensure_access_token(
|
||||
|
|
@ -1,6 +1,21 @@
|
|||
import pytest
|
||||
|
||||
import litellm
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def local_model_cost_map(monkeypatch):
|
||||
original_model_cost = litellm.model_cost
|
||||
monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")
|
||||
litellm.model_cost = litellm.get_model_cost_map(url="")
|
||||
litellm.get_model_info.cache_clear()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
litellm.model_cost = original_model_cost
|
||||
litellm.get_model_info.cache_clear()
|
||||
|
||||
|
||||
def test_reasoning_effort_stays_unsupported_on_vertex_partner_models(local_model_cost_map):
|
||||
assert "reasoning_effort" in litellm.get_supported_openai_params(
|
||||
model="mistral-medium-3", custom_llm_provider="mistral"
|
||||
0
tests/unit/llms/vertex_ai/videos/__init__.py
Normal file
0
tests/unit/llms/vertex_ai/videos/__init__.py
Normal file
|
|
@ -137,30 +137,6 @@ class TestVolcengineResponsesAPITransformation:
|
|||
with pytest.raises(ValueError, match='Volcengine API key is required\\. Set ARK_API_KEY /'):
|
||||
config.validate_environment(headers={}, model="volcengine/demo", litellm_params={})
|
||||
|
||||
def test_unsupported_params_are_dropped_with_extra_body(self):
|
||||
"""Unknown fields (including extra_body) should be dropped before send."""
|
||||
config = VolcEngineResponsesAPIConfig()
|
||||
|
||||
request = config.transform_responses_api_request(
|
||||
model="volcengine/demo-model",
|
||||
input="hi",
|
||||
response_api_optional_request_params={
|
||||
"unsupported_custom_param": 0.1,
|
||||
"temperature": 0.2,
|
||||
"metadata": {"k": "v"},
|
||||
"extra_body": {"unsupported_custom_param": 1, "temperature": 0.3},
|
||||
},
|
||||
litellm_params=GenericLiteLLMParams(),
|
||||
headers={},
|
||||
)
|
||||
|
||||
assert "unsupported_custom_param" not in request
|
||||
assert "metadata" not in request
|
||||
assert request["temperature"] == 0.2
|
||||
assert "extra_body" in request
|
||||
assert "unsupported_custom_param" not in request["extra_body"]
|
||||
assert request["extra_body"]["temperature"] == 0.3
|
||||
|
||||
def test_valid_thinking_caching_and_expire_at_pass(self):
|
||||
"""Documented params should pass through without validation errors."""
|
||||
config = VolcEngineResponsesAPIConfig()
|
||||
0
tests/unit/llms/voyage/rerank/__init__.py
Normal file
0
tests/unit/llms/voyage/rerank/__init__.py
Normal file
0
tests/unit/llms/watsonx/__init__.py
Normal file
0
tests/unit/llms/watsonx/__init__.py
Normal file
0
tests/unit/llms/watsonx/audio_transcription/__init__.py
Normal file
0
tests/unit/llms/watsonx/audio_transcription/__init__.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
"""
|
||||
Tests for IBM WatsonX Audio Transcription.
|
||||
|
||||
Validates the WatsonX transcription response transformation.
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from litellm.llms.watsonx.audio_transcription.transformation import (
|
||||
IBMWatsonXAudioTranscriptionConfig,
|
||||
)
|
||||
from litellm.types.utils import TranscriptionResponse
|
||||
|
||||
|
||||
class TestWatsonXAudioTranscription:
|
||||
def test_transform_audio_transcription_response_removes_model_field(self):
|
||||
"""
|
||||
Test that transform_audio_transcription_response removes the 'model' field
|
||||
from WatsonX response before creating TranscriptionResponse.
|
||||
|
||||
This test ensures that when WatsonX returns a response with a 'model' field,
|
||||
it is removed before creating the TranscriptionResponse object, since
|
||||
TranscriptionResponse doesn't accept a 'model' parameter.
|
||||
"""
|
||||
handler = IBMWatsonXAudioTranscriptionConfig()
|
||||
|
||||
# Mock response with 'model' field (as WatsonX may return)
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "Hello, this is a test transcription.",
|
||||
"model": "whisper-large-v3-turbo", # This field should be removed
|
||||
"duration": 5.5,
|
||||
}
|
||||
mock_response.text = '{"text": "Hello, this is a test transcription.", "model": "whisper-large-v3-turbo", "duration": 5.5}'
|
||||
|
||||
# This should not raise a TypeError - model field should be removed
|
||||
result = handler.transform_audio_transcription_response(mock_response)
|
||||
|
||||
# Verify the result is a TranscriptionResponse
|
||||
assert isinstance(result, TranscriptionResponse)
|
||||
|
||||
# Verify the text is correct
|
||||
assert result.text == "Hello, this is a test transcription."
|
||||
|
||||
# Verify duration is set via dictionary assignment
|
||||
assert result["duration"] == 5.5
|
||||
|
||||
# Verify the model field is NOT in the serialized result
|
||||
# Check via model_dump() or dict() to ensure it's not in the output
|
||||
try:
|
||||
result_dict = result.model_dump()
|
||||
except AttributeError:
|
||||
# Fallback for pydantic v1
|
||||
result_dict = result.dict()
|
||||
|
||||
# The 'model' field should not be in the result
|
||||
assert "model" not in result_dict, "Model field should be removed from response"
|
||||
|
||||
def test_transform_audio_transcription_response_without_model_field(self):
|
||||
"""
|
||||
Test that transform_audio_transcription_response works correctly
|
||||
when WatsonX response doesn't include a 'model' field.
|
||||
"""
|
||||
handler = IBMWatsonXAudioTranscriptionConfig()
|
||||
|
||||
# Mock response without 'model' field
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {
|
||||
"text": "Hello, this is a test transcription.",
|
||||
"duration": 5.5,
|
||||
}
|
||||
mock_response.text = (
|
||||
'{"text": "Hello, this is a test transcription.", "duration": 5.5}'
|
||||
)
|
||||
|
||||
result = handler.transform_audio_transcription_response(mock_response)
|
||||
|
||||
# Verify the result is a TranscriptionResponse
|
||||
assert isinstance(result, TranscriptionResponse)
|
||||
|
||||
# Verify the text is correct
|
||||
assert result.text == "Hello, this is a test transcription."
|
||||
|
||||
# Verify duration is set via dictionary assignment
|
||||
assert result["duration"] == 5.5
|
||||
0
tests/unit/llms/watsonx/rerank/__init__.py
Normal file
0
tests/unit/llms/watsonx/rerank/__init__.py
Normal file
74
tests/unit/llms/watsonx/test_watsonx.py
Normal file
74
tests/unit/llms/watsonx/test_watsonx.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import json
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
import litellm
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tokenizer_config_cached", [False, True], ids=["tokenizer_config", "cached_config_jinja"])
|
||||
async def test_watsonx_text_gpt_oss_async_completion_fetches_hf_template_off_the_event_loop(
|
||||
monkeypatch, tokenizer_config_cached
|
||||
):
|
||||
import httpx
|
||||
|
||||
from litellm._uuid import uuid
|
||||
from litellm.litellm_core_utils.prompt_templates import huggingface_template_handler
|
||||
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
||||
|
||||
hf_model = f"openai/gpt-oss-{uuid.uuid4()}"
|
||||
chat_template = "{% for m in messages %}<|{{ m['role'] }}|>{{ m['content'] }}{% endfor %}"
|
||||
if tokenizer_config_cached:
|
||||
cached_config = {"status": "success", "tokenizer": {"bos_token": None, "eos_token": None}}
|
||||
monkeypatch.setattr(litellm, "known_tokenizer_config", {hf_model: cached_config})
|
||||
expected_fetch = f"https://huggingface.co/{hf_model}/raw/main/chat_template.jinja"
|
||||
else:
|
||||
monkeypatch.setattr(litellm, "known_tokenizer_config", {})
|
||||
expected_fetch = f"https://huggingface.co/{hf_model}/raw/main/tokenizer_config.json"
|
||||
hf_fetched = []
|
||||
captured = {}
|
||||
|
||||
def forbid_sync_client():
|
||||
raise AssertionError("sync HuggingFace fetch ran on the request path")
|
||||
|
||||
async def serve_hf_file(url, **kwargs):
|
||||
hf_fetched.append(url)
|
||||
if url.endswith(".jinja"):
|
||||
return httpx.Response(200, content=chat_template.encode())
|
||||
return httpx.Response(200, json={"chat_template": chat_template, "bos_token": None, "eos_token": None})
|
||||
|
||||
monkeypatch.setattr(huggingface_template_handler, "_get_httpx_client", forbid_sync_client)
|
||||
monkeypatch.setattr(huggingface_template_handler, "get_async_httpx_client", lambda **kwargs: Mock(get=serve_hf_file))
|
||||
|
||||
def handle(request):
|
||||
captured["body"] = json.loads(request.content)
|
||||
return httpx.Response(
|
||||
200,
|
||||
json={
|
||||
"model_id": hf_model,
|
||||
"results": [
|
||||
{
|
||||
"generated_text": "Hi",
|
||||
"generated_token_count": 1,
|
||||
"input_token_count": 1,
|
||||
"stop_reason": "eos_token",
|
||||
}
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
client = AsyncHTTPHandler()
|
||||
client.client = httpx.AsyncClient(transport=httpx.MockTransport(handle))
|
||||
|
||||
response = await litellm.acompletion(
|
||||
model=f"watsonx_text/{hf_model}",
|
||||
messages=[{"role": "user", "content": "Hi there"}],
|
||||
api_base="https://test-api.watsonx.ai",
|
||||
project_id="test-project-id",
|
||||
token="test-token",
|
||||
client=client,
|
||||
)
|
||||
|
||||
assert response.choices[0].message.content == "Hi"
|
||||
assert hf_fetched == [expected_fetch]
|
||||
assert captured["body"]["input"] == "<|user|>Hi there"
|
||||
0
tests/unit/llms/you_com/__init__.py
Normal file
0
tests/unit/llms/you_com/__init__.py
Normal file
Loading…
Add table
Reference in a new issue