← Master Index
Vol. 18 Module 18.1 Lecture

Gemini SDK

SDKs

How This Lesson Fits the Module & Volume

Module 18.1 closes the vendor triangle: OpenAI, Anthropic, and now Gemini (Google). You already left Vol. 17’s Automatic1111 / ComfyUI studios for hosted clients. Gemini is the multimodal-native option—text, images, audio, video, and PDF parts in one contents payload—plus Google Cloud / Vertex paths for enterprises.

This page is the last SDK. Module 18.2 starts immediately after: what an API even is, then FastAPI so your app does not import three vendor SDKs in the browser.

Learning Objectives

By the end of this lesson, students should be able to:

  • Authenticate Google’s Gen AI SDK (google-genai) via API key or Vertex ADC.
  • Call generate_content with string or multi-part contents.
  • Read text, usage metadata, and safety / finish signals from the response.
  • Contrast Gemini’s multimodal parts with OpenAI messages and Anthropic blocks.
  • Sketch grounding / Google Search tools vs Vol. 15 function calling.
  • Hand off all three SDKs to a single backend API in Module 18.2.
Definition

Gemini is Google’s multimodal model family. The Gemini SDK here means the official Google Gen AI client (from google import genai / google-genai), which talks to the Gemini Developer API or Vertex AI. You send a model ID plus contents (text and/or inline/file parts). It is not Imagen’s still-image catalog alone (Imagen is a sibling product), and it is not a local Comfy graph.

Three SDKs, One Shipping Job

SDKAuth (typical)Core callSignature strength
OpenAIOPENAI_API_KEYchat.completions / ResponsesEcosystem + Azure-compat
AnthropicANTHROPIC_API_KEYmessages.createLong context, Messages blocks
GeminiGEMINI_API_KEY or Vertex ADCmodels.generate_contentNative multimodal + Google cloud

google-genai Sketch

Package names shifted historically (google-generativeai vs google-genai). Teach the current official client; re-read docs before a spike. Model IDs are SKUs.

import os from pathlib import Path from google import genai from google.genai import types # pip install google-genai # export GEMINI_API_KEY=... (or use Vertex: google.auth default credentials) client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) resp = client.models.generate_content( model="gemini-2.5-flash", # SKU; pin in config contents="In one sentence, why wrap Gemini behind FastAPI?", ) print(resp.text) print(getattr(resp, "usage_metadata", None)) # Multimodal: image + text (not A1111 — this is hosted vision) img_bytes = Path("chair.png").read_bytes() multi = client.models.generate_content( model="gemini-2.5-flash", contents=[ types.Part.from_bytes(data=img_bytes, mime_type="image/png"), "Is this still a product photo or a diffusion artifact? Be brief.", ], ) print(multi.text) # Streaming tokens → Module 18.2 SSE for chunk in client.models.generate_content_stream( model="gemini-2.5-flash", contents="Count to five.", ): if chunk.text: print(chunk.text, end="", flush=True)

Multimodal vs Vol. 16 / 17

Gemini parts

  • Understand image/PDF/audio in-context
  • Great for “what is in this still?”
  • Hosted; ToS + safety filters

Vol. 16 / 17 stills

Module 18.2

  • One POST /v1/chat
  • Provider switch in config
  • Auth, stream, meter, deploy

Reach for Gemini when

  • You need cheap/fast multimodal understanding
  • You already live on GCP / Vertex
  • Grounding with Google Search is a product requirement

Still abstract it

  • Do not leak google.genai types to the SPA
  • Safety ratings ≠ your authz policy
  • Vertex vs AI Studio is a deploy choice, not a new architecture

Related Lectures

LectureWhy
OpenAI / AnthropicSibling SDKs; same adapter job
Vision / ImagenUnderstand vs generate images
Tool callingGemini function / grounding tools
APINext module—your HTTP contract
Common Misconception

“Gemini is just Google’s ChatGPT.” Product UIs differ; the SDK contract is contents + parts, and Vertex vs AI Studio changes auth. Second: feeding a PNG to Gemini is understanding, not running Stable Diffusion. Third: google.generativeai notebook snippets from 2024 may not match google-genai today—read current docs. Fourth: finishing Module 18.1 is not shipping; without your own API, every frontend still holds a vendor key.

Knowledge Check

  1. Short Answer: Name the current official Python import path used in this lecture. Answer: google.genai (package google-genai), via genai.Client.
  2. True/False: Gemini contents can include image bytes plus text. Answer: True.
  3. Multiple Choice: Vertex ADC is primarily: (a) cloud credentials, not a browser cookie, (b) a DDIM sampler, (c) a Redis eviction policy. Answer: (a).
  4. Short Answer: Why is Gemini the last lecture in Module 18.1? Answer: Completes the three-vendor SDK set before Module 18.2 backends.
  5. True/False: Calling Gemini vision replaces Automatic1111 for txt2img. Answer: False—vision understands; generation is Imagen/SD/FLUX/etc.
  6. Multiple Choice: Next page after this lecture: (a) API (18.2), (b) CUDA, (c) t-SNE. Answer: (a).
  7. Short Answer: Where should GEMINI_API_KEY live? Answer: Server env / secret store—not the SPA.
  8. True/False: You should expose google.genai response objects directly to mobile clients. Answer: False—normalize to your API DTO.
  9. Multiple Choice: Gemini’s closest OpenAI analogue for chat is: (a) generate_content, (b) k-means, (c) PNG Info. Answer: (a).
  10. Short Answer: Name one thing Module 18.2 adds that SDKs alone do not. Answer: Any of: your HTTP API, FastAPI, auth, Docker, queues, streaming gateway, monitoring.

Key Takeaways

  • Gemini SDK: google-genai + generate_content + multimodal parts.
  • AI Studio key vs Vertex ADC is auth/deploy, not a new model family.
  • Understand (Gemini) ≠ generate (Imagen / SD / FLUX / A1111).
  • Module 18.1 done: three vendors. Abstract them.
  • Next module: API.
Trainer’s Guide

Lab: Same question to OpenAI, Anthropic, and Gemini. Then send a still (from A1111 or phone) only to Gemini vision. Design a one-page interface: ChatRequest {provider, messages, attachments[]}.

Whiteboard: Arrow all three SDKs into “your API.” Cross out keys in the browser. That box is Module 18.2.

Recap: Gemini completes Module 18.1. Ship through your own HTTP API next: API (Application Programming Interface).