Open History to see the last commit
1---
2title: Extend Adapter
3id: extend-adapter
4order: 8
5description: "Add your own model names, such as fine-tuned models or proxy models, to an adapter factory. You keep type checks for input types and provider options."
6keywords:
7 - tanstack ai
8 - extendAdapter
9 - custom models
10 - fine-tuned models
11 - createModel
12 - type safety
13 - adapter factory
14---
15
16`extendAdapter` adds your model ids to an existing text adapter. `openaiText('my-fine-tuned-model')` type-checks after you register that id.
17
18The built-in models keep their types, and your models get the types that you declare.
19
20## Quick setup
21
22### 1. Declare your models
23
24Wrap the factory with `extendAdapter`. Describe each model with `createModel`: the name, then the input types it can read.
25
26```typescript
27// lib/ai/my-openai.ts
28import { createModel, extendAdapter } from '@tanstack/ai'
29import { openaiText } from '@tanstack/ai-openai'
30
31export const myOpenai = extendAdapter(openaiText, [
32 createModel('my-fine-tuned-model', ['text', 'image']),
33 createModel('my-other-model', ['text']),
34])
35```
36
37### 2. Use the new factory
38
39Use `myOpenai` where you used `openaiText`:
40
41```typescript
42import { chat } from '@tanstack/ai'
43import { myOpenai } from './lib/ai/my-openai'
44
45const stream = chat({
46 adapter: myOpenai('my-fine-tuned-model'),
47 messages: [{ role: 'user', content: 'Hello!' }],
48 threadId: 'thread-1',
49})
50
51for await (const chunk of stream) {
52 console.log(chunk)
53}
54```
55
56Your model name now autocompletes, and the request goes to the provider with that name.
57
58## Use the built-in models and the factory config
59
60The new factory still takes every model of the original factory. It also takes the same config argument:
61
62```typescript
63import { createModel, extendAdapter } from '@tanstack/ai'
64import { openaiText } from '@tanstack/ai-openai'
65
66const myOpenai = extendAdapter(openaiText, [
67 createModel('my-fine-tuned-model', ['text', 'image']),
68])
69
70// A built-in model, with its normal types
71const builtIn = myOpenai('gpt-6.1-sol')
72
73// Your model, with a config for the client
74const custom = myOpenai('my-fine-tuned-model', {
75 baseURL: 'https://my-proxy.com/v1',
76 timeout: 30000,
77})
78```
79
80## Example: an OpenAI-compatible proxy
81
82Give types to the models that your proxy serves:
83
84```typescript
85import { createModel, extendAdapter } from '@tanstack/ai'
86import { openaiText } from '@tanstack/ai-openai'
87
88const proxyAdapter = extendAdapter(openaiText, [
89 createModel('llama-3.1-70b', ['text']),
90 createModel('mixtral-8x7b', ['text']),
91])
92
93const adapter = proxyAdapter('llama-3.1-70b', {
94 baseURL: 'https://my-llm-proxy.com/v1',
95})
96```
97
98## Example: a fine-tuned model on Anthropic
99
100`extendAdapter` works with every adapter factory, for example `anthropicText`:
101
102```typescript
103import { chat, createModel, extendAdapter } from '@tanstack/ai'
104import { anthropicText } from '@tanstack/ai-anthropic'
105
106const myAnthropic = extendAdapter(anthropicText, [
107 createModel('ft:claude-3-opus:my-org:custom-task:abc123', ['text', 'image']),
108])
109
110const stream = chat({
111 adapter: myAnthropic('ft:claude-3-opus:my-org:custom-task:abc123'),
112 messages: [{ role: 'user', content: 'Analyze this text.' }],
113 threadId: 'thread-1',
114})
115```
116
117## How it works
118
119`extendAdapter` changes only the types. At runtime, it returns the original factory.
120
121- The library does not check your model names. The factory gets exactly what you pass.
122- The API of the provider decides if the model name is valid.
123- There is no runtime cost.
124- A custom model has no reasoning data, so `chat()` does not take a `reasoning` option for it.
125
126## Check what a model accepts
127
128A user attaches an image, but the selected model reads only text. You want to know that before you send the request. Read `inputModalities` on the text adapter:
129
130```typescript
131import { openaiText } from '@tanstack/ai-openai'
132
133const adapter = openaiText('gpt-6.1-sol')
134
135console.log(adapter.inputModalities) // ['image', 'text']
136
137if (!adapter.inputModalities?.includes('image')) {
138 console.log('This model cannot read images. Pick another model.')
139}
140```
141
142- The list comes from the model metadata of the provider package.
143- It is `undefined` when the package does not know the model, for example a custom model from `extendAdapter`. Treat `undefined` as "not known", not as "text only".
144
145Do you write your own adapter? See [Set inputModalities in your own adapter](./extend-adapter-advanced#set-inputmodalities-in-your-own-adapter).
146
147## Advanced
148
149### Input types
150
151The second argument of the positional form lists the input types of the model:
152
153```typescript
154import { createModel } from '@tanstack/ai'
155
156const models = [
157 createModel('text-only-model', ['text']),
158 createModel('multimodal-model', ['text', 'image', 'audio']),
159] as const
160```
161
162The input types are `'text'`, `'image'`, `'audio'`, `'video'`, and `'document'`.
163
164### Capabilities-object form
165
166A model definition (`ExtendedModelDef`) has three required fields: `name`, `input`, and `modelOptions`. It also has two optional fields: `features` and `tools`.
167
168To set the typed `modelOptions`, the `features`, or the provider `tools`, give `createModel` a capabilities object as the second argument:
169
170```typescript
171import { createModel } from '@tanstack/ai'
172import type { OpenAITextProviderOptions } from '@tanstack/ai-openai'
173
174// Type brand for the provider options. The value is not used at runtime.
175const modelOptions: OpenAITextProviderOptions = {}
176
177const reasoner = createModel('my-reasoner', {
178 input: ['text'],
179 features: ['reasoning', 'structured_outputs'],
180 tools: ['web_search'],
181 modelOptions,
182})
183```
184
185- `input`: the input types, the same as in the positional form. If you do not set it, the value is `['text']`.
186- `features`: the feature flags of the model, for example `'reasoning'` or `'structured_outputs'`.
187- `tools`: the provider tools of the model, for example `'web_search'`.
188- `modelOptions`: a type brand for the provider options of the model. The value is not used at runtime. Declare an empty object with the type of the provider options.
189
190### Type errors for unknown names
191
192The new factory rejects a name that is not in either list:
193
194```typescript ignore
195import { createModel, extendAdapter } from '@tanstack/ai'
196import { openaiText } from '@tanstack/ai-openai'
197
198const myOpenai = extendAdapter(openaiText, [createModel('custom-model', ['text'])])
199
200// OK: a built-in model, with its built-in types
201const a1 = myOpenai('gpt-6.1-sol')
202
203// OK: your model, with the types that you declared
204const a2 = myOpenai('custom-model')
205
206// Type error: the name is not known.
207// The check works when you assign the result to a variable.
208const invalid = myOpenai('nonexistent-model')
209```
210
211### Write your own adapter
212
213Do you build a full adapter for a new provider? [Extend Adapter: Advanced](./extend-adapter-advanced) shows how to:
214
215- Set `inputModalities`.
216- Add reasoning levels.
217- Send the prompt cache.
218- Send mid-conversation changes.
219- Send assistant blocks in order.