The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: tighten file input handling in image edit endpoints
Yuneng Jiang375bf4d7d67a
diff --git a/litellm/llms/black_forest_labs/image_edit/transformation.py b/litellm/llms/black_forest_labs/image_edit/transformation.pyindex c6d8e8298e3..d05a802d235 100644--- a/litellm/llms/black_forest_labs/image_edit/transformation.py+++ b/litellm/llms/black_forest_labs/image_edit/transformation.py@@ -14,7 +14,9 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Unionimport httpxfrom httpx._types import RequestFiles+import litellmfrom litellm.constants import DEFAULT_MAX_RECURSE_DEPTH+from litellm.litellm_core_utils.url_utils import safe_getfrom litellm.llms.base_llm.image_edit.transformation import BaseImageEditConfigfrom litellm.secret_managers.main import get_secret_strfrom litellm.types.images.main import ImageEditOptionalRequestParams@@ -206,14 +208,14 @@ class BlackForestLabsImageEditConfig(BaseImageEditConfig):)elif isinstance(image, str):if image.startswith(("http://", "https://")):- # Download image from URL- response = httpx.get(image, timeout=60.0)+ response = safe_get(litellm.module_level_client, image, timeout=60.0)response.raise_for_status()return response.contentelse:- # Assume it's a file path- with open(image, "rb") as f:- return f.read()+ raise ValueError(+ f"Unsupported image input: plain string values that are not URLs are not accepted. "+ "Provide image bytes or a file-like object."+ )elif hasattr(image, "read"):# File-like objectpos = getattr(image, "tell", lambda: 0)()diff --git a/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.py b/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.pyindex 7979e0e7901..e35b340f0c0 100644--- a/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.py+++ b/litellm/llms/vertex_ai/image_edit/vertex_imagen_transformation.py@@ -348,13 +348,16 @@ class VertexAIImagenImageEditConfig(BaseImageEditConfig, VertexLLM):if stream_pos is not None:image.seek(stream_pos)return data- if isinstance(image, (str, Path)):- path_obj = Path(image)- if not path_obj.exists():- raise ValueError(- f"Mask/image path does not exist for Vertex AI Imagen image edit: {path_obj}"- )- return path_obj.read_bytes()+ if isinstance(image, str):+ raise ValueError(+ "Unsupported image input: plain string values are not accepted for "+ "Vertex AI Imagen image edit. Provide image bytes or a file-like object."+ )+ if isinstance(image, Path):+ raise ValueError(+ "Unsupported image input: filesystem paths are not accepted for "+ "Vertex AI Imagen image edit. Provide image bytes or a file-like object."+ )if hasattr(image, "read"):data = image.read()if isinstance(data, str):diff --git a/litellm/proxy/image_endpoints/endpoints.py b/litellm/proxy/image_endpoints/endpoints.pyindex 4f994b87f58..fe8b7c6fdc9 100644--- a/litellm/proxy/image_endpoints/endpoints.py+++ b/litellm/proxy/image_endpoints/endpoints.py@@ -285,6 +285,13 @@ async def image_edit_api(if mask_files:data["mask"] = mask_files+ for _field in ("image", "mask"):+ if _field in data and isinstance(data[_field], str):+ raise HTTPException(+ status_code=422,+ detail=f"'{_field}' must be provided as a multipart file upload, not a string.",+ )+# Ensure prompt exists in data (default to None for models that don't require it)if "prompt" not in data:data["prompt"] = None