test_image_edit_array_handling

This commit is contained in:
Ishaan Jaff 2025-08-27 09:51:22 -07:00
parent d5440f9614
commit ed52b67fcf

View file

@ -660,117 +660,3 @@ async def test_image_edit_array_handling():
assert mock_post.call_count == 2
@pytest.mark.asyncio
async def test_openai_transformation_handles_multiple_images():
"""Test that OpenAI transformation correctly handles multiple images in request"""
from litellm.llms.openai.image_edit.transformation import OpenAIImageEditConfig
from litellm.types.router import GenericLiteLLMParams
config = OpenAIImageEditConfig()
# Test with multiple images
prompt = "Edit these images"
images = [b"fake_image_1", b"fake_image_2", b"fake_image_3"]
litellm_params = GenericLiteLLMParams(api_key="test_key")
data, files = config.transform_image_edit_request(
model="gpt-image-1",
prompt=prompt,
image=images,
image_edit_optional_request_params={"n": 1},
litellm_params=litellm_params,
headers={}
)
# Check that data contains the prompt and parameters
assert data["prompt"] == prompt
assert data["model"] == "gpt-image-1"
assert data["n"] == 1
# Check that files contains all images with correct field names
assert len(files) == len(images)
for i, file_entry in enumerate(files):
assert file_entry[0] == "image[]" # OpenAI uses image[] for multiple files
assert file_entry[1][1] == images[i] # Image data
assert file_entry[1][2] == "image/png" # Content type
print(f"Successfully processed {len(images)} images in transformation")
@pytest.mark.asyncio
async def test_multiple_image_edit_parameter_validation():
"""Test parameter validation with multiple images"""
from litellm import aimage_edit
# Mock response
mock_response = {
"created": 1589478378,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="
}
]
}
class MockResponse:
def __init__(self, json_data, status_code):
self._json_data = json_data
self.status_code = status_code
self.text = json.dumps(json_data)
def json(self):
return self._json_data
with patch(
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
new_callable=AsyncMock,
) as mock_post:
mock_post.return_value = MockResponse(mock_response, 200)
# Test with valid parameters
result = await aimage_edit(
prompt="Test prompt",
model="gpt-image-1",
image=TEST_IMAGES,
n=1,
size="1024x1024",
response_format="b64_json"
)
ImageResponse.model_validate(result)
# Verify the request was made with correct parameters
mock_post.assert_called_once()
call_args = mock_post.call_args
# Check that the request contains the expected data
if 'data' in call_args.kwargs:
form_data = call_args.kwargs['data']
assert 'model' in form_data
assert 'prompt' in form_data
assert 'n' in form_data
assert form_data['n'] == 1 # Could be int or string depending on implementation print("Parameter validation passed for multiple image edit")
@pytest.mark.asyncio
async def test_multiple_image_edit_error_handling():
"""Test error handling with multiple images"""
from litellm import aimage_edit
# Test with None image (should raise error)
with pytest.raises(Exception):
await aimage_edit(
prompt="Test prompt",
model="gpt-image-1",
image=None,
)
# Test with invalid model (should raise error)
with pytest.raises(Exception):
await aimage_edit(
prompt="Test prompt",
model="invalid-model",
image=TEST_IMAGES,
)
print("Error handling tests passed for multiple image edit")