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(gemini): use JSON instead of form-data for image edit requests (#18012)
Cesar Garcia999ffabc39cd
diff --git a/litellm/llms/base_llm/image_edit/transformation.py b/litellm/llms/base_llm/image_edit/transformation.pyindex f3ae2d32eaa..d522675296f 100644--- a/litellm/llms/base_llm/image_edit/transformation.py+++ b/litellm/llms/base_llm/image_edit/transformation.py@@ -109,6 +109,15 @@ class BaseImageEditConfig(ABC):) -> ImageResponse:pass+ def use_multipart_form_data(self) -> bool:+ """+ Return True if the provider uses multipart/form-data for image edit requests.+ Return False if the provider uses JSON requests.++ Default is True for backwards compatibility with OpenAI-style providers.+ """+ return True+def get_error_class(self, error_message: str, status_code: int, headers: Union[dict, httpx.Headers]) -> BaseLLMException:diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.pyindex 4a7789a181f..4b38f542159 100644--- a/litellm/llms/custom_httpx/llm_http_handler.py+++ b/litellm/llms/custom_httpx/llm_http_handler.py@@ -3768,13 +3768,24 @@ class BaseLLMHTTPHandler:)try:- response = sync_httpx_client.post(- url=api_base,- headers=headers,- data=data,- files=files,- timeout=timeout,- )+ # Check if provider uses multipart/form-data or JSON+ if image_edit_provider_config.use_multipart_form_data():+ # Use form-data (OpenAI style)+ response = sync_httpx_client.post(+ url=api_base,+ headers=headers,+ data=data,+ files=files,+ timeout=timeout,+ )+ else:+ # Use JSON (Gemini style)+ response = sync_httpx_client.post(+ url=api_base,+ headers=headers,+ json=data,+ timeout=timeout,+ )except Exception as e:raise self._handle_error(@@ -3853,13 +3864,24 @@ class BaseLLMHTTPHandler:)try:- response = await async_httpx_client.post(- url=api_base,- headers=headers,- data=data,- files=files,- timeout=timeout,- )+ # Check if provider uses multipart/form-data or JSON+ if image_edit_provider_config.use_multipart_form_data():+ # Use form-data (OpenAI style)+ response = await async_httpx_client.post(+ url=api_base,+ headers=headers,+ data=data,+ files=files,+ timeout=timeout,+ )+ else:+ # Use JSON (Gemini style)+ response = await async_httpx_client.post(+ url=api_base,+ headers=headers,+ json=data,+ timeout=timeout,+ )except Exception as e:raise self._handle_error(diff --git a/litellm/llms/gemini/image_edit/transformation.py b/litellm/llms/gemini/image_edit/transformation.pyindex 830c58a0062..78a7ff9546f 100644--- a/litellm/llms/gemini/image_edit/transformation.py+++ b/litellm/llms/gemini/image_edit/transformation.py@@ -63,6 +63,10 @@ class GeminiImageEditConfig(BaseImageEditConfig):headers["Content-Type"] = "application/json"return headers+ def use_multipart_form_data(self) -> bool:+ """Gemini uses JSON requests, not multipart/form-data."""+ return False+def get_complete_url(self,model: str,diff --git a/tests/test_litellm/llms/gemini/image_edit/test_gemini_image_edit_transformation.py b/tests/test_litellm/llms/gemini/image_edit/test_gemini_image_edit_transformation.pyindex 2732bf1595a..021cfaeff5e 100644--- a/tests/test_litellm/llms/gemini/image_edit/test_gemini_image_edit_transformation.py+++ b/tests/test_litellm/llms/gemini/image_edit/test_gemini_image_edit_transformation.py@@ -147,3 +147,14 @@ class TestGeminiImageEditTransformation:headers={},)+ def test_use_multipart_form_data_returns_false(self) -> None:+ """+ Gemini uses JSON requests, not multipart/form-data.+ This is critical because httpx sends data differently:+ - data=dict sends form-encoded+ - json=dict sends JSON++ Without this, Gemini returns: "Invalid JSON payload received. Unexpected token."+ """+ assert self.config.use_multipart_form_data() is False+