---
title: "Why does the Google Gemini API fail with 400 Invalid Argument when sending base64 image data?"  
description: "Why does the Google Gemini API fail with 400 Invalid Argument when sending base64 image data?"  
author: "Lily Chitlangiya"  
published: 2026-09-22  
canonical: https://answers.mindstick.com/qa/117262/why-does-the-google-gemini-api-fail-with-400-invalid-argument-when-sending-base64-image-data  
category: "Artificial Intelligence"  
tags: ["gemini", "Google Cloud", "Base64", "Python", "api"]  
reading_time: 2 minutes  

---

# Why does the Google Gemini API fail with 400 Invalid Argument when sending base64 image data?

When passing inline base64 image payloads to the [Google Gemini API](https://www.mindstick.com/forum/335/api) using models like `gemini-1.5-flash`, requests often fail with a generic `400 Invalid Argument` error without specifying which parameter was rejected.

## Common causes for invalid argument errors on image inputs

This status code typically occurs due to payload formatting discrepancies rather than authentication issues:

1. **Data URI Prefix inclusion:** Including string prefixes like `data:image/png;base64,` instead of sending raw unadorned base64 string bytes.
2. **Invalid MIME types:** Using informal MIME string aliases such as `image/jpg` instead of the standard `image/jpeg`.

### Formatting image parts correctly

The Python SDK expects a structured dict containing only raw base64 data and a valid media type string.

```python
import google.generativeai as genai

# Helper function to clean raw base64 input strings
def format_image_part(base64_data_str, mime_type="image/jpeg"):
    # Strip Data URI scheme if present in input
    if "," in base64_data_str:
        base64_data_str = base64_data_str.split(",")[1]

    # Construct image object formatted for the Gemini API
    return {
        "mime_type": mime_type,
        "data": base64_data_str
    }

# Configure API credentials
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-1.5-flash')

# Generate content with prompt text and cleaned image payload
image_part = format_image_part("data:image/jpeg;base64,/9j/4AAQSkZJRg...")
response = model.generate_content(["What is shown in this image?", image_part])
```

What are the recommended maximum file size limits for inline data before switching to the Gemini File API for media uploads?


---

Original Source: https://answers.mindstick.com/qa/117262/why-does-the-google-gemini-api-fail-with-400-invalid-argument-when-sending-base64-image-data

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
