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

extendAdapter.md

79 lines1.9 KB

1---
2id: extendAdapter
3title: extendAdapter
4---
5​
6# Function: extendAdapter()
7​
8```ts
9function extendAdapter<TFactory, TDefs>(factory, _customModels): ExtendedFactory<TFactory, TDefs>;
10```
11​
12Defined in: [packages/ai/src/extend-adapter.ts:247](https://github.com/TanStack/ai/blob/main/packages/ai/src/extend-adapter.ts#L247)
13​
14Extends an existing adapter factory with additional custom models.
15​
16The extended adapter accepts both original models (with full original type inference)
17and custom models (with types from your definitions).
18​
19At runtime, this simply passes through to the original factory - no validation is performed.
20The original factory's signature is fully preserved, including any config parameters.
21​
22## Type Parameters
23​
24### TFactory
25​
26`TFactory` *extends* `AnyAdapterFactory`
27​
28### TDefs
29​
30`TDefs` *extends* readonly [`ExtendedModelDef`](../interfaces/ExtendedModelDef.md)\<`string`, readonly [`Modality`](../type-aliases/Modality.md)[], `unknown`, readonly `string`[], readonly `string`[]\>[]
31​
32## Parameters
33​
34### factory
35​
36`TFactory`
37​
38The original adapter factory function (e.g., `openaiText`, `anthropicText`)
39​
40### \_customModels
41​
42`TDefs`
43​
44## Returns
45​
46`ExtendedFactory`\<`TFactory`, `TDefs`\>
47​
48A new factory function that accepts both original and custom models
49​
50## Example
51​
52```typescript
53import { extendAdapter, createModel } from '@tanstack/ai'
54import { openaiText } from '@tanstack/ai-openai'
55​
56// Define custom models
57const customModels = [
58 createModel('my-fine-tuned-gpt4', ['text', 'image']),
59 createModel('local-llama', ['text']),
60] as const
61​
62// Create extended adapter
63const myOpenai = extendAdapter(openaiText, customModels)
64​
65// Use with original models - full type inference preserved
66const gpt4 = myOpenai('gpt-4o')
67​
68// Use with custom models
69const custom = myOpenai('my-fine-tuned-gpt4')
70​
71// Type error: 'invalid-model' is not a valid model
72// myOpenai('invalid-model')
73​
74// Works with chat()
75chat({
76 adapter: myOpenai('my-fine-tuned-gpt4'),
77 messages: [...]
78})
79```