When passing inline base64 image payloads to the Google Gemini 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:
- Data URI Prefix inclusion: Including string prefixes like
data:image/png;base64,instead of sending raw unadorned base64 string bytes. - Invalid MIME types: Using informal MIME string aliases such as
image/jpginstead of the standardimage/jpeg.
Formatting image parts correctly
The Python SDK expects a structured dict containing only raw base64 data and a valid media type string.
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?