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

extendAdapter.md

95 lines2.2 KB

1---
2id: extendAdapter
3title: extendAdapter
4---
5​
6# Function: extendAdapter()
7​
8```ts
9function extendAdapter<TFactory, TDefs>(factory, _customModels): (model, ...args) => InferAdapterReturn<TFactory>;
10```
11​
12Defined in: [packages/typescript/ai/src/extend-adapter.ts:166](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/extend-adapter.ts#L166)
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* (...`args`) => `any`
27​
28### TDefs
29​
30`TDefs` *extends* readonly [`ExtendedModelDef`](../interfaces/ExtendedModelDef.md)\<`string`, readonly [`Modality`](../type-aliases/Modality.md)[], `unknown`\>[]
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​
46A new factory function that accepts both original and custom models
47​
48```ts
49(model, ...args): InferAdapterReturn<TFactory>;
50```
51​
52### Parameters
53​
54#### model
55​
56`InferFactoryModels`\<`TFactory`\> | `ExtractCustomModelNames`\<`TDefs`\>
57​
58#### args
59​
60...`InferConfig`\<`TFactory`\> *extends* `undefined` ? \[\] : \[`InferConfig`\<`TFactory`\>\]
61​
62### Returns
63​
64`InferAdapterReturn`\<`TFactory`\>
65​
66## Example
67​
68```typescript
69import { extendAdapter, createModel } from '@tanstack/ai'
70import { openaiText } from '@tanstack/ai-openai'
71​
72// Define custom models
73const customModels = [
74 createModel('my-fine-tuned-gpt4', ['text', 'image']),
75 createModel('local-llama', ['text']),
76] as const
77​
78// Create extended adapter
79const myOpenai = extendAdapter(openaiText, customModels)
80​
81// Use with original models - full type inference preserved
82const gpt4 = myOpenai('gpt-4o')
83​
84// Use with custom models
85const custom = myOpenai('my-fine-tuned-gpt4')
86​
87// Type error: 'invalid-model' is not a valid model
88// myOpenai('invalid-model')
89​
90// Works with chat()
91chat({
92 adapter: myOpenai('my-fine-tuned-gpt4'),
93 messages: [...]
94})
95```