mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-30 01:52:18 +00:00
fix(azure_ai): encode mid-position reference streams from offset 0
a seekable stream read from its current cursor shipped truncated base64 that azure rejected; read from offset 0 to match how httpx rewinds multipart parts
This commit is contained in:
parent
d0730b51d7
commit
3b99829b3c
2 changed files with 20 additions and 5 deletions
|
|
@ -1,6 +1,5 @@
|
|||
import base64
|
||||
from collections.abc import Mapping, Sequence
|
||||
from io import BufferedReader
|
||||
from types import MappingProxyType
|
||||
from typing import Any, Final
|
||||
|
||||
|
|
@ -123,12 +122,11 @@ class AzureFoundryFlux2ImageEditConfig(OpenAIImageEditConfig):
|
|||
|
||||
def _convert_image_to_base64(self, image: Any) -> str:
|
||||
"""Convert image file to base64 string"""
|
||||
if isinstance(image, BufferedReader):
|
||||
image_bytes = image.read()
|
||||
image.seek(0) # Reset file pointer for potential reuse
|
||||
elif isinstance(image, bytes):
|
||||
if isinstance(image, bytes):
|
||||
image_bytes = image
|
||||
elif hasattr(image, "read"):
|
||||
if image.seekable():
|
||||
image.seek(0)
|
||||
image_bytes = image.read()
|
||||
else:
|
||||
raise ValueError(f"Unsupported image type: {type(image)}")
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import json
|
|||
import struct
|
||||
import zlib
|
||||
from collections.abc import Mapping
|
||||
from io import BytesIO
|
||||
from typing import Final
|
||||
|
||||
import httpx
|
||||
|
|
@ -184,6 +185,22 @@ def test_flux2_image_edit_preserves_controls_and_pixel_cost(dimensions: Mapping[
|
|||
assert response._hidden_params["response_cost"] == pytest.approx(catalog_rate * 2048 * 1024 * 2)
|
||||
|
||||
|
||||
def test_flux2_image_edit_encodes_a_mid_position_stream_from_the_start():
|
||||
stream: Final = BytesIO(b"prefix" + b"image")
|
||||
stream.seek(6)
|
||||
|
||||
request, files = AzureFoundryFlux2ImageEditConfig().transform_image_edit_request(
|
||||
model="FLUX.2-flex",
|
||||
prompt="Blend every reference",
|
||||
image=[stream],
|
||||
image_edit_optional_request_params={},
|
||||
litellm_params={},
|
||||
headers={},
|
||||
)
|
||||
|
||||
assert request["input_image"] == base64.b64encode(b"prefiximage").decode()
|
||||
|
||||
|
||||
def _png_bytes(width: int, height: int) -> bytes:
|
||||
"""Smallest well-formed PNG carrying real IHDR dimensions."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue