Open History to see the last commit
1---
2title: Migration Guide
3id: migration
4order: 1
5description: "Migrate existing TanStack AI code to the latest version — adapter function splits, flattened options, renamed modelOptions, and the old embedding() API's replacement by embed()."
6keywords:
7 - tanstack ai
8 - migration
9 - upgrade
10 - breaking changes
11 - tree-shaking
12 - modelOptions
13 - toServerSentEventsStream
14---
15
16This guide helps you migrate from the previous version of TanStack AI to the latest version. The major changes focus on improved tree-shaking, clearer API naming, and simplified configuration.
17
18## Overview of Changes
19
20The main breaking changes in this release are:
21
221. **Adapter functions split** - Adapters are now split into activity-specific functions for optimal tree-shaking
232. **Common options flattened** - Options are now flattened in the config instead of nested
243. **`providerOptions` renamed** - Now called `modelOptions` for clarity
254. **`toResponseStream` renamed** - Now called `toServerSentEventsStream` for clarity
265. **Embeddings removed, then reintroduced** - The old `embedding()` API was removed; embeddings are back as the new `embed()` activity with multimodal support
27
28## 1. Adapter Functions Split
29
30Adapters have been split into activity-specific functions to enable optimal tree-shaking. Instead of importing a monolithic adapter, you now import specific functions for each activity type.
31
32### Before
33
34```typescript ignore
35import { chat } from '@tanstack/ai'
36import { openai } from '@tanstack/ai-openai'
37
38const stream = chat({
39 adapter: openai(),
40 model: 'gpt-5.2',
41 messages: [{ role: 'user', content: 'Hello!' }],
42})
43```
44
45### After
46
47```typescript
48import { chat } from '@tanstack/ai'
49import { openaiText } from '@tanstack/ai-openai'
50
51const stream = chat({
52 adapter: openaiText('gpt-5.2'),
53 messages: [{ role: 'user', content: 'Hello!' }],
54})
55```
56
57### Key Changes
58
59- **Model is passed to adapter factory** - The model name is now passed directly to the adapter function (e.g., `openaiText('gpt-5.2')`)
60- **No separate `model` parameter** - The model is stored on the adapter, so you don't need to pass it separately to `chat()`
61- **Activity-specific imports** - Import only what you need (e.g., `openaiText`, `openaiSummarize`, `openaiImage`)
62
63### All Adapter Functions
64
65Each provider package now exports activity-specific functions:
66
67#### OpenAI
68
69```typescript
70import {
71 openaiText, // Chat/text generation
72 openaiSummarize, // Summarization
73 openaiImage, // Image generation
74 openaiSpeech, // Text-to-speech
75 openaiTranscription, // Audio transcription
76 openaiVideo, // Video generation
77} from '@tanstack/ai-openai'
78```
79
80#### Anthropic
81
82```typescript
83import {
84 anthropicText, // Chat/text generation
85 anthropicSummarize, // Summarization
86} from '@tanstack/ai-anthropic'
87```
88
89#### Gemini
90
91```typescript
92import {
93 geminiText, // Chat/text generation
94 geminiSummarize, // Summarization
95 geminiImage, // Image generation
96 geminiSpeech, // Text-to-speech (experimental)
97} from '@tanstack/ai-gemini'
98```
99
100#### Ollama
101
102```typescript
103import {
104 ollamaText, // Chat/text generation
105 ollamaSummarize, // Summarization
106} from '@tanstack/ai-ollama'
107```
108
109### Migration Example
110
111Here's a complete example of migrating adapter usage:
112
113#### Before
114
115```typescript ignore
116import { chat } from '@tanstack/ai'
117import { openai } from '@tanstack/ai-openai'
118import { anthropic } from '@tanstack/ai-anthropic'
119
120type Provider = 'openai' | 'anthropic'
121
122function getAdapter(provider: Provider) {
123 switch (provider) {
124 case 'openai':
125 return openai()
126 case 'anthropic':
127 return anthropic()
128 }
129}
130
131const stream = chat({
132 adapter: getAdapter(provider),
133 model: provider === 'openai' ? 'gpt-5.2' : 'claude-sonnet-4-5',
134 messages,
135})
136```
137
138#### After
139
140```typescript
141import { chat } from '@tanstack/ai'
142import { openaiText } from '@tanstack/ai-openai'
143import { anthropicText } from '@tanstack/ai-anthropic'
144
145type Provider = 'openai' | 'anthropic'
146
147const messages = [{ role: 'user' as const, content: 'Hello!' }]
148
149const adapters = {
150 openai: () => openaiText('gpt-5.2'),
151 anthropic: () => anthropicText('claude-sonnet-4-5'),
152}
153
154const provider: Provider = 'openai'
155
156const stream = chat({
157 adapter: adapters[provider](),
158 messages,
159})
160```
161
162## 2. Common Options Flattened
163
164Common options that were previously nested in an `options` object are now flattened directly in the config.
165
166### Before
167
168```typescript ignore
169const stream = chat({
170 adapter: openai(),
171 model: 'gpt-5.2',
172 messages,
173 options: {
174 temperature: 0.7,
175 maxTokens: 1000,
176 topP: 0.9,
177 },
178})
179```
180
181### After
182
183```typescript ignore
184const stream = chat({
185 adapter: openaiText('gpt-5.2'),
186 messages,
187 temperature: 0.7,
188 maxTokens: 1000,
189 topP: 0.9,
190})
191```
192
193### Available Options
194
195These options are now available at the top level:
196
197- `temperature` - Controls randomness (0.0 to 2.0)
198- `topP` - Nucleus sampling parameter
199- `maxTokens` - Maximum tokens to generate
200- `metadata` - Additional metadata to attach
201
202> **Heads up — sampling has since moved (breaking).** In a later release, the sampling props (`temperature`, `topP`, `maxTokens`) were removed from the root of `chat()` and now live in provider-native `modelOptions`. Passing them at the root no longer type-checks or takes effect. See [Moving Sampling Options into modelOptions](./sampling-options-to-model-options) for the codemod and provider-native key names. `metadata` stays at the root.
203
204## 3. `providerOptions` → `modelOptions`
205
206The `providerOptions` parameter has been renamed to `modelOptions` for clarity. This parameter contains model-specific options that vary by provider and model.
207
208### Before
209
210```typescript ignore
211const stream = chat({
212 adapter: openai(),
213 model: 'gpt-5.2',
214 messages,
215 providerOptions: {
216 // OpenAI-specific options
217 responseFormat: { type: 'json_object' },
218 logitBias: { '123': 1.0 },
219 },
220})
221```
222
223### After
224
225```typescript ignore
226const stream = chat({
227 adapter: openaiText('gpt-5.2'),
228 messages,
229 modelOptions: {
230 // OpenAI-specific options
231 responseFormat: { type: 'json_object' },
232 logitBias: { '123': 1.0 },
233 },
234})
235```
236
237### Type Safety
238
239`modelOptions` is fully typed based on the adapter and model you're using:
240
241```typescript ignore
242import { openaiText } from '@tanstack/ai-openai'
243
244const adapter = openaiText('gpt-5.2')
245
246// TypeScript knows the exact modelOptions type for gpt-5.2
247const stream = chat({
248 adapter,
249 messages,
250 modelOptions: {
251 // Autocomplete and type checking for gpt-5.2 options
252 responseFormat: { type: 'json_object' },
253 },
254})
255```
256
257## 4. `toResponseStream` → `toServerSentEventsStream`
258
259The `toResponseStream` function has been renamed to `toServerSentEventsStream` to better reflect its purpose. Additionally, the API has changed slightly.
260
261### Before
262
263```typescript ignore
264import { chat, toResponseStream } from '@tanstack/ai'
265import { openai } from '@tanstack/ai-openai'
266
267export async function POST(request: Request) {
268 const { messages } = await request.json()
269 const abortController = new AbortController()
270
271 const stream = chat({
272 adapter: openai(),
273 model: 'gpt-5.2',
274 messages,
275 abortController,
276 })
277
278 return toResponseStream(stream, { abortController })
279}
280```
281
282### After
283
284```typescript
285import { chat, toServerSentEventsStream } from '@tanstack/ai'
286import { openaiText } from '@tanstack/ai-openai'
287
288export async function POST(request: Request) {
289 const { messages } = await request.json()
290 const abortController = new AbortController()
291
292 const stream = chat({
293 adapter: openaiText('gpt-5.2'),
294 messages,
295 abortController,
296 })
297
298 const readableStream = toServerSentEventsStream(stream, abortController)
299 return new Response(readableStream, {
300 headers: {
301 'Content-Type': 'text/event-stream',
302 'Cache-Control': 'no-cache',
303 Connection: 'keep-alive',
304 },
305 })
306}
307```
308
309### Key Changes
310
311- **Function renamed** - `toResponseStream` → `toServerSentEventsStream`
312- **Returns ReadableStream** - Now returns a `ReadableStream` instead of a `Response`
313- **Manual Response creation** - You create the `Response` object yourself with appropriate headers
314- **AbortController parameter** - Passed as a separate parameter instead of in options
315
316### Alternative: HTTP Stream Format
317
318If you need HTTP stream format (newline-delimited JSON) instead of SSE, use `toHttpStream`:
319
320```typescript ignore
321import { toHttpStream } from '@tanstack/ai'
322
323const readableStream = toHttpStream(stream, abortController)
324return new Response(readableStream, {
325 headers: {
326 'Content-Type': 'application/x-ndjson',
327 },
328})
329```
330
331## 5. Embeddings Removed, Then Reintroduced as `embed()`
332
333The original `embedding()` function was removed from TanStack AI. Embeddings have since returned as a new activity with a different API: a single `embed()` function with multimodal input support and per-model type safety. The old `embedding()` name and adapter factories (like `openaiEmbed()`) are not coming back.
334
335### Before
336
337```typescript ignore
338import { embedding } from '@tanstack/ai'
339import { openaiEmbed } from '@tanstack/ai-openai'
340
341const result = await embedding({
342 adapter: openaiEmbed(),
343 model: 'text-embedding-3-small',
344 input: 'Hello, world!',
345})
346```
347
348### After
349
350```typescript
351import { embed } from '@tanstack/ai'
352import { openaiEmbedding } from '@tanstack/ai-openai'
353
354const result = await embed({
355 adapter: openaiEmbedding('text-embedding-3-small'),
356 input: 'Hello, world!',
357})
358
359console.log(result.embeddings[0]?.vector)
360```
361
362Key differences from the old API:
363
364- **Model moves into the adapter factory** - `openaiEmbedding('text-embedding-3-small')` instead of a separate `model` option, matching every other activity
365- **Single function for one or many inputs** - `input` accepts a single item or an array; the result always has an `embeddings` array with one vector per input item
366- **Multimodal inputs** - models like Cohere embed-v4.0 and Amazon Titan Multimodal accept image parts and fused text+image items
367- **Top-level `dimensions`** - request Matryoshka dimensions without provider-specific options
368
369See the [Embeddings guide](../embeddings.md) for full usage.
370
371## 6. Provider Tools Moved to `/tools` Subpath
372
373Provider-specific tools (web search, code execution, computer use, etc.) are now
374exported from a dedicated `/tools` subpath on every adapter package. This keeps
375tool imports tree-shakeable and avoids name collisions between providers.
376
377The only breaking change is in `@tanstack/ai-openrouter`:
378`createWebSearchTool` has been removed from the package root, renamed to
379`webSearchTool`, and moved to `@tanstack/ai-openrouter/tools`. Every other
380provider tool (Anthropic, OpenAI, Gemini) is newly exported — no existing
381import breaks.
382
383### Before
384
385```typescript ignore
386import { createWebSearchTool } from '@tanstack/ai-openrouter'
387
388const tools = [
389 createWebSearchTool({ engine: 'native', maxResults: 5 }),
390]
391```
392
393### After
394
395```typescript
396import { webSearchTool } from '@tanstack/ai-openrouter/tools'
397
398const tools = [
399 webSearchTool({ engine: 'native', maxResults: 5 }),
400]
401```
402
403### Key Changes
404
405- **Import path is now `/tools`** — matches the existing `/adapters` subpath
406 pattern used elsewhere in each provider package.
407- **Factory renamed** — `createWebSearchTool` → `webSearchTool`. The `create*`
408 prefix has been dropped to align with every other provider
409 (`webSearchTool` in `@tanstack/ai-anthropic/tools`,
410 `@tanstack/ai-openai/tools`, etc.).
411- **Runtime behavior is unchanged** — the factory accepts the same config
412 object and returns a tool that works identically in `chat({ tools: [...] })`.
413- **Type-level gating is new** — if you pass a provider tool to a model that
414 doesn't support it (per the model's `supports.tools` array), you now get a
415 type error on the `tools` array. User-defined `toolDefinition()` tools are
416 unaffected.
417
418For the full list of available provider tools and which models support each
419one, see [Provider Tools](../tools/provider-tools.md).
420
421## Complete Migration Example
422
423Here's a complete example showing all the changes together:
424
425### Before
426
427```typescript ignore
428import { chat, toResponseStream } from '@tanstack/ai'
429import { openai } from '@tanstack/ai-openai'
430
431export async function POST(request: Request) {
432 const { messages } = await request.json()
433 const abortController = new AbortController()
434
435 const stream = chat({
436 adapter: openai(),
437 model: 'gpt-5.2',
438 messages,
439 options: {
440 temperature: 0.7,
441 maxTokens: 1000,
442 },
443 providerOptions: {
444 responseFormat: { type: 'json_object' },
445 },
446 abortController,
447 })
448
449 return toResponseStream(stream, { abortController })
450}
451```
452
453### After
454
455```typescript ignore
456import { chat, toServerSentEventsStream } from '@tanstack/ai'
457import { openaiText } from '@tanstack/ai-openai'
458
459export async function POST(request: Request) {
460 const { messages } = await request.json()
461 const abortController = new AbortController()
462
463 const stream = chat({
464 adapter: openaiText('gpt-5.2'),
465 messages,
466 // Sampling now lives in provider-native `modelOptions` (OpenAI Responses
467 // keys: `temperature`, `max_output_tokens`). `metadata` stays at the root.
468 modelOptions: {
469 temperature: 0.7,
470 max_output_tokens: 1000,
471 responseFormat: { type: 'json_object' },
472 },
473 abortController,
474 })
475
476 const readableStream = toServerSentEventsStream(stream, abortController)
477 return new Response(readableStream, {
478 headers: {
479 'Content-Type': 'text/event-stream',
480 'Cache-Control': 'no-cache',
481 Connection: 'keep-alive',
482 },
483 })
484}
485```
486
487## Benefits of These Changes
488
4891. **Better Tree-Shaking** - Import only what you need, resulting in smaller bundle sizes
4902. **Clearer API** - Function names clearly indicate their purpose
4913. **Type Safety** - Model-specific options are fully typed
4924. **Simpler Configuration** - Flattened options are easier to work with
4935. **Focused Scope** - Removed features that are better handled elsewhere
494
495## Need Help?
496
497If you encounter issues during migration:
498
4991. Check the [Tree-Shaking Guide](../advanced/tree-shaking) for details on the new adapter structure
5002. Review the [API Reference](../api/ai) for complete function signatures
5013. Look at the [examples](../getting-started/quick-start) for working code samples
502
503