How to send video to Gemini API
Quick answer
To send a video to the
Gemini API, encode the video file as base64 or provide a URL, then include it in the messages or inputs parameter depending on the API endpoint. Use the gemini-1.5-flash or gemini-2.0-flash model with the official Google AI Python client, passing the video data as a binary or base64 string in the request payload.PREREQUISITES
Python 3.8+Google Cloud account with Gemini API accesspip install google-ai==0.1.0 or latest Google AI SDKSet environment variable GOOGLE_API_KEY with your API key
Setup
Install the official Google AI Python SDK and set your API key as an environment variable.
- Run
pip install google-aito install the SDK. - Export your API key:
export GOOGLE_API_KEY='your_api_key_here'on Linux/macOS or set it in your environment variables on Windows.
pip install google-ai Step by step
Use the Google AI SDK to send a video file to the Gemini API by reading the video as bytes and encoding it in base64. Then include it in the messages payload with the appropriate content type.
import os
import base64
from google.ai import AIClient
# Initialize client with API key from environment
client = AIClient(api_key=os.environ["GOOGLE_API_KEY"])
# Load video file and encode as base64
video_path = "path/to/video.mp4"
with open(video_path, "rb") as f:
video_bytes = f.read()
video_b64 = base64.b64encode(video_bytes).decode("utf-8")
# Prepare message with video content
messages = [
{
"role": "user",
"content": {
"type": "video_base64",
"data": video_b64,
"mime_type": "video/mp4"
}
}
]
# Call Gemini model
response = client.chat.completions.create(
model="gemini-1.5-flash",
messages=messages
)
print(response.choices[0].message.content) output
Video processed successfully. Response text here...
Common variations
You can also send videos by URL instead of base64 encoding. Use the type field as video_url and provide the url key in the content dictionary.
For asynchronous or streaming use cases, the Google AI SDK supports async calls and streaming completions with similar parameters.
messages_url = [
{
"role": "user",
"content": {
"type": "video_url",
"url": "https://example.com/video.mp4",
"mime_type": "video/mp4"
}
}
]
response_url = client.chat.completions.create(
model="gemini-2.0-flash",
messages=messages_url
)
print(response_url.choices[0].message.content) output
Video URL processed successfully. Response text here...
Troubleshooting
- If you get a
400 Bad Request, verify your video encoding and MIME type. - For
401 Unauthorized, check your API key environment variable. - Large videos may require chunking or using URLs instead of base64.
Key Takeaways
- Use base64 encoding or video URLs to send video content to Gemini API.
- Set your API key securely in environment variables before calling the API.
- Choose the appropriate Gemini model like
gemini-1.5-flashorgemini-2.0-flashfor video input. - Check MIME types and encoding format to avoid request errors.
- For large videos, prefer URLs or chunked uploads over base64 encoding.