mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: rename fork-flag to unit-flag now that it applies on every event * test: move tests/test_litellm root and small trees into tests/unit Pure renames, no content changes. Follow-up commits in this PR fix references, merge the three files that already existed in tests/unit, keep live-provider tests in tests/test_litellm and wire CI. * test: carry tests/test_litellm conftest isolation into tests/unit Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS, proxy-URL and keychain env, and session-end client cleanup now reset for unit tests too. The environment isolation owns its MonkeyPatch so a test's own monkeypatch is undone before the model-cost teardown runs. * test: merge, split and prune the moved root and small-tree tests Merge batches/test_batch_utils.py and the chat_completions and messages dispatch tests into the files that already existed in tests/unit. Keep the live Gemini interactions tests, the async image-fetch format test and the OpenAI embedding scorer test in tests/test_litellm since they need real network or keys. Put test_router.py under tests/unit/test_router so the existing package no longer shadows it. Delete eight tests the audit found superseded by stronger ones kept in this move. * ci: run the moved root and small-tree tests under their legacy flags Add the misc and responses-caching-types flags to unit_selection.sh and CircleCI, extend enterprise-routing and mcp-integration, and point the legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest and change classifier at the new paths. * test: make the new tests/unit directories packages tests/unit/test_package_layout.py requires every directory to carry an __init__.py, and without one the moved and retained test_litellm_responses_bridge.py modules collide on import. * test: scope the unit socket block to tests/unit in shared sessions The GHA shards collect the legacy test-path and the unit selection in one pytest session. The unit conftest's loopback-only block leaked into legacy modules that reach the network at import. The legacy conftest now lifts the restriction at collect and setup time, and the unit conftest re-applies it when collecting its own modules. * test: move tests/test_litellm/llms into tests/unit/llms Rename-only. Moves the provider tests and the fine-tuning fixtures they load, mirroring the old paths. Follow-up commits merge, split and wire them. * test: merge, split and prune the moved llms tests Merges the Databricks chat transformation tests into the existing unit file, keeps the tests that need real keys or the network in tests/test_litellm, deletes the audited tests a stronger unit test already covers, and points imports at tests.unit.llms. * ci: run the moved llms tests under their legacy flags The Vertex AI and All Other Providers shards keep their legacy test-path for the retained files and add the llm-vertex-ai and llm-other-providers unit selections. CircleCI gets matching unit jobs. * test: make the tests/unit/llms directories packages Adds __init__.py to the moved dirs and drops the legacy ones whose directories no longer hold tests. * test: drop script runners and path hacks the llms split left dangling The __main__ runners in the split openai_like files and the Databricks e2e runner called tests that now live in the other half of the split or were deleted. The retained legacy halves also no longer need sys.path edits. * test: give the shard-script tests their own GITHUB_OUTPUT They only passed where the runner set it. The CircleCI unit job's env allowlist drops it, so the script's redirect failed there. * test: point the router and module-deletion checks at tests/unit router_code_coverage and code_qa_check_tests only searched tests/test_litellm, so the moved router tests no longer counted. The two silent-experiment tests the audit deleted were the only direct callers of those methods; they are replaced with tests that assert the forwarded shadow request and the recursion guard. * test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path The Databricks e2e file is a manual script whose main() calls the tests that were pruned, so pruning them broke the documented run. It is back to its main version. The SageMaker Nova docstring now points at the file's real location in tests/local_testing. * test: keep the job's UNIT_FLAG out of the shard-script tests --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
342 lines
12 KiB
Python
342 lines
12 KiB
Python
"""
|
|
Tests for DeepInfra rerank functionality following repository patterns.
|
|
"""
|
|
|
|
import asyncio
|
|
import json
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
# Add litellm to path
|
|
import litellm
|
|
|
|
|
|
def assert_response_shape(response, custom_llm_provider):
|
|
"""Helper function to validate response structure."""
|
|
assert hasattr(response, "id")
|
|
assert hasattr(response, "results")
|
|
assert hasattr(response, "meta")
|
|
assert isinstance(response.results, list)
|
|
|
|
for result in response.results:
|
|
assert "index" in result
|
|
assert "relevance_score" in result
|
|
assert isinstance(result["index"], int)
|
|
assert isinstance(result["relevance_score"], (int, float))
|
|
|
|
# Check meta structure
|
|
assert "tokens" in response.meta
|
|
assert "billed_units" in response.meta
|
|
assert "input_tokens" in response.meta["tokens"]
|
|
assert "total_tokens" in response.meta["billed_units"]
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
@patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post")
|
|
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
|
|
def test_basic_rerank_deepinfra(mock_sync_post, mock_async_post, sync_mode):
|
|
"""Test basic DeepInfra rerank functionality."""
|
|
# Mock response data that matches DeepInfra API format
|
|
mock_response_data = {
|
|
"scores": [0.9, 0.1],
|
|
"input_tokens": 25,
|
|
"request_id": "deepinfra-request-123",
|
|
"inference_status": {
|
|
"status": "success",
|
|
"runtime_ms": 150,
|
|
"cost": 0.0001,
|
|
"tokens_generated": 0,
|
|
"tokens_input": 25,
|
|
},
|
|
}
|
|
|
|
def return_val():
|
|
return mock_response_data
|
|
|
|
api_key = "test_deepinfra_api_key"
|
|
api_base = "https://api.deepinfra.com"
|
|
|
|
if sync_mode:
|
|
# Create mock response object for sync
|
|
mock_response = MagicMock()
|
|
mock_response.json = return_val
|
|
mock_response.status_code = 200
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_response.text = json.dumps(mock_response_data)
|
|
mock_sync_post.return_value = mock_response
|
|
|
|
response = litellm.rerank(
|
|
model="deepinfra/Qwen/Qwen3-Reranker-0.6B",
|
|
query="hello",
|
|
documents=["hello", "world"],
|
|
top_n=2,
|
|
custom_llm_provider="deepinfra",
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
)
|
|
mock_sync_post.assert_called_once()
|
|
else:
|
|
# Create mock response object for async
|
|
mock_response = AsyncMock()
|
|
|
|
def return_val():
|
|
return mock_response_data
|
|
|
|
mock_response.json = return_val
|
|
mock_response.status_code = 200
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_response.text = json.dumps(mock_response_data)
|
|
mock_async_post.return_value = mock_response
|
|
|
|
response = asyncio.run(
|
|
litellm.arerank(
|
|
model="deepinfra/Qwen/Qwen3-Reranker-0.6B",
|
|
query="hello",
|
|
documents=["hello", "world"],
|
|
top_n=2,
|
|
custom_llm_provider="deepinfra",
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
)
|
|
)
|
|
mock_async_post.assert_called_once()
|
|
|
|
# Verify response structure
|
|
assert response.id == "deepinfra-request-123"
|
|
assert response.results is not None
|
|
assert len(response.results) == 2
|
|
assert response.results[0]["index"] == 0
|
|
assert response.results[0]["relevance_score"] == 0.9
|
|
assert response.results[1]["index"] == 1
|
|
assert response.results[1]["relevance_score"] == 0.1
|
|
|
|
# Verify metadata
|
|
assert response.meta["tokens"]["input_tokens"] == 25
|
|
assert response.meta["billed_units"]["total_tokens"] == 25
|
|
|
|
# Verify hidden params specific to DeepInfra
|
|
assert response._hidden_params["status"] == "success"
|
|
assert response._hidden_params["runtime_ms"] == 150
|
|
assert response._hidden_params["cost"] == 0.0001
|
|
# Note: The model name is processed and the 'deepinfra/' prefix is removed
|
|
assert response._hidden_params["model"] == "Qwen/Qwen3-Reranker-0.6B"
|
|
|
|
assert_response_shape(response, custom_llm_provider="deepinfra")
|
|
|
|
|
|
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
|
|
def test_deepinfra_rerank_with_queries_param(mock_post):
|
|
"""Test DeepInfra rerank with multiple queries parameter."""
|
|
mock_response_data = {
|
|
"scores": [0.8, 0.6, 0.2],
|
|
"input_tokens": 35,
|
|
"request_id": "deepinfra-multi-query-123",
|
|
"inference_status": {"status": "success", "runtime_ms": 200},
|
|
}
|
|
|
|
def return_val():
|
|
return mock_response_data
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.json = return_val
|
|
mock_response.status_code = 200
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_response.text = json.dumps(mock_response_data)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.rerank(
|
|
model="deepinfra/Qwen/Qwen3-Reranker-4B",
|
|
query="hello",
|
|
documents=["hello", "world", "test"],
|
|
queries=["hello", "hi there"], # DeepInfra specific param
|
|
custom_llm_provider="deepinfra",
|
|
api_key="test_key",
|
|
api_base="https://api.deepinfra.com",
|
|
)
|
|
|
|
mock_post.assert_called_once()
|
|
# Verify that queries parameter was passed in request
|
|
call_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
assert "queries" in call_data
|
|
assert call_data["queries"] == ["hello", "hi there"]
|
|
|
|
assert response.results is not None
|
|
assert len(response.results) == 3
|
|
|
|
|
|
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
|
|
def test_deepinfra_rerank_with_service_tier(mock_post):
|
|
"""Test DeepInfra rerank with service_tier parameter."""
|
|
mock_response_data = {
|
|
"scores": [0.95, 0.75],
|
|
"input_tokens": 30,
|
|
"request_id": "deepinfra-premium-123",
|
|
}
|
|
|
|
def return_val():
|
|
return mock_response_data
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.json = return_val
|
|
mock_response.status_code = 200
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_response.text = json.dumps(mock_response_data)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.rerank(
|
|
model="deepinfra/Qwen/Qwen3-Reranker-8B",
|
|
query="premium search",
|
|
documents=["doc1", "doc2"],
|
|
service_tier="premium", # DeepInfra specific param
|
|
custom_llm_provider="deepinfra",
|
|
api_key="test_key",
|
|
api_base="https://api.deepinfra.com",
|
|
)
|
|
|
|
mock_post.assert_called_once()
|
|
|
|
# Verify URL
|
|
call_url = mock_post.call_args.kwargs["url"]
|
|
assert "api.deepinfra.com/inference/Qwen/Qwen3-Reranker-8B" in call_url
|
|
|
|
# Verify request contains service_tier
|
|
call_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
assert call_data["service_tier"] == "premium"
|
|
|
|
assert response.results is not None
|
|
|
|
|
|
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
|
|
def test_deepinfra_rerank_request_format(mock_post):
|
|
"""Test that the request is properly formatted for DeepInfra API."""
|
|
mock_response_data = {"scores": [0.9, 0.1], "input_tokens": 20}
|
|
|
|
def return_val():
|
|
return mock_response_data
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.json = return_val
|
|
mock_response.status_code = 200
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_response.text = json.dumps(mock_response_data)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.rerank(
|
|
model="deepinfra/Qwen/Qwen3-Reranker-0.6B",
|
|
query="test query",
|
|
documents=["doc1", "doc2"],
|
|
custom_llm_provider="deepinfra",
|
|
api_key="test_key",
|
|
api_base="https://api.deepinfra.com",
|
|
instruction="custom instruction",
|
|
webhook="https://webhook.example.com",
|
|
)
|
|
|
|
mock_post.assert_called_once()
|
|
|
|
# Verify URL format
|
|
call_url = mock_post.call_args.kwargs["url"]
|
|
assert call_url == "https://api.deepinfra.com/inference/Qwen/Qwen3-Reranker-0.6B"
|
|
|
|
# Verify headers
|
|
headers = mock_post.call_args.kwargs["headers"]
|
|
assert headers["Authorization"] == "Bearer test_key"
|
|
assert headers["accept"] == "application/json"
|
|
assert headers["content-type"] == "application/json"
|
|
|
|
# Verify request body format
|
|
request_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
assert request_data["queries"] == [
|
|
"test query",
|
|
"test query",
|
|
] # DeepInfra requires queries to match documents length
|
|
assert request_data["documents"] == ["doc1", "doc2"]
|
|
assert request_data["instruction"] == "custom instruction"
|
|
assert request_data["webhook"] == "https://webhook.example.com"
|
|
|
|
assert response.results is not None
|
|
|
|
|
|
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
|
|
def test_deepinfra_rerank_error_handling(mock_post):
|
|
"""Test DeepInfra rerank error handling."""
|
|
error_response = {"detail": {"error": "Invalid API key"}}
|
|
|
|
def return_val():
|
|
return error_response
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 401
|
|
mock_response.json = return_val
|
|
mock_response.text = json.dumps(error_response)
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_post.return_value = mock_response
|
|
|
|
# The current implementation handles errors gracefully, so we expect a successful response
|
|
# with the error information in the hidden params
|
|
response = litellm.rerank(
|
|
model="deepinfra/Qwen/Qwen3-Reranker-0.6B",
|
|
query="hello",
|
|
documents=["hello", "world"],
|
|
custom_llm_provider="deepinfra",
|
|
api_key="invalid_key",
|
|
api_base="https://api.deepinfra.com",
|
|
)
|
|
|
|
# Verify that the response contains error information
|
|
assert (
|
|
response._hidden_params["status"] == "unknown"
|
|
) # Default status when error occurs
|
|
|
|
|
|
def test_deepinfra_rerank_models():
|
|
"""Test that DeepInfra Qwen rerank models are recognized."""
|
|
# These should not raise errors during model validation
|
|
models = [
|
|
"deepinfra/Qwen/Qwen3-Reranker-0.6B",
|
|
"deepinfra/Qwen/Qwen3-Reranker-4B",
|
|
"deepinfra/Qwen/Qwen3-Reranker-8B",
|
|
]
|
|
|
|
for model in models:
|
|
resolved_model, provider, _, api_base = litellm.get_llm_provider(model=model)
|
|
assert provider == "deepinfra"
|
|
assert resolved_model == model.removeprefix("deepinfra/")
|
|
assert api_base == "https://api.deepinfra.com/v1/openai"
|
|
|
|
|
|
@patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post")
|
|
def test_deepinfra_rerank_minimal_response(mock_post):
|
|
"""Test handling of minimal DeepInfra response."""
|
|
# Minimal response with just scores
|
|
mock_response_data = {"scores": [0.7, 0.3]}
|
|
|
|
def return_val():
|
|
return mock_response_data
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.json = return_val
|
|
mock_response.status_code = 200
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_response.text = json.dumps(mock_response_data)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.rerank(
|
|
model="deepinfra/Qwen/Qwen3-Reranker-0.6B",
|
|
query="hello",
|
|
documents=["hello", "world"],
|
|
custom_llm_provider="deepinfra",
|
|
api_key="test_key",
|
|
api_base="https://api.deepinfra.com",
|
|
)
|
|
|
|
# Should handle minimal response gracefully
|
|
assert response.results is not None
|
|
assert len(response.results) == 2
|
|
assert response.results[0]["relevance_score"] == 0.7
|
|
assert response.results[1]["relevance_score"] == 0.3
|
|
|
|
# Should have default values for missing fields
|
|
assert response.meta["tokens"]["input_tokens"] == 0 # Default when missing
|
|
assert response._hidden_params["status"] == "unknown" # Default when missing
|