feat(ai, ai-openrouter): add defaultModelOptions to text adapters #2244
Open
HugoPerard(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/2244/head·opened View on GitHubSet defaultModelOptions on a text adapter, and every chat() call with that adapter sends them. A key that the call sets in modelOptions replaces the default key. The OpenRouter Chat and Responses adapters take the option, so you can set provider routing (for example sort: 'latency') once per adapter.
🎯 Changes#
TextAdapterhas an optionaldefaultModelOptionsfield.chat()merges the defaults under the call'smodelOptionsonce, intoRuntimeTextActivityOptions. The merge is one level deep, like thedefaultOptionsof TanStack Query.- The engine, the middleware (
onConfig,ctx.modelOptions), and the adapter all get the merged value. SosupportsCombinedToolsAndSchemaandcombinedStructuredOutputSourcesee the defaults too. createOpenRouterText(),openRouterText(),createOpenRouterResponsesText(), andopenRouterResponsesText()takedefaultModelOptions. The adapters remove it from the config before they create the SDK client.OpenRouterTextConfig<TModel>is a new exported type that keeps the per-model types.- Docs: a new section in
docs/adapters/openrouter.md(Model routing) and indocs/advanced/typed-options.md(the order of the levels).
OpenRouterConfig does not change, because the summarize adapter uses it and does not go through chat().
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request.- 493 of 495 tasks pass. The 2 failures also occur on clean
mainon this machine (Node 26, macOS), and have no relation to this PR:@tanstack/ai-openai:test:lib(tests/siwc.test.ts):localStorageisundefinedon Node 26.@tanstack/ai-sandbox-cloudflare:test:lib(tests/container-checkpoint.test.ts): the BSDchmoddoes not accept--.
- 493 of 495 tasks pass. The 2 failures also occur on clean
- I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Testing#
Commands run
pnpm test:pr: 493 of 495 tasks pass. Two tasks fail on cleanmaintoo, on this machine (Node 26, macOS):@tanstack/ai-openai:test:lib:tests/siwc.test.tsfails becauselocalStorageisundefinedon Node 26.@tanstack/ai-sandbox-cloudflare:test:lib:tests/container-checkpoint.test.tsfails because the BSDchmoddoes not accept--.
pnpm --filter @tanstack/ai-e2e test:e2e: 805 pass. Two specs (multimodal-tool-result-wire,harness-protocol-inputs) fail on some runs and pass on others. They have no relation to this change.- The new unit tests in
packages/aifail (3 of 4) without the change inchat().
Manual test
- Run
pnpm --filter @tanstack/ai-e2e test:e2e -- --grep "adapter defaultModelOptions". - The
defaultscenario sendsprovider: { sort: 'latency', preferred_max_latency: { p90: 2.5 } }. - The
overridescenario sends onlyprovider: { only: ['openai'] }.
How this PR makes testing easy
packages/ai/tests/chat-default-model-options.test.ts: the merge, the override, and the merged value in the engine and middleware.packages/ai-openrouter/tests/default-model-options-wire.test.ts: the request body for the Chat and Responses adapters.testing/e2e/tests/openrouter-default-model-options-wire.spec.ts: the body on the wire, read from the aimock journal.
Risk / rollback#
Low. Adapters without defaultModelOptions behave as before, because chat() only merges when the field is set. To undo, revert this PR.
Public API change#
Before
const adapter = openRouterText('openai/gpt-5.5')
chat({
adapter,
messages,
threadId,
modelOptions: { provider: { sort: 'latency' } }, // on every call
})
After
const adapter = openRouterText('openai/gpt-5.5', {
defaultModelOptions: { provider: { sort: 'latency' } },
})
chat({ adapter, messages, threadId })
🤖 Generated with Claude Code
Summary by CodeRabbit#
- Documentation
- Expanded the typed-options guide with an OpenRouter example covering provider routing and completion-token limits.
- Clarified how adapter defaults,
chat()options, and middleware configuration take precedence, including how call-level values override defaults andundefinedpreserves them. - Explained that
chat()merges options one level deep, replacing nested objects such asproviderortextin full.
- 1
- 2
Applied a fix from CIc90d614
Sign in to join this conversation.
Merged
maininto the head. I resolved 6 conflicted files..changeset/adapter-default-model-options.mdusesopenai/gpt-6.1-solfrommain.docs/config.jsonusesupdatedAt2026-11-24frommain.packages/ai/src/activities/chat/index.tscallswithDefaultModelOptionsintoRuntimeTextActivityOptions.defaultModelOptionstosuper()and strip it from the SDK config.Docs keep both sides:
docs/advanced/typed-options.mdkeeps every-adapter defaults frommain.undefinedkeys, and middlewareonConfigreplace rules.TanStack Source bot, conflicts agent