Open History to see the last commit
1---
2title: Google Gemini
3id: gemini-adapter
4order: 3
5description: "Use Google Gemini in TanStack AI with @tanstack/ai-gemini: multimodal chat with thinking, native image generation, speech, embeddings, video understanding, and Google tools."
6keywords:
7 - tanstack ai
8 - gemini
9 - google gemini
10 - nano banana
11 - image generation
12 - adapter
13 - google ai
14---
15
16The Gemini adapter calls Gemini for multimodal chat, thinking, and Google tools. It also calls image, speech, embedding, music, and video models.
17
18Need Gemini on Vertex AI, for regional endpoints and Google Cloud credentials? Use the [Vertex adapter](./vertex).
19
20## 1. Install
21
22<!-- ::start:tabs variant="package-manager" mode="install" -->
23
24react: @tanstack/ai-gemini
25vue: @tanstack/ai-gemini
26solid: @tanstack/ai-gemini
27svelte: @tanstack/ai-gemini
28preact: @tanstack/ai-gemini
29angular: @tanstack/ai-gemini
30vanilla: @tanstack/ai-gemini
31octane: @tanstack/ai-gemini
32
33<!-- ::end:tabs -->
34
35## 2. Set the API key
36
371. Open [Google AI Studio](https://aistudio.google.com/apikey).
382. Create an API key.
393. Put the key in the environment of your server:
40
41```bash
42GEMINI_API_KEY=your-api-key-here
43```
44
45The short factories (`geminiText`, `geminiImage`, and the others) read `GOOGLE_API_KEY` first, then `GEMINI_API_KEY`. To pass the key in code, use the `create*` factory of the same adapter:
46
47```typescript
48import { createGeminiChat } from "@tanstack/ai-gemini";
49
50const adapter = createGeminiChat("gemini-3.8-flash", process.env.GEMINI_API_KEY!);
51```
52
53## 3. Stream a chat from the server
54
55```typescript
56import {
57 chat,
58 chatParamsFromRequest,
59 toServerSentEventsResponse,
60} from "@tanstack/ai";
61import { geminiText } from "@tanstack/ai-gemini";
62
63export async function POST(request: Request) {
64 const { messages, threadId, runId } = await chatParamsFromRequest(request);
65
66 const stream = chat({
67 adapter: geminiText("gemini-3.8-flash"),
68 messages,
69 threadId,
70 runId,
71 });
72
73 return toServerSentEventsResponse(stream);
74}
75```
76
77`gemini-3.8-flash` takes text, images, audio, video, and documents. It supports thinking, built-in tools, structured output, and caching.
78
79## 4. Render the chat
80
81The client is the same for every provider. Point `useChat` at your route:
82
83```tsx
84import { useState } from "react";
85import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
86
87export function Chat() {
88 const [input, setInput] = useState("");
89 const { messages, sendMessage } = useChat({
90 connection: fetchServerSentEvents("/api/chat"),
91 threadId: "support-chat",
92 });
93
94 return (
95 <form
96 onSubmit={(event) => {
97 event.preventDefault();
98 sendMessage(input);
99 setInput("");
100 }}
101 >
102 {messages.map((message) => (
103 <p key={message.id}>
104 {message.parts
105 .map((part) => (part.type === "text" ? part.content : ""))
106 .join("")}
107 </p>
108 ))}
109 <input value={input} onChange={(event) => setInput(event.target.value)} />
110 </form>
111 );
112}
113```
114
115For Vue, Solid, Svelte, Angular, and the other frameworks, see [Quick Start](../getting-started/quick-start#3-render-the-chat).
116
117You now have a streaming Gemini chat. [Tools](../tools/tools) and [Structured Outputs](../structured-outputs/overview) work the same as with every adapter.
118
119## Images
120
121`geminiImage` takes the Gemini native image models (Nano Banana), such as `gemini-nano-banana-2.1`. Set an aspect ratio and a resolution tier in `size`, for example `"16:9_4K"`. You can edit the last image with a short prompt.
122
123```typescript
124import { generateImage } from "@tanstack/ai";
125import { geminiImage } from "@tanstack/ai-gemini";
126
127const result = await generateImage({
128 adapter: geminiImage("gemini-nano-banana-2.1"),
129 prompt: "A futuristic cityscape at sunset",
130 threadId: "thread-1",
131 numberOfImages: 1,
132 size: "16:9_4K",
133});
134
135console.log(result.images);
136```
137
138The sizes of each model, image edits, and the image options are on [Gemini: Advanced](./gemini-advanced#images). For a full app, see the [media generation example](https://github.com/TanStack/ai/tree/main/examples/ts-react-media).
139
140## Video understanding
141
142Ask questions about a video: "what happens at 0:30?", "summarize the demo", "list every product shown". A video is too large to send inline, so upload it to the Gemini Files API first:
143
144- `uploadGeminiFile()` uploads the file and waits until it is ready.
145- `geminiVideoPart()` turns that upload into a content part for a message.
146
147```typescript
148import { chat, toServerSentEventsResponse } from "@tanstack/ai";
149import { geminiText, uploadGeminiFile, geminiVideoPart } from "@tanstack/ai-gemini";
150
151export async function POST(request: Request) {
152 const file = await uploadGeminiFile("./demo.mp4", { mimeType: "video/mp4" });
153
154 const stream = chat({
155 adapter: geminiText("gemini-3.8-flash"),
156 messages: [
157 {
158 role: "user",
159 content: [
160 { type: "text", content: "Summarize this video and cite timestamps." },
161 geminiVideoPart(file),
162 ],
163 },
164 ],
165 threadId: "thread-1",
166 });
167
168 return toServerSentEventsResponse(stream);
169}
170```
171
172To change the frame rate, cut a clip, or let the model explore the video, see [Video understanding options](./gemini-advanced#video-understanding-options).
173
174## Speech
175
176Gemini speech is experimental.
177
178```typescript
179import { generateSpeech } from "@tanstack/ai";
180import { geminiSpeech } from "@tanstack/ai-gemini";
181
182const result = await generateSpeech({
183 adapter: geminiSpeech("gemini-3.1-flash-tts-preview"),
184 text: "Hello from Gemini TTS!",
185 threadId: "thread-1",
186});
187
188console.log(result.audio); // Base64 encoded audio
189```
190
191For a dialogue with two voices, see [Speech with two speakers](./gemini-advanced#speech-with-two-speakers).
192
193## Embeddings
194
195```typescript
196import { embed } from "@tanstack/ai";
197import { geminiEmbedding } from "@tanstack/ai-gemini";
198
199const result = await embed({
200 adapter: geminiEmbedding("gemini-embedding-001"),
201 input: "a red guitar",
202 dimensions: 1536,
203 modelOptions: {
204 taskType: "RETRIEVAL_DOCUMENT",
205 },
206});
207
208console.log(result.embeddings[0]?.vector);
209```
210
211- The embedding endpoint of the Gemini API does not report token usage, so `result.usage` is absent.
212- This adapter does not support `multimodalembedding@001`, which is a Vertex-only model.
213
214See the [Embeddings guide](../embeddings.md).
215
216## Summarization
217
218```typescript
219import { summarize } from "@tanstack/ai";
220import { geminiSummarize } from "@tanstack/ai-gemini";
221
222const result = await summarize({
223 adapter: geminiSummarize("gemini-3.1-pro-preview"),
224 text: "Your long text to summarize...",
225 threadId: "thread-1",
226 maxLength: 100,
227 style: "concise", // "concise" | "bullet-points" | "paragraph"
228});
229
230console.log(result.summary);
231```
232
233## Music, video, voice, and files
234
235- **Music**: `geminiAudio` makes music with the Lyria models. Experimental. See [Audio Generation](../media/audio-generation).
236- **Video**: `geminiVideo` makes videos with Veo and Gemini Omni. Experimental. See [Video Generation](../media/video-generation).
237- **Realtime voice**: `geminiRealtime` and `geminiRealtimeToken` run a live voice chat. See [Realtime Voice Chat](../media/realtime-chat).
238- **Files**: `geminiFiles` uploads media to the Gemini Files API. See [Files API](../advanced/files-api).
239
240## Provider tools
241
242Gemini runs some tools itself, such as Google Search and a code sandbox. Import them from `@tanstack/ai-gemini/tools` and add them to `tools`:
243
244```typescript
245import { chat } from "@tanstack/ai";
246import { geminiText } from "@tanstack/ai-gemini";
247import { googleSearchTool } from "@tanstack/ai-gemini/tools";
248
249const stream = chat({
250 adapter: geminiText("gemini-3.8-flash"),
251 messages: [{ role: "user", content: "What's the weather in Tokyo right now?" }],
252 threadId: "thread-1",
253 tools: [googleSearchTool()],
254});
255```
256
257Tools that search or read:
258
259- `googleSearchTool` and `googleSearchRetrievalTool`: search Google.
260- `googleMapsTool`: find places and directions.
261- `urlContextTool`: read the URLs in the conversation.
262- `fileSearchTool`: search files in Gemini file search stores.
263
264Tools that act:
265
266- `codeExecutionTool`: run Python in a sandbox.
267- `computerUseTool`: control a virtual desktop.
268
269Each tool, with a full sample, is on [Gemini: Advanced](./gemini-advanced#provider-tools). For which models take which tool, see [Provider Tools](../tools/provider-tools.md).
270
271## Models and options
272
273### Model options
274
275Provider options go in `modelOptions`, with the camelCase names of the Gemini SDK. This includes the sampling options `temperature`, `topP`, and `maxOutputTokens`:
276
277```typescript
278import { chat } from "@tanstack/ai";
279import { geminiText } from "@tanstack/ai-gemini";
280
281const stream = chat({
282 adapter: geminiText("gemini-3.8-flash"),
283 messages: [{ role: "user", content: "Hello!" }],
284 threadId: "thread-1",
285 modelOptions: {
286 maxOutputTokens: 2048,
287 temperature: 0.7,
288 topP: 0.9,
289 topK: 40,
290 stopSequences: ["END"],
291 },
292});
293```
294
295- For JSON output, set `responseMimeType: "application/json"`. For typed output, use [Structured Outputs](../structured-outputs/overview).
296- Sampling options do not go at the root of `chat()`. See [Moving Sampling Options into modelOptions](../migration/sampling-options-to-model-options).
297
298### Reasoning
299
300Want Gemini to think harder? Set `reasoning` on `chat()`:
301
302```typescript
303import { chat } from "@tanstack/ai";
304import { geminiText } from "@tanstack/ai-gemini";
305
306const stream = chat({
307 adapter: geminiText("gemini-3.8-flash"),
308 messages: [{ role: "user", content: "Plan a database migration." }],
309 threadId: "thread-1",
310 reasoning: "high",
311});
312```
313
314What goes to Gemini:
315
316- Gemini 3 models: `thinkingConfig.thinkingLevel`.
317- Gemini 2.5 models: `thinkingConfig.thinkingBudget`. Set it with `budgetTokens`, or the level picks it. `off` sends a budget of `0`.
318- `summary` sets `thinkingConfig.includeThoughts`, so the thinking text streams.
319- `geminiTextInteractions`: `generation_config.thinking_level` and `thinking_summaries`.
320
321The levels of each model:
322
323| Model | Levels | `budgetTokens` |
324| --- | --- | --- |
325| `gemini-3.8-flash`, `gemini-3.7-flash`, `gemini-3.1-pro-preview` | `low`, `medium`, `high` | no |
326| `gemini-3.6-flash`, `gemini-3.5-flash`, `gemini-3.5-flash-lite`, `gemini-3.1-flash-lite`, `gemini-3.1-flash-lite-preview`, `gemini-3-flash-preview` | `minimal` to `high` | no |
327| `gemini-2.5-pro` | `minimal` to `high` | yes |
328| `gemini-2.5-flash`, `gemini-2.5-flash-lite` | `off`, `high` | yes |
329
330A Gemini 2.5 budget:
331
332```typescript
333import { chat } from "@tanstack/ai";
334import { geminiText } from "@tanstack/ai-gemini";
335
336const stream = chat({
337 adapter: geminiText("gemini-2.5-pro"),
338 messages: [{ role: "user", content: "Check this proof." }],
339 threadId: "thread-1",
340 reasoning: { level: "high", budgetTokens: 8000 },
341});
342```
343
344Image models keep their own `thinkingConfig` in `modelOptions`. See [Image model options](./gemini-advanced#image-model-options).
345
346### Models
347
348Newest first. The package exports the full lists, for example `GeminiTextModels`.
349
350| Activity | Factory | Models |
351| --- | --- | --- |
352| Chat | `geminiText` | `gemini-3.8-flash`, `gemini-3.7-flash`, `gemini-3.6-flash`, `gemini-3.5-flash`, `gemini-3.5-flash-lite`, `gemini-3.1-pro-preview`, `gemini-3-flash-preview`, `gemini-3.1-flash-lite`, `gemini-3.1-flash-lite-preview`, `gemini-2.5-pro`, `gemini-2.5-flash`, `gemini-2.5-flash-lite` |
353| Images | `geminiImage` | `gemini-nano-banana-2.1`, `gemini-3.1-flash-image`, `gemini-3.1-flash-lite-image`, `gemini-3-pro-image` |
354| Speech | `geminiSpeech` | `gemini-3.1-flash-tts-preview`, `gemini-2.5-flash-preview-tts`, `gemini-2.5-pro-preview-tts` |
355| Music | `geminiAudio` | `lyria-3-pro-preview`, `lyria-3-clip-preview` |
356| Video | `geminiVideo` | `veo-3.1-generate-preview`, `veo-3.1-fast-generate-preview`, `veo-3.1-lite-generate-preview`, `gemini-omni-1.1-flash` |
357| Embeddings | `geminiEmbedding` | `gemini-embedding-001` |
358
359`adapter.inputModalities` lists the input kinds of the selected model. See [Check what a model accepts](../advanced/extend-adapter#check-what-a-model-accepts).
360
361## Advanced
362
363These topics are on [Gemini: Advanced](./gemini-advanced):
364
365- Configuration and a proxy or gateway.
366- Stateful conversations with the Interactions API (experimental).
367- Image sizes, image edits, and image options.
368- Video understanding options and speech with two speakers.
369- Every provider tool, with a full sample, and the API reference.