Open History to see the last commit
1---
2title: Migration Guide
3id: migration
4order: 1
5description: "Move code from an early TanStack AI 0.x release: split adapter functions, modelOptions, the SSE helpers, embed(), provider tools on /tools, and the { text, chunks } result."
6keywords:
7 - tanstack ai
8 - migration
9 - upgrade
10 - breaking changes
11 - tree-shaking
12 - modelOptions
13 - toServerSentEventsStream
14---
15
16Early 0.x called `openai()`, passed `model` to `chat()`, and used `providerOptions` or `toResponseStream`. Replace those first, then follow [Upgrade to 1.0](./v1).
17
18## What changed
19
20| Old | New | Section |
21| --- | --- | --- |
22| `openai()` plus a `model` option | `openaiText('<model>')` | [1](#1-adapter-functions-split) |
23| `options: { temperature, topP, maxTokens }` and `providerOptions` | `modelOptions`, with the provider's own key names | [2](#2-options-moved-into-modeloptions) |
24| `toResponseStream(stream, { abortController })` | `toServerSentEventsStream(stream, abortController)` or `toServerSentEventsResponse(stream)` | [3](#3-toresponsestream-is-now-toserversenteventsstream) |
25| `embedding()` with `openaiEmbed()` | `embed()` with `openaiEmbedding('<model>')` | [4](#4-embedding-is-now-embed) |
26| `createWebSearchTool` from `@tanstack/ai-openrouter` | `webSearchTool` from `@tanstack/ai-openrouter/tools` | [5](#5-provider-tools-moved-to-tools) |
27| `chat({ stream: false })` resolves to a string | It resolves to `{ text, chunks }` | [6](#6-stream-false-returns-an-object) |
28
29## Upgrade
30
311. Go through the sections below in order. Skip a section if your code does not use the old API.
322. For section 2, run the [sampling codemod](./sampling-options-to-model-options#run-the-codemod). It moves the sampling options into `modelOptions` for you.
333. Run your type checker, and fix the errors that are left.
344. Do [Upgrade to 1.0](./v1).
35
36## 1. Adapter functions split
37
38Each provider exports one function for each activity. The model id goes into that function, so `chat()` has no `model` option.
39
40```typescript ignore
41// Before
42import { chat } from '@tanstack/ai'
43import { openai } from '@tanstack/ai-openai'
44
45const stream = chat({
46 adapter: openai(),
47 model: 'gpt-5.2',
48 messages: [{ role: 'user', content: 'Hello!' }],
49})
50```
51
52```typescript
53// After
54import { chat } from '@tanstack/ai'
55import { openaiText } from '@tanstack/ai-openai'
56
57const stream = chat({
58 adapter: openaiText('gpt-6.1-sol'),
59 messages: [{ role: 'user', content: 'Hello!' }],
60 threadId: 'thread-1',
61})
62```
63
64Import only the functions that you use:
65
66```typescript
67import {
68 openaiText, // Chat/text generation
69 openaiSummarize, // Summarization
70 openaiImage, // Image generation
71 openaiSpeech, // Text-to-speech
72 openaiTranscription, // Audio transcription
73 openaiVideo, // Video generation
74 openaiEmbedding, // Embeddings
75} from '@tanstack/ai-openai'
76```
77
78```typescript
79import {
80 anthropicText, // Chat/text generation
81 anthropicSummarize, // Summarization
82} from '@tanstack/ai-anthropic'
83```
84
85```typescript
86import {
87 geminiText, // Chat/text generation
88 geminiSummarize, // Summarization
89 geminiImage, // Image generation
90 geminiSpeech, // Text-to-speech (experimental)
91} from '@tanstack/ai-gemini'
92```
93
94```typescript
95import {
96 ollamaText, // Chat/text generation
97 ollamaSummarize, // Summarization
98} from '@tanstack/ai-ollama'
99```
100
101If you switch providers at runtime, keep a map of adapter functions:
102
103```typescript ignore
104// Before
105import { chat } from '@tanstack/ai'
106import { openai } from '@tanstack/ai-openai'
107import { anthropic } from '@tanstack/ai-anthropic'
108
109type Provider = 'openai' | 'anthropic'
110
111function getAdapter(provider: Provider) {
112 switch (provider) {
113 case 'openai':
114 return openai()
115 case 'anthropic':
116 return anthropic()
117 }
118}
119
120const stream = chat({
121 adapter: getAdapter(provider),
122 model: provider === 'openai' ? 'gpt-5.2' : 'claude-sonnet-4-5',
123 messages,
124})
125```
126
127```typescript
128// After
129import { chat } from '@tanstack/ai'
130import { openaiText } from '@tanstack/ai-openai'
131import { anthropicText } from '@tanstack/ai-anthropic'
132
133type Provider = 'openai' | 'anthropic'
134
135const messages = [{ role: 'user' as const, content: 'Hello!' }]
136
137const adapters = {
138 openai: () => openaiText('gpt-6.1-sol'),
139 anthropic: () => anthropicText('claude-sonnet-5-5'),
140}
141
142const provider: Provider = 'openai'
143
144const stream = chat({
145 adapter: adapters[provider](),
146 messages,
147 threadId: 'thread-1',
148})
149```
150
151For more on the split, see [Tree-Shaking](../advanced/tree-shaking).
152
153## 2. Options moved into `modelOptions`
154
155This change came in two releases:
156
1571. The `options` object was flattened. `temperature`, `topP`, and `maxTokens` moved to the root of `chat()`, and `providerOptions` was renamed to `modelOptions`.
1582. Later, the root `temperature`, `topP`, and `maxTokens` were removed. They now live in `modelOptions`, under the key name of each provider.
159
160`metadata` stays at the root of `chat()`.
161
162```typescript ignore
163// Before
164const stream = chat({
165 adapter: openai(),
166 model: 'gpt-5.2',
167 messages,
168 options: {
169 temperature: 0.7,
170 maxTokens: 1000,
171 topP: 0.9,
172 },
173 providerOptions: {
174 responseFormat: { type: 'json_object' },
175 logitBias: { '123': 1.0 },
176 },
177})
178```
179
180```typescript
181// After
182import { chat } from '@tanstack/ai'
183import { openaiText } from '@tanstack/ai-openai'
184
185const messages = [{ role: 'user' as const, content: 'Hello!' }]
186
187const stream = chat({
188 adapter: openaiText('gpt-6.1-sol'),
189 messages,
190 threadId: 'thread-1',
191 modelOptions: {
192 temperature: 0.7,
193 max_output_tokens: 1000,
194 top_p: 0.9,
195 },
196})
197```
198
199The keys in `modelOptions` are the keys of the provider API. `openaiText` has no `responseFormat` or `logitBias` key. For typed JSON output, use [`outputSchema`](../structured-outputs/overview).
200
201`modelOptions` is typed from the adapter and the model that you pass. Your editor shows the keys that your model accepts. For the key names of each provider, see [Moving Sampling Options into modelOptions](./sampling-options-to-model-options).
202
203## 3. `toResponseStream` is now `toServerSentEventsStream`
204
205`toServerSentEventsStream` returns a `ReadableStream`, not a `Response`. It takes the `AbortController` as the second argument, not in an options object.
206
207```typescript ignore
208// Before
209import { chat, toResponseStream } from '@tanstack/ai'
210import { openai } from '@tanstack/ai-openai'
211
212export async function POST(request: Request) {
213 const { messages } = await request.json()
214 const abortController = new AbortController()
215
216 const stream = chat({
217 adapter: openai(),
218 model: 'gpt-5.2',
219 messages,
220 abortController,
221 })
222
223 return toResponseStream(stream, { abortController })
224}
225```
226
227```typescript
228// After
229import { chat, toServerSentEventsStream } from '@tanstack/ai'
230import { openaiText } from '@tanstack/ai-openai'
231
232export async function POST(request: Request) {
233 const { messages, threadId } = await request.json()
234 const abortController = new AbortController()
235
236 const stream = chat({
237 adapter: openaiText('gpt-6.1-sol'),
238 messages,
239 threadId,
240 abortController,
241 })
242
243 const readableStream = toServerSentEventsStream(stream, abortController)
244 return new Response(readableStream, {
245 headers: {
246 'Content-Type': 'text/event-stream',
247 'Cache-Control': 'no-cache',
248 Connection: 'keep-alive',
249 },
250 })
251}
252```
253
254If you do not need your own `Response`, return `toServerSentEventsResponse(stream, { abortController })`. It makes the `Response` for you.
255
256For newline-delimited JSON, not SSE, use `toHttpStream`:
257
258```typescript ignore
259import { toHttpStream } from '@tanstack/ai'
260
261const readableStream = toHttpStream(stream, abortController)
262return new Response(readableStream, {
263 headers: {
264 'Content-Type': 'application/x-ndjson',
265 },
266})
267```
268
269## 4. `embedding()` is now `embed()`
270
271The old `embedding()` function was removed. Embeddings came back as a new activity, `embed()`. The old `embedding()` name and the old adapter factories (like `openaiEmbed()`) do not come back.
272
273```typescript ignore
274// Before
275import { embedding } from '@tanstack/ai'
276import { openaiEmbed } from '@tanstack/ai-openai'
277
278const result = await embedding({
279 adapter: openaiEmbed(),
280 model: 'text-embedding-3-small',
281 input: 'Hello, world!',
282})
283```
284
285```typescript
286// After
287import { embed } from '@tanstack/ai'
288import { openaiEmbedding } from '@tanstack/ai-openai'
289
290const result = await embed({
291 adapter: openaiEmbedding('text-embedding-3-small'),
292 input: 'Hello, world!',
293})
294
295console.log(result.embeddings[0]?.vector)
296```
297
298What is different:
299
300- **The model goes into the adapter function**, the same as every other activity.
301- **One function for one input or many.** `input` takes one item or an array. The result always has an `embeddings` array, with one vector for each input item.
302- **Multimodal input.** Models like Cohere embed-v4.0 and Amazon Titan Multimodal take image parts, and text and image together.
303- **`dimensions` is a root option.** Request Matryoshka dimensions with no provider-specific option.
304
305See the [Embeddings guide](../embeddings.md).
306
307## 5. Provider tools moved to `/tools`
308
309Each adapter package exports its provider tools (web search, code execution, computer use, and more) from a `/tools` subpath.
310
311Only `@tanstack/ai-openrouter` has a breaking change. `createWebSearchTool` is gone from the package root. It is now `webSearchTool`, from `@tanstack/ai-openrouter/tools`. The provider tools of Anthropic, OpenAI, and Gemini were new exports, so no import breaks there.
312
313```typescript ignore
314// Before
315import { createWebSearchTool } from '@tanstack/ai-openrouter'
316
317const tools = [
318 createWebSearchTool({ engine: 'native', maxResults: 5 }),
319]
320```
321
322```typescript
323// After
324import { webSearchTool } from '@tanstack/ai-openrouter/tools'
325
326const tools = [
327 webSearchTool({ engine: 'native', maxResults: 5 }),
328]
329```
330
331- **The config and the behavior do not change.** The factory takes the same config object. The tool works the same in `chat({ tools: [...] })`.
332- **The `create` prefix is gone**, to match the other providers (`webSearchTool` in `@tanstack/ai-anthropic/tools`, `@tanstack/ai-openai/tools`, and more).
333- **New: type-level gating.** The `supports.tools` list of each model names the provider tools that it supports. If you pass a provider tool that is not in the list, you get a type error on the `tools` array. Your `toolDefinition()` tools are not affected.
334
335For each provider tool and the models that support it, see [Provider Tools](../tools/provider-tools.md).
336
337## 6. `stream: false` returns an object
338
339`chat({ stream: false })` and `streamToText(stream)` resolve to a `ChatResult` object, not a string. Read `text` from the result.
340
341```diff
342- const text = await chat({ adapter, messages, stream: false })
343+ const { text } = await chat({ adapter, messages, stream: false })
344
345- const streamedText = await streamToText(stream)
346+ const { text: streamedText } = await streamToText(stream)
347```
348
349```typescript
350// After
351import { chat, streamToText } from '@tanstack/ai'
352import { openaiText } from '@tanstack/ai-openai'
353
354const { text } = await chat({
355 adapter: openaiText('gpt-6.1-sol'),
356 messages: [{ role: 'user', content: 'Hello!' }],
357 threadId: 'thread-1',
358 stream: false,
359})
360
361const stream = chat({
362 adapter: openaiText('gpt-6.1-sol'),
363 messages: [{ role: 'user', content: 'Hello!' }],
364 threadId: 'thread-1',
365})
366const { text: streamedText } = await streamToText(stream)
367```
368
369`text` is the same string as before. The new `chunks` array holds every chunk of the run, so you can also read tool calls, usage, and interrupts. See [`chat()` returns](../api/ai.md#returns).
370
371With `outputSchema`, `stream: false` still resolves to the parsed object.
372
373## All changes together
374
375```typescript ignore
376// Before
377import { chat, toResponseStream } from '@tanstack/ai'
378import { openai } from '@tanstack/ai-openai'
379
380export async function POST(request: Request) {
381 const { messages } = await request.json()
382 const abortController = new AbortController()
383
384 const stream = chat({
385 adapter: openai(),
386 model: 'gpt-5.2',
387 messages,
388 options: {
389 temperature: 0.7,
390 maxTokens: 1000,
391 },
392 providerOptions: {
393 responseFormat: { type: 'json_object' },
394 },
395 abortController,
396 })
397
398 return toResponseStream(stream, { abortController })
399}
400```
401
402```typescript
403// After
404import { chat, toServerSentEventsResponse } from '@tanstack/ai'
405import { openaiText } from '@tanstack/ai-openai'
406
407export async function POST(request: Request) {
408 const { messages, threadId } = await request.json()
409 const abortController = new AbortController()
410
411 const stream = chat({
412 adapter: openaiText('gpt-6.1-sol'),
413 messages,
414 threadId,
415 modelOptions: {
416 temperature: 0.7,
417 max_output_tokens: 1000,
418 },
419 abortController,
420 })
421
422 return toServerSentEventsResponse(stream, { abortController })
423}
424```
425
426## Done
427
428Your code has no `openai()`, `model:`, `options:`, `providerOptions`, `toResponseStream`, or `embedding()` call, and your type checker passes. Next, do [Upgrade to 1.0](./v1).