Repository navigation
fix(tanstack-ai): forward reasoning_effort and chat_template_kwargs (#503) - #505
Merged
Merged
Conversation
…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
commit: |
Merged
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
modelOptionspassthrough for Workers AI-specific reasoning controls (reasoning_effort,chat_template_kwargs) increateWorkersAiChat.binding.run(model, inputs)at theinputslevel across all four config modes (binding / REST / gateway binding / gateway REST).WorkersAiTextModelOptionswith an index-signature escape hatch for future Workers AI input fields.Closes #503.
Why
The TanStack AI adapter silently dropped
reasoning_effortandchat_template_kwargsbecausechatStreamandstructuredOutputnever 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.tsWorkersAiTextModelOptionsinterface: typedreasoning_effort/chat_template_kwargsplus a[key: string]: unknownescape hatch.normalizeModelOptionshelper: stripsundefined, preservesnull(becausereasoning_effort: nullis the explicit "disable reasoning" signal), defensively ignores non-object runtime values so stray primitives don't leak index-keyed entries.WorkersAiTextAdapternow types the provider-options generic withWorkersAiTextModelOptions.chatStreamandstructuredOutputspread normalizedmodelOptionsfirst 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.modelOptionson their create() calls.src/utils/create-fetcher.tscreateWorkersAiBindingFetch's input builder to pass throughreasoning_effortandchat_template_kwargsfrom the OpenAI-shaped body onto theinputsobject (2nd arg ofbinding.run). Uses!== undefinedsonullround-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.query, so no change is needed there.src/index.ts— exportsWorkersAiTextModelOptions.README.md— new "Reasoning Controls" section with the usage pattern and a note onnullvsundefinedsemantics.Usage
Test plan
reasoning_effortviamodelOptionsreaches binding inputs (chatStream)chat_template_kwargsviamodelOptionsreaches binding inputsreasoning_effort: nullpreserved end-to-endundefinedvalues inmodelOptionsstrippedtemperaturewins over a conflictingmodelOptions.temperaturestructuredOutputforwardsmodelOptionsalongsideresponse_formatmodelOptions) leaves inputs untouchedmodelOptions(string, array) falls back gracefully — no index-keyed leakmodelOptionson retrychat_template_kwargspassthroughnullreasoning preserved{}forchat_template_kwargsforwardedseed,stream_options, future fields) dropped — allowlist policy locked inrequest.query,modelstill strippedreasoning_effort: nullinrequest.queryNotes for reviewers
Made with Cursor