fix(ai-cloudflare): read the Jev run result and run third-party models on /ai/run #2232
main fromrefs/pull/2232/head·merged by tombeckenham View on GitHubdecide() with createCloudflareDecider('typesafe/jev', ...) failed on every call. Over the binding, it threw Cannot read properties of undefined (reading 'input_tokens'). Over REST, it got 400 No route for that URI. This PR reads the Jev answer from the Workers AI run result, and sends third-party models to the REST /ai/run endpoint.
🎯 Changes#
evaluate.ts: read{ model, answers, usage }fromresultin the{ state, result }output that Workers AI returns.run.ts(REST only): model ids without an@prefix go toPOST /ai/runwith a{ model, input }body.@cf/and@hf/models keep the/ai/run/{model}route.- Updated unit test and a new E2E spec. Patch changeset for
@tanstack/ai-cloudflare.
No docs change: docs/adapters/cloudflare.md already shows this usage. With this fix, that usage works.
✅ 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. - 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).
Root cause#
Issue. Every decide() call with the Cloudflare decider and typesafe/jev fails. Over the binding, it throws a TypeError. Over REST, it gets a 400. The binding call is billed, but the answers are lost.
Cause. Workers AI returns third-party model output as { state: 'Completed', result: { model, answers, usage } }. CloudflareEvaluateAdapter.evaluate read usage from the top level. Also, runModel built REST URLs as /ai/run/{model}. Cloudflare has that route only for @cf/ and @hf/ models.
Fix. The adapter reads result. On REST, runModel sends model ids without an @ prefix to /ai/run with { model, input }, as the Jev page and the REST API page show. That endpoint returns the same { state, result } output, so one read covers both transports.
Possible alternatives#
- Read
resultinsiderunModelfor every adapter. The image, TTS and transcription adapters also get this output for third-party ids, but with otherresultshapes. Each one needs its own mapping, so that is a separate change. - Send every REST run to
/ai/run.@cf/models on/ai/runneed acf-aig-gateway-idheader. REST users without a gateway would break. - Throw when
stateis not'Completed'. This gives a clearer error for a failed run. I did not add it, because no failed run was reproduced.
Testing (test plan)#
Commands run
pnpm test:pr: pass (76 tasks, React Native smoke,scan-dangling-dts).pnpm --filter @tanstack/ai-e2e test:e2e --grep "cloudflare": 4 passed. I did not run the full E2E matrix locally.- Unit repro on clean
main(c695ada) and on this branch:
=== clean main (c695ada) ===
× runs on /ai/run, unwraps the run result, and forwards gateway headers
× unwraps the run result from the binding
TypeError: Cannot read properties of undefined (reading 'input_tokens')
TypeError: Cannot read properties of undefined (reading 'input_tokens')
Tests 2 failed | 1 passed (3)
=== this branch ===
Test Files 8 passed (8)
Tests 61 passed (61)
- E2E spec with the old
src/:Route failed: Cannot read properties of undefined (reading 'input_tokens'). With this branch: passed. - Live Workers AI, with a packed build of this branch:
wrangler devwith a remote AI binding, and a Node script with{ accountId, apiKey }. Both returned answers andusage: { promptTokens: 397, completionTokens: 49, totalTokens: 446 }.
Manual test
- Make a Worker with
"ai": { "binding": "AI", "remote": true }and@tanstack/ai-cloudflare@1.2.1. - Call
decide({ adapter: createCloudflareDecider('typesafe/jev', { binding: env.AI }), state, questions }). - Run
wrangler dev, then open the route. On 1.2.1, the call throws theTypeError. - Install this branch's build and run it again. The call returns the answers and
meta.usage. - For REST, use
{ accountId, apiKey }in Node. On 1.2.1, the call gets the 400. On this branch, it returns the answers.
How this PR makes testing easy
packages/ai-cloudflare/tests/evaluate-adapter.test.tsuses the real REST URL, body and response, and the real binding output.testing/e2e/tests/cloudflare-evaluate-wire.spec.tsrunsdecide()over a fakeenv.AIand over REST with a fake fetch.
Linked issues#
Fixes #2231
Risk / rollback#
Low risk. The REST change applies only to model ids without an @ prefix, which got 400 before. @cf/ and @hf/ runs do not change. To undo, revert this PR.
🤖 Generated with Claude Code
Summary by CodeRabbit#
- Bug Fixes
- Cloudflare AI evaluations now correctly read results returned in the completed-run response, including answers and usage details.
- REST requests for third-party models now use the appropriate endpoint and request format, improving compatibility with Cloudflare’s AI service.
- 1
mckamyk(GitHub profile, opens in a new tab) linked this to fix #2231
tanstack-source added labelsbug
No actionable comments were generated in the recent review.
Walkthrough
-
The Cloudflare evaluation adapter now unwraps Workers AI’s
resultenvelope. REST calls for model IDs without an@prefix use/ai/runwith{ model, input }; prefixed models retain model-in-path routing. Unit and end-to-end tests cover binding and REST behavior.Changes: Cloudflare evaluation
Layer / File(s) Summary Unwrap results and route REST requests
.changeset/cloudflare-evaluate-run-result.md,packages/ai-cloudflare/src/adapters/evaluate.ts,packages/ai-cloudflare/src/utils/run.ts,packages/ai-cloudflare/tests/evaluate-adapter.test.tsThe adapter extracts model output from the resultfield. REST requests for models without an@prefix use/ai/runand send{ model, input }; prefixed models retain model-in-path routing. Unit tests cover binding and REST responses.Exercise binding and REST evaluation end to end
testing/e2e/src/routes/api.cloudflare-evaluate-wire.ts,testing/e2e/src/routeTree.gen.ts,testing/e2e/tests/cloudflare-evaluate-wire.spec.tsA POST route runs decide()with fake binding and REST configurations and returns both results and the captured REST request. Route registration and an end-to-end test cover the route.Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
alemtuzlak,tombeckenham
Merge Risk:
Minimal · up to076bc-
This change fixes how the Cloudflare decider reads Jev results and routes REST requests for third-party models. No merge-blocking risk is evident, and unit and E2E tests cover both transports.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the two real changes in the diff: the adapter now reads the Jev run result from the { state, result }envelope, and REST runs for third-party models go to/ai/run. It is specific a…Description check The description follows the template. It explains the problem and the fix in the Changes section, and it includes a root-cause section, testing commands with results, and a linked issue ( #2231). The c…Linked Issues check Issue #2231has two coding requirements. (1) Read the Jev answer from the Workers AI{ state, result }envelope. The head diff ofpackages/ai-cloudflare/src/adapters/evaluate.tsdestructures `resu…Out of Scope Changes check All changes support issue #2231. Theevaluate.tsandrun.tshunks implement the two requested fixes. The unit test, the E2E routetesting/e2e/src/routes/api.cloudflare-evaluate-wire.ts, its spec…Full details: Title check
-
Explanation
The title names the two real changes in the diff: the adapter now reads the Jev run result from the
{ state, result }envelope, and REST runs for third-party models go to/ai/run. It is specific and matches the changeset, adapter, and run.ts changes. It uses the conventionalfix(ai-cloudflare):prefix and is one sentence. At 81 characters it is slightly long, but it is not vague or off-topic.
Full details: Description check
-
Explanation
The description follows the template. It explains the problem and the fix in the Changes section, and it includes a root-cause section, testing commands with results, and a linked issue (
#2231). The checklist is filled in, and a changeset is referenced. The description does not give a reason for the unchecked CI/dev-only box, but that box is an alternative to the checked release box, so this is not a gap. The Changes section does not say that the docs were left unchanged in the checklist, but it does say so in the body text.
Full details: Linked Issues check
-
Explanation
Issue
#2231has two coding requirements. (1) Read the Jev answer from the Workers AI{ state, result }envelope. The head diff ofpackages/ai-cloudflare/src/adapters/evaluate.tsdestructuresresultfrom therunModeloutput and readsusage,answers, andmodelfrom it. This covers the binding path. (2) Over REST, send model ids without an@prefix toPOST /ai/runwith a{ model, input }body, and keep the/ai/run/{model}route for@cf/and@hf/ids. The head diff ofpackages/ai-cloudflare/src/utils/run.tssetsuniversal = !model.startsWith('@'), builds the URL and body from that flag, and leaves the@path unchanged. The unit test update intests/evaluate-adapter.test.tsand the E2E speccloudflare-evaluate-wire.spec.tscover both transports. The PR description reports the test results. I did not re-run them. The issue excludes image, TTS, and transcription adapters, and the PR does not change them.
Full details: Out of Scope Changes check
-
Explanation
All changes support issue
#2231. Theevaluate.tsandrun.tshunks implement the two requested fixes. The unit test, the E2E routetesting/e2e/src/routes/api.cloudflare-evaluate-wire.ts, its spec, and the generatedrouteTree.gen.tsentries provide tests for both transports. The changeset documents the patch for@tanstack/ai-cloudflare. One side effect: theuniversalcheck inrunModelis shared by all Cloudflare run paths. Third-party ids used by image, TTS, or transcription adapters now also go to/ai/runwith{ model, input }. Before this change, those requests failed with 400. Their response shapes are not unwrapped, and the issue marks that adapter work as a follow-up. This is a consequence of the requested routing change, not an unrelated change.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/ai
- Branch: main
- File: .coderabbit.yaml
- Commit: c695ada
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
04b04954-22cf-437e-b967-40b2f11caf00
Commits-
Reviewing files that changed from the base of the PR and between c695ada5091e7022bf13b235220645ceb65d43b0 and 076bc2643e6c3f517756120b5db90861825f03c1.
Files selected for processing: 7-
.changeset/cloudflare-evaluate-run-result.mdpackages/ai-cloudflare/src/adapters/evaluate.tspackages/ai-cloudflare/src/utils/run.tspackages/ai-cloudflare/tests/evaluate-adapter.test.tstesting/e2e/src/routeTree.gen.tstesting/e2e/src/routes/api.cloudflare-evaluate-wire.tstesting/e2e/tests/cloudflare-evaluate-wire.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
-
github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
Thanks for the PR, @mckamyk! 🙌 @tombeckenham will take a look.
Automated pre-review checks
- ✅ CI passing
- ✅ No merge conflicts
- ✅ Changeset present
- ✅ E2E test changes included
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
tombeckenham approved these changes
Thanks @mckamyk - reproduced both failures (binding input_tokens TypeError, REST 400) on clean main with an independent test and confirmed this branch fixes them without changing @cf/ routing. Small follow-up worth considering: guard on state !== 'Completed' in evaluate.ts so a failed Jev run gives a clear error.
View your CI Pipeline Execution ↗ for commit 076bc2643e6c3f517756120b5db90861825f03c1
Command Status Duration Result nx run-many --targets=build --exclude=examples/...✅ Succeeded 9s View ↗ 💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗
☁️ Nx Cloud last updated this comment at
2026-11-24 07:40:15UTC@tanstack/ai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@2232@tanstack/ai-acp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@2232@tanstack/ai-angular
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@2232@tanstack/ai-anthropic
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@2232@tanstack/ai-bedrock
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@2232@tanstack/ai-byteplus
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-byteplus@2232@tanstack/ai-cartesia
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cartesia@2232@tanstack/ai-claude-code
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@2232@tanstack/ai-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@2232@tanstack/ai-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cloudflare@2232@tanstack/ai-code-mode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@2232@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-snippets@2232@tanstack/ai-codex
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@2232@tanstack/ai-cohere
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-cohere@2232@tanstack/ai-compaction
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-compaction@2232@tanstack/ai-computer-local
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-local@2232@tanstack/ai-computer-playwright
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-playwright@2232@tanstack/ai-computer-sandbox
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-sandbox@2232@tanstack/ai-computer-use
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-computer-use@2232@tanstack/ai-devtools-core
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@2232@tanstack/ai-durable-stream
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-durable-stream@2232@tanstack/ai-effect
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-effect@2232@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@2232@tanstack/ai-event-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@2232@tanstack/ai-fal
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@2232@tanstack/ai-gemini
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@2232@tanstack/ai-grok
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@2232@tanstack/ai-grok-build
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@2232@tanstack/ai-groq
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@2232@tanstack/ai-harness
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-harness@2232@tanstack/ai-harness-cli
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-harness-cli@2232@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@2232@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-daytona@2232@tanstack/ai-isolate-e2b
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-e2b@2232@tanstack/ai-isolate-node
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@2232@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@2232@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs-bun@2232@tanstack/ai-llmgateway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-llmgateway@2232@tanstack/ai-lovable
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-lovable@2232@tanstack/ai-mcp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@2232@tanstack/ai-memory
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-memory@2232@tanstack/ai-meshy
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-meshy@2232@tanstack/ai-mistral
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@2232@tanstack/ai-muse
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-muse@2232@tanstack/ai-neon
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-neon@2232@tanstack/ai-octane
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-octane@2232@tanstack/ai-ollama
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@2232@tanstack/ai-ollaya
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollaya@2232@tanstack/ai-openai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@2232@tanstack/ai-opencode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@2232@tanstack/ai-openrouter
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@2232@tanstack/ai-optimize
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-optimize@2232@tanstack/ai-perplexity
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-perplexity@2232@tanstack/ai-persistence
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence@2232@tanstack/ai-preact
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@2232@tanstack/ai-react
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@2232@tanstack/ai-reactor
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-reactor@2232@tanstack/ai-remix
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-remix@2232@tanstack/ai-sandbox
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@2232@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-blaxel@2232@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-boxd@2232@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@2232@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@2232@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@2232@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-e2b@2232@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@2232@tanstack/ai-sandbox-railway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-railway@2232@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@2232@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-upstash-box@2232@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@2232@tanstack/ai-skills
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-skills@2232@tanstack/ai-solid
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@2232@tanstack/ai-svelte
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@2232@tanstack/ai-typesafe
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-typesafe@2232@tanstack/ai-uthana
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-uthana@2232@tanstack/ai-utils
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@2232@tanstack/ai-vercel-compat
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vercel-compat@2232@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vercel-gateway@2232@tanstack/ai-vertex
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vertex@2232@tanstack/ai-vue
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@2232@tanstack/ai-worldlabs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-worldlabs@2232@tanstack/openai-base
npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@2232@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@2232@tanstack/react-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@2232@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@2232@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/svelte-ai-devtools@2232commit:
076bc26tombeckenham merged thisfbe7d4e
tombeckenham closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 076bc2643e6c3f517756120b5db90861825f03c1
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR