Skip to content
Source
Sign inSign up
TanStack/ai

feat(ai, ai-openrouter): add defaultModelOptions to text adapters #2244

OpenHugoPerard(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2244/headopened View on GitHub

HugoPerard(GitHub profile, opens in a new tab)commented

Set 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

  • TextAdapter has an optional defaultModelOptions field.
  • chat() merges the defaults under the call's modelOptions once, in toRuntimeTextActivityOptions. The merge is one level deep, like the defaultOptions of TanStack Query.
  • The engine, the middleware (onConfig, ctx.modelOptions), and the adapter all get the merged value. So supportsCombinedToolsAndSchema and combinedStructuredOutputSource see the defaults too.
  • createOpenRouterText(), openRouterText(), createOpenRouterResponsesText(), and openRouterResponsesText() take defaultModelOptions. 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 in docs/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 main on this machine (Node 26, macOS), and have no relation to this PR:
      • @tanstack/ai-openai:test:lib (tests/siwc.test.ts): localStorage is undefined on Node 26.
      • @tanstack/ai-sandbox-cloudflare:test:lib (tests/container-checkpoint.test.ts): the BSD chmod does not accept --.
  • 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 clean main too, on this machine (Node 26, macOS):
    • @tanstack/ai-openai:test:lib: tests/siwc.test.ts fails because localStorage is undefined on Node 26.
    • @tanstack/ai-sandbox-cloudflare:test:lib: tests/container-checkpoint.test.ts fails because the BSD chmod does 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/ai fail (3 of 4) without the change in chat().

Manual test

  1. Run pnpm --filter @tanstack/ai-e2e test:e2e -- --grep "adapter defaultModelOptions".
  2. The default scenario sends provider: { sort: 'latency', preferred_max_latency: { p90: 2.5 } }.
  3. The override scenario sends only provider: { 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 and undefined preserves them.
    • Explained that chat() merges options one level deep, replacing nested objects such as provider or text in full.
    1. Applied a fix from CIc90d614

    2. tanstack-sourceBotcommented Only here. This is not on GitHub.

      Merged main into the head. I resolved 6 conflicted files.

      1. .changeset/adapter-default-model-options.md uses openai/gpt-6.1-sol from main.
      2. docs/config.json uses updatedAt 2026-11-24 from main.
      3. packages/ai/src/activities/chat/index.ts calls withDefaultModelOptions in toRuntimeTextActivityOptions.
      4. OpenRouter Chat and Responses adapters pass defaultModelOptions to super() and strip it from the SDK config.

      Docs keep both sides:

      1. docs/advanced/typed-options.md keeps every-adapter defaults from main.
      2. The same page keeps the OpenRouter provider-routing example from the pull request.
      3. It also keeps merge order, undefined keys, and middleware onConfig replace rules.

      TanStack Source bot, conflicts agent

Sign in to join this conversation.