feat: add defaultModelOptions to every adapter #2254
main fromfeat/default-model-options-all-adaptersmerged by tombeckenham View on GitHubApplied a fix from CIc88f9f6
- 2 bot comments
- @@ -11,8 +11,14 @@1111 > extends BaseTranscriptionAdapter<TModel> {1212 readonly name = 'cartesia' as const1313 private readonly client: Cartesia14+ readonly defaultModelOptions: Record<string, unknown> | undefined14151516 /** Configure the model and API connection for transcription. */16- constructor(model: TModel, config?: CartesiaConfig) {17+ constructor(18+ model: TModel,19+ config: CartesiaAdapterConfig<Record<string, unknown>> = {},20+ ) {1721 super(model)18- this.client = createCartesiaClient(config)22+ const { defaultModelOptions, ...clientConfig } = config23+ this.client = createCartesiaClient(clientConfig)24+ this.defaultModelOptions = defaultModelOptions@@ -54,9 +54,14 @@5454 override readonly kind = 'image' as const5555 readonly name = 'fal' as const5656 private readonly client: FalClient57+ readonly defaultModelOptions: FalImageProviderOptions<TModel> | undefined5758 // Consumes fal storage URLs uploaded via falFiles().5859 override readonly supportsFileSources = true596060- constructor(model: TModel, config?: FalClientConfig) {61+ constructor(62+ model: TModel,63+ config?: FalAdapterConfig<FalImageProviderOptions<TModel>>,64+ ) {6165 super(model, {})6266 this.client = createAdapterFalClient(config)67+ this.defaultModelOptions = config?.defaultModelOptions@@ -232,6 +239,11 @@232239 // single user message — anything else is a follow-up). Errors evict233240 // immediately so a failed turn never chains into the next one.234241 private readonly interactionIdByThread = new Map<string, string>()242+ readonly defaultModelOptions: ResolveProviderOptions | undefined235243236- constructor(config: GeminiTextInteractionsConfig, model: TModel) {244+ constructor(245+ { defaultModelOptions, ...config }: GeminiTextInteractionsConfig,246+ model: TModel,247+ ) {237248 super({}, model)249+ this.defaultModelOptions = defaultModelOptions@@ -96,3 +96,13 @@9696 return {}9797 }989899+/** Configuration for the Ollama text adapter. */100+export interface OllamaTextConfig<101+ TModel extends string = string,102+> extends OllamaClientConfig {103+ /**104+ * Default `modelOptions` for every call. The activity merges them under105+ * the call's `modelOptions`. A key the call sets replaces the default key106+ * in full.107+ */108+ defaultModelOptions?: ResolveModelOptions<TModel>@@ -48,11 +58,14 @@4858 private readonly baseUrl: string4959 private readonly headers: Record<string, string>5060 private readonly fetchFn: typeof fetch | undefined61+ readonly defaultModelOptions: Record<string, unknown> | undefined516252- constructor(config: OllayaClientConfig, model: TModel) {63+ constructor(config: OllayaEvaluateConfig, model: TModel) {5364 super({}, model)54- this.apiKey = config.apiKey55- const transport = resolveOllayaTransport(config)65+ const { defaultModelOptions, ...clientConfig } = config66+ this.apiKey = clientConfig.apiKey67+ const transport = resolveOllayaTransport(clientConfig)5668 this.baseUrl = transport.baseUrl5769 this.headers = transport.headers58- this.fetchFn = config.fetch70+ this.fetchFn = clientConfig.fetch71+ this.defaultModelOptions = defaultModelOptions@@ -83,3 +83,5 @@8383 }8484 export type OpenRouterTextModels = (typeof OPENROUTER_CHAT_MODELS)[number]858586+/** `OpenRouterConfig` plus the text adapter's own options. */87+export type OpenRouterTextConfig<TModel extends string> = OpenRouterConfig & {@@ -39,8 +49,11 @@3949 readonly name = 'reactor' as const4050 private readonly clientConfig: ReactorClientConfig4151 private readonly apiKey: string52+ readonly defaultModelOptions: ReactorWorldProviderOptions | undefined425343- constructor(model: TModel, config: ReactorClientConfig = {}) {54+ constructor(model: TModel, config: ReactorWorldConfig = {}) {4455 super(model, {})45- this.apiKey = resolveReactorApiKey(config)46- this.clientConfig = config56+ const { defaultModelOptions, ...clientConfig } = config57+ this.apiKey = resolveReactorApiKey(clientConfig)58+ this.clientConfig = clientConfig59+ this.defaultModelOptions = defaultModelOptions@@ -50,11 +60,14 @@5060 private readonly baseUrl: string5161 private readonly headers: Record<string, string>5262 private readonly fetchFn: typeof fetch | undefined63+ readonly defaultModelOptions: Record<string, unknown> | undefined536454- constructor(config: TypesafeClientConfig, model: TModel) {65+ constructor(config: TypesafeEvaluateConfig, model: TModel) {5566 super({}, model)56- this.apiKey = config.apiKey57- const transport = resolveTypesafeTransport(config)67+ const { defaultModelOptions, ...clientConfig } = config68+ this.apiKey = clientConfig.apiKey69+ const transport = resolveTypesafeTransport(clientConfig)5870 this.baseUrl = transport.baseUrl5971 this.headers = transport.headers60- this.fetchFn = config.fetch72+ this.fetchFn = clientConfig.fetch73+ this.defaultModelOptions = defaultModelOptions@@ -66,13 +40,18 @@66406741 export function vertexText<TModel extends GeminiTextModel>(6842 model: TModel,69- config: VertexClientConfig = {},43+ config: VertexAdapterConfig<GeminiTextConfig<TModel>> = {},7044 ): GeminiTextAdapter<TModel> {71- return createVertex(config).text(model)45+ const { defaultModelOptions, ...client } = config46+ return new GeminiTextAdapter(47+ { ...resolveVertexGeminiOptions(client), defaultModelOptions },48+ model,49+ )7250 }73517452 export function vertexSummarize<TModel extends GeminiTextModel>(7553 model: TModel,7654 config: VertexClientConfig = {},7755 ): ChatStreamSummarizeAdapter<TModel> {78- return createVertex(config).summarize(model)56+ return new ChatStreamSummarizeAdapter(57+ new GeminiTextAdapter(resolveVertexGeminiOptions(config), model),
tanstack-source reviewed
@@ -66,13 +39,18 @@39+> = VertexClientConfig & Pick<TAdapterConfig, 'defaultModelOptions'>66406741 export function vertexText<TModel extends GeminiTextModel>(6842 model: TModel,69- config: VertexClientConfig = {},43+ config: VertexAdapterConfig<GeminiTextConfig<TModel>> = {},7044 ): GeminiTextAdapter<TModel> {71- return createVertex(config).text(model)45+ const { defaultModelOptions, ...client } = config46+ return new GeminiTextAdapter(47+ { ...resolveVertexGeminiOptions(client), defaultModelOptions },48+ model,49+ )7250 }73517452 export function vertexSummarize<TModel extends GeminiTextModel>(7553 model: TModel,7654 config: VertexClientConfig = {},7755 ): ChatStreamSummarizeAdapter<TModel> {78- return createVertex(config).summarize(model)56+ return new ChatStreamSummarizeAdapter(@@ -91 +96 @@9196 apiKey: 'vertex',@@ -0,0 +17,21 @@17+'@tanstack/ai-groq': minor18+'@tanstack/ai-llmgateway': minor19+'@tanstack/ai-lovable': minor20+'@tanstack/ai-meshy': minor21+'@tanstack/ai-mistral': minor22+'@tanstack/ai-muse': minor23+'@tanstack/ai-neon': minor24+'@tanstack/ai-ollama': minor25+'@tanstack/ai-ollaya': minor26+'@tanstack/ai-openai': minor27+'@tanstack/ai-opencode': minor28+'@tanstack/ai-openrouter': minor29+'@tanstack/ai-reactor': minor30+'@tanstack/ai-typesafe': minor31+'@tanstack/ai-uthana': minor32+'@tanstack/ai-vercel-gateway': minor33+'@tanstack/ai-vertex': minor34+'@tanstack/ai-worldlabs': minor35+'@tanstack/openai-base': minor36+---37+
AlemTuzlak pushed59acabd
tanstack-source reviewed
@@ -11,8 +11,14 @@1111 > extends BaseTranscriptionAdapter<TModel> {1212 readonly name = 'cartesia' as const1313 private readonly client: Cartesia14+ readonly defaultModelOptions: Record<string, unknown> | undefined14151516 /** Configure the model and API connection for transcription. */16- constructor(model: TModel, config?: CartesiaConfig) {17+ constructor(18+ model: TModel,19+ config: CartesiaAdapterConfig<Record<string, unknown>> = {},20+ ) {1721 super(model)18- this.client = createCartesiaClient(config)22+ const { defaultModelOptions, ...clientConfig } = config23+ this.client = createCartesiaClient(clientConfig)24+ this.defaultModelOptions = defaultModelOptions@@ -54,9 +54,14 @@5454 override readonly kind = 'image' as const5555 readonly name = 'fal' as const5656 private readonly client: FalClient57+ readonly defaultModelOptions: FalImageProviderOptions<TModel> | undefined5758 // Consumes fal storage URLs uploaded via falFiles().5859 override readonly supportsFileSources = true596060- constructor(model: TModel, config?: FalClientConfig) {61+ constructor(62+ model: TModel,63+ config?: FalAdapterConfig<FalImageProviderOptions<TModel>>,64+ ) {6165 super(model, {})6266 this.client = createAdapterFalClient(config)67+ this.defaultModelOptions = config?.defaultModelOptions@@ -232,5 +239,7 @@232239 // single user message — anything else is a follow-up). Errors evict233240 // immediately so a failed turn never chains into the next one.234241 private readonly interactionIdByThread = new Map<string, string>()242+ readonly defaultModelOptions: ResolveProviderOptions | undefined235243236- constructor(config: GeminiTextInteractionsConfig, model: TModel) {244+ constructor(245+ { defaultModelOptions, ...config }: GeminiTextInteractionsConfig,@@ -96,3 +96,13 @@9696 return {}9797 }989899+/** Configuration for the Ollama text adapter. */100+export interface OllamaTextConfig<101+ TModel extends string = string,102+> extends OllamaClientConfig {103+ /**104+ * Default `modelOptions` for every call. The activity merges them under105+ * the call's `modelOptions`. A key the call sets replaces the default key106+ * in full.107+ */108+ defaultModelOptions?: ResolveModelOptions<TModel>
tombeckenham pushedcf1cc38
tanstack-source reviewed
@@ -91,4 +96,5 @@9196 apiKey: 'vertex',9297 baseURL,9398 defaultHeaders: config.defaultHeaders,99+ defaultModelOptions: config.defaultModelOptions,94100 fetch: async (input, init) => {@@ -0,0 +24,21 @@24+ return mocks.clientOptionsSpy.mock.calls[0]?.[0]25+}26+27+describe('Grok defaultModelOptions', () => {28+ beforeEach(() => {29+ vi.clearAllMocks()30+ })31+32+ it('text adapter stores the defaults and keeps them out of the SDK client', () => {33+ const adapter = createGrokText('grok-4.3', 'test-key', {34+ defaultModelOptions: { temperature: 0.2 },35+ })36+37+ expect(adapter.defaultModelOptions).toEqual({ temperature: 0.2 })38+ expect(sdkClientOptions()).toEqual({39+ apiKey: 'test-key',40+ baseURL: 'https://api.x.ai/v1',41+ })42+ })43+44+ it('vertex text adapter stores the defaults and keeps them out of the SDK client', () => {- @@ -101,3 +101,17 @@101101 }102102 ```103103104+## Keep model options when a route sets its own105+106+The spread replaces the whole `modelOptions` object. If a route sets `modelOptions`, the shared `modelOptions` are gone.107+108+To keep a model option on every call, put it on the adapter with `defaultModelOptions`. Every text adapter takes this option:109+110+```typescript111+// lib/ai/chat-options.ts112+import { createChatOptions } from '@tanstack/ai'113+import { openaiText } from '@tanstack/ai-openai'114+115+export const supportChatOptions = createChatOptions({116+ adapter: openaiText('gpt-6.1-sol', {117+ defaultModelOptions: { verbosity: 'low' },@@ -103,0 +129,21 @@129+const stream = chat({130+ ...supportChatOptions,131+ messages: [{ role: 'user', content: 'Where is my order?' }],132+ threadId: 'thread-1',133+ modelOptions: { max_output_tokens: 500 },134+})135+```136+137+A key in the `chat()` call's `modelOptions` replaces the same key in `defaultModelOptions`. A call key set to `undefined` keeps the default.138+139+`chat()` merges one level deep. A nested key, like `text`, is replaced in full.140+141+A middleware sees the merged value in `onConfig`. If it returns `modelOptions`, that object replaces the merged one in full. Spread `config.modelOptions` to keep the defaults and the call's keys:142+143+```ts144+import type { ChatMiddleware } from '@tanstack/ai'145+146+const lowTemperature: ChatMiddleware = {147+ name: 'low-temperature',148+ onConfig: (_ctx, config) => ({149+ modelOptions: { ...config.modelOptions, temperature: 0 },@@ -181,13 +188,18 @@188+ maxTokensKey: string | undefined,189+ defaults: Record<string, unknown> | undefined,181190 ): Record<string, unknown> {182191 const merged: Record<string, unknown> = { ...modelOptions }192+ // ponytail: value equality, so a call that repeats the default limit and193+ // also passes maxLength gets maxLength. Both are explicit, so either is fine.194+ const callerSet = (k: string) =>195+ typeof merged[k] === 'number' && merged[k] !== defaults?.[k]183196184197 if (adapterName === 'ollama' && maxTokensKey === undefined) {185198 // Honor a caller-set limit in either shape: a recognised flat key (e.g.186199 // left over from a migration) or the nested `options.num_predict`.187- const callerSetFlatLimit = KNOWN_MAX_TOKENS_KEYS.some(188- (k) => typeof merged[k] === 'number',189- )190200 const existing =191201 merged.options && typeof merged.options === 'object'192202 ? (merged.options as Record<string, unknown>)193203 : undefined204+ // The merge is one level deep, so default `options` keep their identity.205+ const callerOptions = existing !== defaults?.options
Applied a fix from CI1945c77
- 2 bot comments
tanstack-source reviewed
@@ -189,14 +200,17 @@189- )190200 const existing =191201 merged.options && typeof merged.options === 'object'192202 ? (merged.options as Record<string, unknown>)193203 : undefined204+ // The merge is one level deep, so default `options` keep their identity.205+ const callerOptions = existing !== defaults?.options194206 if (195- callerSetFlatLimit ||196- (existing && typeof existing.num_predict === 'number')207+ KNOWN_MAX_TOKENS_KEYS.some(callerSet) ||208+ (callerOptions && typeof existing?.num_predict === 'number')197209 ) {198210 return merged199211 }200- merged.options = { num_predict: maxLength, ...existing }212+ merged.options = callerOptions213+ ? { num_predict: maxLength, ...existing }214+ : { ...existing, num_predict: maxLength }201215 return merged202216 }
Applied a fix from CI43de3c5
- 2 bot comments
tanstack-source reviewed
@@ -69,5 +69,6 @@6969 > {7070 readonly name = 'fal' as const7171 private readonly client: FalClient72+ readonly defaultModelOptions: FalAudioProviderOptions<TModel> | undefined727373- constructor(model: TModel, config?: FalClientConfig) {74+ constructor(@@ -84,6 +91,11 @@8491 }85928693 private readonly client: GeminiClient94+ readonly defaultModelOptions: GeminiNativeImageProviderOptions | undefined879588- constructor(config: GeminiImageConfig, model: TModel) {96+ constructor(97+ { defaultModelOptions, ...config }: GeminiImageConfig,98+ model: TModel,99+ ) {89100 super(model, config)101+ this.defaultModelOptions = defaultModelOptions
tombeckenham pushedc5d7322
tanstack-source reviewed
@@ -70,6 +70,9 @@7070 readonly name = 'fal' as const7171 private readonly client: FalClient727273- constructor(model: TModel, config?: FalClientConfig) {74- super(model, {})73+ constructor(74+ model: TModel,75+ config?: FalAdapterConfig<FalAudioProviderOptions<TModel>>,76+ ) {77+ super(model, { defaultModelOptions: config?.defaultModelOptions })7578 this.client = createAdapterFalClient(config)@@ -44,3 +44,3 @@4444 /**4545 * Configuration for Gemini image adapter4646 */
tombeckenham pushedef01724
tanstack-source reviewed
@@ -70,5 +70,8 @@7070 readonly name = 'fal' as const7171 private readonly client: FalClient727273- constructor(model: TModel, config?: FalClientConfig) {74- super(model, {})73+ constructor(74+ model: TModel,75+ config?: FalAdapterConfig<FalAudioProviderOptions<TModel>>,76+ ) {77+ super(model, { defaultModelOptions: config?.defaultModelOptions })@@ -85,5 +92,8 @@85928693 private readonly client: GeminiClient879488- constructor(config: GeminiImageConfig, model: TModel) {89- super(model, config)95+ constructor(96+ { defaultModelOptions, ...config }: GeminiImageConfig,97+ model: TModel,98+ ) {99+ super(model, { ...config, defaultModelOptions })
tombeckenham pushed7241be0
tanstack-source reviewed
@@ -70,5 +70,8 @@7070 readonly name = 'fal' as const7171 private readonly client: FalClient727273- constructor(model: TModel, config?: FalClientConfig) {74- super(model, {})73+ constructor(74+ model: TModel,75+ config?: FalAdapterConfig<FalAudioProviderOptions<TModel>>,76+ ) {77+ super(model, { defaultModelOptions: config?.defaultModelOptions })@@ -85,5 +92,8 @@85928693 private readonly client: GeminiClient879488- constructor(config: GeminiImageConfig, model: TModel) {89- super(model, config)95+ constructor(96+ { defaultModelOptions, ...config }: GeminiImageConfig,97+ model: TModel,98+ ) {99+ super(model, { ...config, defaultModelOptions })github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
github-actions[bot](GitHub profile, opens in a new tab) removed labelswaiting-on: author
tombeckenham approved these changes
tombeckenham pushed95cc0a8
tanstack-source reviewed
@@ -70,5 +70,8 @@7070 readonly name = 'fal' as const7171 private readonly client: FalClient727273- constructor(model: TModel, config?: FalClientConfig) {74- super(model, {})73+ constructor(74+ model: TModel,75+ config?: FalAdapterConfig<FalAudioProviderOptions<TModel>>,76+ ) {77+ super(model, { defaultModelOptions: config?.defaultModelOptions })@@ -85,5 +92,8 @@85928693 private readonly client: GeminiClient879488- constructor(config: GeminiImageConfig, model: TModel) {89- super(model, config)95+ constructor(96+ { defaultModelOptions, ...config }: GeminiImageConfig,97+ model: TModel,98+ ) {99+ super(model, { ...config, defaultModelOptions })github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: author
github-actions[bot](GitHub profile, opens in a new tab) removed labelswaiting-on: maintainer
tombeckenham merged thisafed460
tombeckenham closed this
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 95cc0a8c364bd8e06c9f899a3cea4029814038b1
nx run-many --targets=test:coverage --projects=...💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗
☁️ Nx Cloud last updated this comment at
2026-12-02 06:50:03UTC