
Python and JavaScript SDKs: A Deep Dive
Master the primary tools for Gemini development. Compare the Python SDK for backend logic and the JavaScript SDK for frontend/Node.js interactions.
Python and JavaScript SDKs
The SDKs are wrappers around the REST API, adding conveniences like Type Hinting, Streaming helpers, and Object mapping.
Python SDK (google.generativeai)
This is the gold standard for backend development, data science, and scripts.
model = genai.GenerativeModel('gemini-1.5-flash')
# 1. Simple
response = model.generate_content("Hi")
# 2. Chat Session (manages history list for you)
chat = model.start_chat(history=[])
r1 = chat.send_message("My name is Sudeep")
r2 = chat.send_message("What is my name?") # It knows 'Sudeep'
JavaScript SDK (@google/generative-ai)
This runs in Node.js AND in the Browser.
- Browser: You can call Gemini directly from a React app. Warning: This exposes your API key to the user. Only do this if you limit the key to your domain in Google Cloud Console.
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash"});
async function run() {
const result = await model.generateContent("Hello Node!");
console.log(result.response.text());
}
Summary
- Python for Backend/Logic.
- JS for Web/Frontend.
- Both have nearly identical feature parity (Stream, Chat, Config).
In the next lesson, we build Embedding Workflows.