mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-09 22:31:41 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
220 lines
7.6 KiB
Python
220 lines
7.6 KiB
Python
import json
|
|
from datetime import datetime
|
|
|
|
|
|
import litellm
|
|
import pytest
|
|
from datetime import timedelta
|
|
from litellm.types.utils import ImageResponse, ImageObject
|
|
from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (
|
|
LiteLLMResponseObjectHandler,
|
|
)
|
|
|
|
|
|
def test_convert_to_image_response_basic():
|
|
# Test basic conversion with minimal input
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [{"url": "http://example.com/image.jpg"}],
|
|
}
|
|
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert isinstance(result, ImageResponse)
|
|
assert result.created == 1234567890
|
|
assert result.data[0].url == "http://example.com/image.jpg"
|
|
|
|
|
|
def test_convert_to_image_response_with_hidden_params():
|
|
# Test with hidden params
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [{"url": "http://example.com/image.jpg"}],
|
|
}
|
|
hidden_params = {"api_key": "test_key"}
|
|
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(
|
|
response_dict, hidden_params=hidden_params
|
|
)
|
|
|
|
assert result._hidden_params == {"api_key": "test_key"}
|
|
|
|
|
|
def test_convert_to_image_response_multiple_images():
|
|
# Test handling multiple images in response
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [
|
|
{"url": "http://example.com/image1.jpg"},
|
|
{"url": "http://example.com/image2.jpg"},
|
|
],
|
|
}
|
|
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert len(result.data) == 2
|
|
assert result.data[0].url == "http://example.com/image1.jpg"
|
|
assert result.data[1].url == "http://example.com/image2.jpg"
|
|
|
|
|
|
def test_convert_to_image_response_with_b64_json():
|
|
# Test handling b64_json in response
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [{"b64_json": "base64encodedstring"}],
|
|
}
|
|
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert result.data[0].b64_json == "base64encodedstring"
|
|
|
|
|
|
def test_convert_to_image_response_with_extra_fields():
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [
|
|
{
|
|
"url": "http://example.com/image1.jpg",
|
|
"content_filter_results": {"category": "violence", "flagged": True},
|
|
},
|
|
{
|
|
"url": "http://example.com/image2.jpg",
|
|
"content_filter_results": {"category": "violence", "flagged": True},
|
|
},
|
|
],
|
|
}
|
|
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert result.data[0].url == "http://example.com/image1.jpg"
|
|
assert result.data[1].url == "http://example.com/image2.jpg"
|
|
|
|
|
|
def test_convert_to_image_response_with_extra_fields_2():
|
|
"""
|
|
Date from a non-OpenAI API could have some obscure field in addition to the expected ones. This should not break the conversion.
|
|
"""
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [
|
|
{
|
|
"url": "http://example.com/image1.jpg",
|
|
"very_obscure_field": "some_value",
|
|
},
|
|
{
|
|
"url": "http://example.com/image2.jpg",
|
|
"very_obscure_field2": "some_other_value",
|
|
},
|
|
],
|
|
}
|
|
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert result.data[0].url == "http://example.com/image1.jpg"
|
|
assert result.data[1].url == "http://example.com/image2.jpg"
|
|
|
|
|
|
def test_convert_to_image_response_with_none_usage_fields():
|
|
"""
|
|
Test handling of None values in usage fields, specifically for gpt-image-1 responses.
|
|
|
|
This test verifies the fix for the bug where gpt-image-1 returns None values
|
|
for usage statistics fields, which caused Pydantic validation errors.
|
|
The fix should clean these None values and let ImageResponse constructor
|
|
handle the default values.
|
|
"""
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [{"b64_json": "base64encodedstring"}],
|
|
"usage": {
|
|
"input_tokens": None, # gpt-image-1 returns None instead of integer
|
|
"input_tokens_details": None, # gpt-image-1 returns None instead of object
|
|
"output_tokens": None, # gpt-image-1 returns None instead of integer
|
|
"total_tokens": None, # gpt-image-1 returns None instead of integer
|
|
},
|
|
}
|
|
|
|
# This should not raise a ValidationError
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert isinstance(result, ImageResponse)
|
|
assert result.created == 1234567890
|
|
assert result.data[0].b64_json == "base64encodedstring"
|
|
|
|
# Usage should be properly initialized with default values
|
|
assert result.usage is not None
|
|
assert result.usage.input_tokens == 0
|
|
assert result.usage.output_tokens == 0
|
|
assert result.usage.total_tokens == 0
|
|
assert result.usage.input_tokens_details is not None
|
|
assert result.usage.input_tokens_details.image_tokens == 0
|
|
assert result.usage.input_tokens_details.text_tokens == 0
|
|
|
|
|
|
def test_convert_to_image_response_with_partial_none_usage_fields():
|
|
"""
|
|
Test handling of mixed None and valid values in usage fields.
|
|
"""
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [{"b64_json": "base64encodedstring"}],
|
|
"usage": {
|
|
"input_tokens": 10, # Valid value
|
|
"input_tokens_details": None, # None value (should be cleaned)
|
|
"output_tokens": None, # None value (should be cleaned)
|
|
"total_tokens": 10, # Valid value
|
|
},
|
|
}
|
|
|
|
# This should not raise a ValidationError
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert isinstance(result, ImageResponse)
|
|
assert result.created == 1234567890
|
|
assert result.data[0].b64_json == "base64encodedstring"
|
|
|
|
# Usage should be properly initialized with defaults where needed
|
|
# Valid values should be preserved, None values should be cleaned and use defaults
|
|
assert result.usage is not None
|
|
assert result.usage.input_tokens == 10 # Valid value should be preserved
|
|
assert result.usage.output_tokens == 0 # None value should become 0
|
|
assert (
|
|
result.usage.total_tokens == 10
|
|
) # Calculated as input_tokens + output_tokens (10 + 0)
|
|
assert result.usage.input_tokens_details is not None
|
|
assert result.usage.input_tokens_details.image_tokens == 0
|
|
assert result.usage.input_tokens_details.text_tokens == 0
|
|
|
|
|
|
def test_convert_to_image_response_with_valid_usage_fields():
|
|
"""
|
|
Test that valid usage fields are preserved correctly.
|
|
"""
|
|
response_dict = {
|
|
"created": 1234567890,
|
|
"data": [{"b64_json": "base64encodedstring"}],
|
|
"usage": {
|
|
"input_tokens": 50,
|
|
"input_tokens_details": {
|
|
"image_tokens": 30,
|
|
"text_tokens": 20,
|
|
},
|
|
"output_tokens": 10,
|
|
"total_tokens": 60,
|
|
},
|
|
}
|
|
|
|
result = LiteLLMResponseObjectHandler.convert_to_image_response(response_dict)
|
|
|
|
assert isinstance(result, ImageResponse)
|
|
assert result.created == 1234567890
|
|
assert result.data[0].b64_json == "base64encodedstring"
|
|
|
|
# Valid usage fields should be preserved
|
|
assert result.usage is not None
|
|
assert result.usage.input_tokens == 50
|
|
assert result.usage.output_tokens == 10
|
|
assert result.usage.total_tokens == 60
|
|
assert result.usage.input_tokens_details is not None
|
|
assert result.usage.input_tokens_details.image_tokens == 30
|
|
assert result.usage.input_tokens_details.text_tokens == 20
|