Skip to content

fix(tanstack-ai): forward reasoning_effort and chat_template_kwargs (#503) - #505

Merged
threepointone merged 2 commits into
mainfrom
fix/tanstack-ai-reasoning-passthrough
Apr 23, 2026
Merged

threepointone merged 2 commits into
mainfrom
fix/tanstack-ai-reasoning-passthrough

Conversation

@threepointone

Copy link
Copy Markdown
Contributor

Summary

  • Adds modelOptions passthrough for Workers AI-specific reasoning controls (reasoning_effort, chat_template_kwargs) in createWorkersAiChat.
  • Values reach binding.run(model, inputs) at the inputs level across all four config modes (binding / REST / gateway binding / gateway REST).
  • New exported type WorkersAiTextModelOptions with an index-signature escape hatch for future Workers AI input fields.

Closes #503.

Why

The TanStack AI adapter silently dropped reasoning_effort and chat_template_kwargs because chatStream and structuredOutput never placed them in the OpenAI SDK body, and the binding shim had a hardcoded allowlist that didn't include them. Reasoning models (GLM-4.7-flash, Kimi K2.5/K2.6, GPT-OSS, QwQ) therefore had no way to control reasoning from the adapter, leading to output token budgets being burned on chain-of-thought with no visible content. Sibling issue to #501 (workers-ai-provider).

What changed

src/adapters/workers-ai.ts

  • New WorkersAiTextModelOptions interface: typed reasoning_effort / chat_template_kwargs plus a [key: string]: unknown escape hatch.
  • normalizeModelOptions helper: strips undefined, preserves null (because reasoning_effort: null is the explicit "disable reasoning" signal), defensively ignores non-object runtime values so stray primitives don't leak index-keyed entries.
  • WorkersAiTextAdapter now types the provider-options generic with WorkersAiTextModelOptions.
  • chatStream and structuredOutput spread normalized modelOptions first in each OpenAI SDK create call, so canonical TanStack-managed fields (model, messages, temperature, max_tokens, stream, tools, response_format, stream_options) always win in the spread order if a user accidentally duplicates a key.
  • Both the streaming path and the non-streaming fallback path carry modelOptions on their create() calls.

src/utils/create-fetcher.ts

  • Extends createWorkersAiBindingFetch's input builder to pass through reasoning_effort and chat_template_kwargs from the OpenAI-shaped body onto the inputs object (2nd arg of binding.run). Uses !== undefined so null round-trips. The allowlist stays tight — unknown fields (seed, stream_options, future OpenAI-only fields) are still dropped so the binding's strict schema validation doesn't reject the whole request.
  • The gateway path already forwards the whole body as query, so no change is needed there.

src/index.ts — exports WorkersAiTextModelOptions.

README.md — new "Reasoning Controls" section with the usage pattern and a note on null vs undefined semantics.

Usage

import { chat } from "@tanstack/ai";
import { createWorkersAiChat } from "@cloudflare/tanstack-ai";

const adapter = createWorkersAiChat("@cf/zai-org/glm-4.7-flash", { binding: env.AI });

const response = chat({
  adapter,
  stream: true,
  messages: [{ role: "user", content: "Summarize in one sentence." }],
  modelOptions: {
    reasoning_effort: "low",              // "low" | "medium" | "high" | null
    chat_template_kwargs: { enable_thinking: false },
  },
});

Test plan

  • 19 new unit tests across binding / adapter / gateway paths
  • Adapter: reasoning_effort via modelOptions reaches binding inputs (chatStream)
  • Adapter: chat_template_kwargs via modelOptions reaches binding inputs
  • Adapter: reasoning_effort: null preserved end-to-end
  • Adapter: undefined values in modelOptions stripped
  • Adapter: top-level temperature wins over a conflicting modelOptions.temperature
  • Adapter: structuredOutput forwards modelOptions alongside response_format
  • Adapter: default path (no modelOptions) leaves inputs untouched
  • Adapter: non-object modelOptions (string, array) falls back gracefully — no index-keyed leak
  • Adapter: non-streaming fallback path preserves modelOptions on retry
  • Binding shim: reasoning fields land on inputs (2nd arg), not options (3rd arg)
  • Binding shim: chat_template_kwargs passthrough
  • Binding shim: null reasoning preserved
  • Binding shim: absence stays absent
  • Binding shim: empty {} for chat_template_kwargs forwarded
  • Binding shim: streaming requests carry reasoning fields
  • Binding shim: unknown body fields (seed, stream_options, future fields) dropped — allowlist policy locked in
  • Gateway: reasoning fields in request.query, model still stripped
  • Gateway: reasoning_effort: null in request.query
  • Gateway: arbitrary unknown fields forwarded (escape hatch)
  • 244/244 unit tests passing
  • Type-check clean

Notes for reviewers

  • Two commits: the original fix plus a review pass adding defensive guards and lock-in tests for the binding-vs-gateway allowlist asymmetry. Happy to squash pre-merge.
  • Scope is intentionally tight — only the two reasoning fields. Other Workers AI-specific fields can be added to the binding shim's allowlist in follow-ups if needed; today they work via REST/gateway through the escape-hatch spread.
  • Sibling PR: fix(workers-ai-provider): forward reasoning_effort and chat_template_kwargs (#501) #504 (workers-ai-provider). The two packages are independent and can merge/release separately.

Made with Cursor

…503)

`createWorkersAiChat` was silently dropping `reasoning_effort` and
`chat_template_kwargs`, so reasoning-capable Workers AI models (GLM-4.7-flash,
Kimi K2.5/K2.6, GPT-OSS, QwQ) had no way to control reasoning from the adapter.

- Add `WorkersAiTextModelOptions` type and wire it through `chatStream` and
  `structuredOutput` via TanStack AI's `modelOptions` field.
- Spread `modelOptions` first in the outbound body so top-level TanStack
  fields (`temperature`, `maxTokens`, etc.) still take precedence.
- Strip `undefined` values but preserve `null` — `reasoning_effort: null` is
  the explicit "disable reasoning" signal.
- Extend `createWorkersAiBindingFetch` so the two reasoning fields land on
  `inputs` (2nd arg of `binding.run`), not on `options`. Gateway and REST
  paths already forward the full body.
- Export `WorkersAiTextModelOptions` from the public API.

Adds 15 tests covering adapter-level forwarding, binding shim passthrough,
gateway query passthrough, null preservation, undefined stripping, empty
objects, streaming requests, and precedence with top-level fields.

Closes #503.

Made-with: Cursor
Review-driven follow-ups on top of the #503 fix:

- Defensively guard `modelOptions` against non-object runtime values.
  `Object.entries("abc")` returns `[["0","a"],["1","b"],["2","c"]]`, which
  would leak spurious index keys into the body. Arrays similarly become
  index-keyed. Only accept plain objects; fall back to no-op otherwise.
- Test: non-object modelOptions falls back without polluting the body.
- Test: non-streaming fallback path preserves modelOptions. Some models
  (e.g. GPT-OSS) reject `stream: true` and the adapter retries without
  it — reasoning controls must survive the retry.
- Test: binding shim's allowlist rejects unknown fields (`seed`,
  `stream_options`, future fields) so the binding's strict schema
  validation doesn't reject the whole request.
- Test: gateway path forwards arbitrary unknown fields on query (the
  escape-hatch side of the asymmetry documented on
  `WorkersAiTextModelOptions`).

Made-with: Cursor
@pkg-pr-new

pkg-pr-new Bot commented Apr 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

npx https://pkg.pr.new/cloudflare/ai/ai-gateway-provider@505
npx https://pkg.pr.new/cloudflare/ai/@cloudflare/tanstack-ai@505
npx https://pkg.pr.new/cloudflare/ai/workers-ai-provider@505

commit: 4ec0af0

@threepointone
threepointone merged commit a1c1355 into main Apr 23, 2026
3 checks passed
@threepointone
threepointone deleted the fix/tanstack-ai-reasoning-passthrough branch April 23, 2026 13:06
@github-actions github-actions Bot mentioned this pull request Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@cloudflare/tanstack-ai: no passthrough for reasoning_effort or chat_template_kwargs

1 participant