Gemini SDK & Multimodal Inputs
From the course Google Gemini API Development
Getting Started with Gemini
Google's Gemini models are natively multimodal, accepting text, images, audio, and video in a single request.
SDK Setup
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Explain how neural networks learn",
});
console.log(response.text);
Multimodal Inputs
Send images alongside text:
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: [
{ text: "What's in this image?" },
{ inlineData: { mimeType: "image/jpeg", data: base64Image } }
],
});
Model Selection
| Model | Best For | Context |
|---|---|---|
| Gemini 2.5 Pro | Complex reasoning, large context | 1M tokens |
| Gemini 2.5 Flash | Balanced speed and quality | 1M tokens |
| Gemini 2.5 Flash Lite | Fast, cost-effective | 1M tokens |
Key Advantages
- Million-token context — process entire codebases or book-length documents
- Native multimodal — text, images, audio, video in one call
- Free tier — generous free usage for development and small projects
- Google ecosystem — integrates with Vertex AI, Cloud, and Search
Common Pitfall: Sending Base64 Images for Large Files
Inline base64 encoding works for images under ~1MB but becomes impractical for larger images or video files. For files over 1MB, use the File API to upload first:
const uploadedFile = await ai.files.upload({
file: fs.createReadStream("large_image.jpg"),
config: { mimeType: "image/jpeg" },
});
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: [
{ text: "Describe this image in detail." },
{ fileData: { fileUri: uploadedFile.uri, mimeType: "image/jpeg" } },
],
});
Uploaded files persist for 48 hours and can be reused across multiple requests, avoiding redundant uploads.
Best Practice: Use System Instructions for Consistent Behavior
System instructions persist across the entire conversation and can significantly reduce prompt length for multi-turn interactions:
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
config: {
systemInstruction: "You are a concise technical writer. Always respond in markdown. Keep answers under 300 words unless specifically asked for more detail.",
},
contents: "Explain REST vs GraphQL",
});
Named Product Reference: Vertex AI for Enterprise Gemini
For enterprise use, access Gemini through Google Cloud Vertex AI rather than the AI Studio API. Vertex provides VPC-SC support (keeps data inside your network perimeter), audit logging, custom IAM roles, and SLA-backed uptime guarantees. The SDK is nearly identical — replace GoogleGenAI with VertexAI from the @google-cloud/vertexai package.
Key Takeaways
- •Gemini models are natively multimodal across text, image, audio, and video
- •Million-token context windows enable processing massive documents
- •Choose between Pro, Flash, and Flash Lite based on complexity needs
- •Google offers a generous free tier for development
Progress is kept in this browser only.