Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

extend-adapter.md

211 lines6.4 KB

1---
2title: Extend Adapter
3id: extend-adapter
4order: 8
5description: "Extend TanStack AI adapter factories with custom model IDs and fine-tuned models while keeping full type safety for input modalities 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​
16The `extendAdapter` utility allows you to extend existing adapter factories (like `openaiText`, `anthropicText`) with custom model names while maintaining full type safety for input modalities and provider options.
17​
18## Basic Usage
19​
20```typescript
21import { createModel, extendAdapter } from '@tanstack/ai'
22import { openaiText } from '@tanstack/ai-openai'
23​
24// Define your custom models using createModel helper
25const myOpenaiModel = createModel('my-fine-tuned-gpt4',['text', 'image']);
26const myOpenaiModelButCooler = createModel('my-fine-tuned-gpt5',['text', 'image']);
28​
29// Create an extended adapter factory - simple API, no type parameters needed
30const myOpenai = extendAdapter(openaiText, [
31 myOpenaiModel,
32 myOpenaiModelButCooler
33])
34​
35// Use with original models - full type inference preserved
36const gpt5Adapter = myOpenai('gpt-5.5')
37​
38// Use with custom models - your custom types are applied
39const customAdapter = myOpenai('my-fine-tuned-gpt4')
40​
41// Works seamlessly with chat()
42import { chat } from '@tanstack/ai'
43​
44const stream = chat({
45 adapter: myOpenai('my-fine-tuned-gpt4'),
46 messages: [{ role: 'user', content: 'Hello!' }]
47})
48```
49​
50## The `createModel` Helper
51​
52The `createModel` function provides a clean way to define custom models with full type inference:
53​
54```typescript
55import { createModel } from '@tanstack/ai'
56​
57// Arguments define name and input modalities
58const model = createModel(
59 'my-model', // model name (literal type inferred)
60 ['text', 'image'] // input modalities (tuple type inferred)
62```
65## Model Definition Structure
66​
67A custom model definition (`ExtendedModelDef`) has the required properties `name`, `input`, and `modelOptions`, plus optional `features` and `tools`. The two `createModel` overloads let you fill these in two ways.
68​
69### Defining Input Modalities
70​
71The positional form takes a model name and an `input` array specifying which content types your model supports:
72​
73```typescript
74import { createModel } from '@tanstack/ai'
75​
76const models = [
77 createModel('text-only-model', ['text']),
78 createModel('multimodal-model', ['text', 'image', 'audio']),
79] as const
80```
81​
82Available modalities: `'text'`, `'image'`, `'audio'`, `'video'`, `'document'`
83​
84### Capabilities-object form
85​
86To attach typed `modelOptions`, declared `features`, or provider `tools` to a custom model, use the second `createModel` overload, which takes a capabilities object as its second argument:
87​
88```typescript
89import { createModel } from '@tanstack/ai'
90import type { OpenAITextProviderOptions } from '@tanstack/ai-openai'
91​
92// Type brand for provider options — the value is unused at runtime.
93const modelOptions: OpenAITextProviderOptions = {}
94​
95const reasoner = createModel('my-reasoner', {
96 input: ['text'],
97 features: ['reasoning', 'structured_outputs'],
98 tools: ['web_search'],
99 modelOptions,
100})
101```
102​
103- `input` — supported input modalities (same as the positional form).
104- `features` — declared feature flags (e.g. `'reasoning'`, `'structured_outputs'`).
105- `tools` — declared provider tools (e.g. `'web_search'`).
106- `modelOptions` — a type brand for the provider options accepted by this model; the value is unused at runtime, so declare an empty object typed as the provider options (e.g. `const modelOptions: OpenAITextProviderOptions = {}`).
108## Preserving Original Factory Behavior
109​
110`extendAdapter` fully preserves the original factory's signature, including any configuration parameters:
111​
112```typescript
113import { createModel, extendAdapter } from '@tanstack/ai'
114import { openaiText } from '@tanstack/ai-openai'
115​
116const customModels = [createModel('my-fine-tuned-gpt4', ['text', 'image'])] as const
117​
118const myOpenai = extendAdapter(openaiText, customModels)
119​
120// Config parameter is preserved
121const adapter = myOpenai('my-fine-tuned-gpt4', {
122 baseURL: 'https://my-proxy.com/v1',
123 timeout: 30000
124})
125```
126​
127## Type Safety
128​
129The extended adapter provides full type safety:
130​
131```typescript ignore
132import { extendAdapter, createModel } from '@tanstack/ai'
133import { openaiText } from '@tanstack/ai-openai'
134​
135const myOpenai = extendAdapter(openaiText, [createModel('custom-model', ['text'])])
136​
137// ✅ Original models work with their original types
138const a1 = myOpenai('gpt-5.5')
139​
140// ✅ Custom models work with your defined types
141const a2 = myOpenai('custom-model')
142​
143// ❌ Type error: invalid model name
144// Note: Type checking works when you assign the result to a variable
145const invalid = myOpenai('nonexistent-model') // TypeScript error!
146```
148​
149## Runtime Behavior
150​
151At runtime, `extendAdapter` simply passes through to the original factory:
152​
153- No validation is performed on custom model names
154- The original factory receives exactly what you pass
155- This allows the original provider's API to handle the model name
156​
157This design is intentional - it allows you to:
158- Use fine-tuned model names that the provider accepts but TypeScript doesn't know about
159- Proxy requests to different backends that accept custom model identifiers
160- Add type safety without runtime overhead
161​
162## Example: OpenAI-Compatible Proxy
163​
164A common use case is typing models for an OpenAI-compatible proxy:
165​
166```typescript
167import { extendAdapter, createModel } from '@tanstack/ai'
168import { openaiText } from '@tanstack/ai-openai'
169​
170// Models available through your proxy
171const proxyModels = [
172 createModel(
173 'llama-3.1-70b',
174 ['text']
175 ),
176 createModel(
177 'mixtral-8x7b',
178 ['text']
179 ),
180] as const
181​
182const proxyAdapter = extendAdapter(openaiText, proxyModels)
183​
184// Use with your proxy's base URL
185const adapter = proxyAdapter('llama-3.1-70b', {
186 baseURL: 'https://my-llm-proxy.com/v1'
187})
188```
189​
190## Example: Fine-tuned Models
191​
192Adding type safety for your fine-tuned models:
193​
194```typescript
195import { chat, createModel, extendAdapter } from '@tanstack/ai'
196import { anthropicText } from '@tanstack/ai-anthropic'
197​
198const fineTunedModels = [
199 createModel(
200 'ft:claude-3-opus:my-org:custom-task:abc123',
201 ['text', 'image']
202 ),
203] as const
204​
205const myAnthropic = extendAdapter(anthropicText, fineTunedModels)
206​
207chat({
208 adapter: myAnthropic('ft:claude-3-opus:my-org:custom-task:abc123'),
209 messages: [{ role: 'user', content: 'Analyze this...' }]
210})
211```