Docs
Cloudflare
Railway
CodeRabbit
Netlify
WorkOS
AG Grid
Clerk
OpenRouter
SerpAPI
Electric
Unkey
Sentry
Prisma
Cloudflare
Railway
CodeRabbit
Netlify
WorkOS
AG Grid
Clerk
OpenRouter
SerpAPI
Electric
Unkey
Sentry
Prisma
Class References
Function References
Interface References
Type Alias References
Variable References

extendAdapter

Function: extendAdapter()

ts
function extendAdapter<TFactory, TDefs>(factory, _customModels): ExtendedFactory<TFactory, TDefs>;
function extendAdapter<TFactory, TDefs>(factory, _customModels): ExtendedFactory<TFactory, TDefs>;

Defined in: packages/ai/src/extend-adapter.ts:247

Extends an existing adapter factory with additional custom models.

The extended adapter accepts both original models (with full original type inference) and custom models (with types from your definitions).

At runtime, this simply passes through to the original factory - no validation is performed. The original factory's signature is fully preserved, including any config parameters.

Type Parameters

TFactory

TFactory extends AnyAdapterFactory

TDefs

TDefs extends readonly ExtendedModelDef<string, readonly Modality[], unknown, readonly string[], readonly string[]>[]

Parameters

factory

TFactory

The original adapter factory function (e.g., openaiText, anthropicText)

_customModels

TDefs

Returns

ExtendedFactory<TFactory, TDefs>

A new factory function that accepts both original and custom models

Example

typescript
import { extendAdapter, createModel } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'

// Define custom models
const customModels = [
  createModel('my-fine-tuned-gpt4', ['text', 'image']),
  createModel('local-llama', ['text']),
] as const

// Create extended adapter
const myOpenai = extendAdapter(openaiText, customModels)

// Use with original models - full type inference preserved
const gpt4 = myOpenai('gpt-4o')

// Use with custom models
const custom = myOpenai('my-fine-tuned-gpt4')

// Type error: 'invalid-model' is not a valid model
// myOpenai('invalid-model')

// Works with chat()
chat({
  adapter: myOpenai('my-fine-tuned-gpt4'),
  messages: [...]
})
import { extendAdapter, createModel } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'

// Define custom models
const customModels = [
  createModel('my-fine-tuned-gpt4', ['text', 'image']),
  createModel('local-llama', ['text']),
] as const

// Create extended adapter
const myOpenai = extendAdapter(openaiText, customModels)

// Use with original models - full type inference preserved
const gpt4 = myOpenai('gpt-4o')

// Use with custom models
const custom = myOpenai('my-fine-tuned-gpt4')

// Type error: 'invalid-model' is not a valid model
// myOpenai('invalid-model')

// Works with chat()
chat({
  adapter: myOpenai('my-fine-tuned-gpt4'),
  messages: [...]
})