ai-cloudflare: `decide()` with `typesafe/jev` fails on every call, over the binding and over REST #2231
TanStack AI version#
@tanstack/ai@1.8.0, @tanstack/ai-cloudflare@1.2.1 (latest). The bug is also on main at c695ada.
Framework/Library version#
Cloudflare Workers (wrangler@4.166.0 dev, remote AI binding) and Node 26 (REST)
Describe the bug and the steps to reproduce it#
decide() with the Cloudflare decider fails on every call, over both transports.
1. Binding: TypeError: Cannot read properties of undefined (reading 'input_tokens')#
Workers AI returns Jev output wrapped in a run envelope:
const raw = await env.AI.run('typesafe/jev', { state, questions })
// {"state":"Completed","result":{"model":"jev-1.13.0","answers":{"kind":{"type":"choice","choice":"receipt","probabilities":{"receipt":1,"other":0},"confidence":1},"shipped":{"type":"noul","noul":0.99}},"usage":{"input_tokens":397,"output_tokens":49}}}
CloudflareEvaluateAdapter.evaluate (src/adapters/evaluate.ts) casts the runModel output to { model, answers, usage } and reads output.usage.input_tokens from the top level, which is undefined:
TypeError: Cannot read properties of undefined (reading 'input_tokens')
at CloudflareEvaluateAdapter.evaluate (worker.js:1081:40)
at async decide (worker.js:884:20)
at async Object.fetch (worker.js:1142:24)
(JavaScriptCore runtimes print the same error as undefined is not an object (evaluating 'output.usage.input_tokens').) The call is billed, but the answers and usage are lost.
2. REST: 400 {"code":7000,"message":"No route for that URI"}#
runModel (src/utils/run.ts) sends POST /accounts/{id}/ai/run/typesafe/jev with body { state, questions }:
Error: Workers AI request for typesafe/jev failed (400): {"success":false,"errors":[{"code":7000,"message":"No route for that URI"}],"messages":[],"result":null}
at runModel (@tanstack/ai-cloudflare/dist/esm/utils/run.js:47:26)
at async CloudflareEvaluateAdapter.evaluate (@tanstack/ai-cloudflare/dist/esm/adapters/evaluate.js:26:19)
at async decide (@tanstack/ai/dist/esm/activities/evaluate/index.js:279:18)
The model-in-path route exists only for @cf/ models. Cloudflare's Jev page and the AI Gateway REST API page document third-party models on POST /accounts/{id}/ai/run with a { "model": "typesafe/jev", "input": { ... } } body. That request works, and it returns the same envelope inside the usual REST wrapper:
{"result":{"state":"Completed","result":{"model":"jev-1.13.0","answers":{...},"usage":{"input_tokens":397,"output_tokens":49}}},"success":true,"errors":[],"messages":[]}
So after runModel strips the REST wrapper, both transports hand the adapter { state, result }.
Why the tests pass#
tests/evaluate-adapter.test.ts mocks the old URL and an un-enveloped { model, answers, usage } body, and no E2E spec covers the Cloudflare decider. Cloudflare's Jev page also shows the output without the envelope. Other third-party non-chat model pages, such as openai/tts-1, google/nano-banana and elevenlabs/eleven-v3, do show { state: "Completed", result, gatewayMetadata }.
Minimal repro#
wrangler.jsonc:
{
"name": "jev-repro",
"main": "src/worker.js",
"compatibility_date": "2026-11-01",
"compatibility_flags": ["nodejs_compat"],
"ai": { "binding": "AI", "remote": true }
}
src/worker.js:
import { decide, choice, boolean } from '@tanstack/ai'
import { createCloudflareDecider } from '@tanstack/ai-cloudflare'
// Synthetic state: a fake order receipt.
const state = {
merchant: 'Example Outfitters',
order_id: 'ORD-0001',
items: [{ name: 'Trail socks', qty: 2, price_usd: 12 }],
total_usd: 24,
status: 'shipped',
}
export default {
async fetch(request, env) {
if (new URL(request.url).pathname === '/raw') {
return Response.json(
await env.AI.run('typesafe/jev', {
state,
questions: {
kind: {
type: 'choice',
instructions: 'What kind of message is this?',
criteria: { receipt: 'Order receipt or confirmation', other: 'Anything else' },
},
shipped: { type: 'noul', instructions: 'Has the order shipped?' },
},
}),
)
}
const result = await decide({
adapter: createCloudflareDecider('typesafe/jev', { binding: env.AI }),
// REST variant (plain Node): createCloudflareDecider('typesafe/jev', { accountId, apiKey })
state,
questions: {
kind: choice({
instructions: 'What kind of message is this?',
options: { receipt: 'Order receipt or confirmation', other: 'Anything else' },
}),
shipped: boolean({ instructions: 'Has the order shipped?' }),
},
})
return Response.json(result)
},
}
npm i @tanstack/ai@1.8.0 @tanstack/ai-cloudflare@1.2.1 && npm i -D wrangler@4.166.0
npx wrangler dev
curl localhost:8787/raw # shows { state, result }
curl localhost:8787/ # TypeError
Proposed fix#
- In
evaluate.ts, readresultfrom the{ state, result }envelope. - In
runModel(REST only), send model ids without an@prefix to/ai/runwith a{ model, input }body.@cf/and@hf/models keep the model-in-path route, because/ai/runneeds acf-aig-gateway-idheader for those models.
I am opening a PR with this fix. It has a unit test and an E2E spec that fail on main and pass with the fix. I also ran it against live Workers AI over both transports.
Not in this issue: for third-party model ids, the image, TTS and transcription adapters also get this envelope, with a different result shape. I can file that separately.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
The code above (a Worker plus wrangler dev with a remote AI binding).
Do you intend to try to help solve this bug with your own PR?#
Yes, I am also opening a PR that solves the problem alongside this issue.
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
tanstack-source added labelsbug
mckamyk(GitHub profile, opens in a new tab) linked this to #2232
github-actions[bot](GitHub profile, opens in a new tab) assignedjherr
github-actions[bot](GitHub profile, opens in a new tab) added labelshas-pr
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
tombeckenham closed this
Sign in to join this conversation.