Back to Posts

Cloudflare's TanStack AI Adapters Are Here: Every AI Model, One Unified Interface

G
github-actions[bot]
View on GitHub
  • cloudflare-workers
  • ai-sdk
  • ai-agents
  • typescript
  • release

If you've been building AI-powered applications on Cloudflare Workers, you know the dance: wire up bindings, handle streaming formats, deal with provider differences, and pray the stream doesn't silently truncate on you. That dance just got a whole lot simpler.

The Cloudflare team just released @cloudflare/tanstack-ai@0.1.0 — a brand-new adapter library that bridges TanStack AI with Cloudflare's Workers AI and AI Gateway ecosystem, plus major third-party providers. This isn't a thin wrapper. It's a full-featured, battle-tested integration layer with 186 unit tests and end-to-end coverage across dozens of models.

Let's dig in.

Workers AI Gets First-Class TanStack Support

The heart of this release is a set of purpose-built adapters that let you use Workers AI models through TanStack AI's unified interface. Each adapter supports four configuration modes out of the box:

  • Plain binding — env.AI directly
  • Plain REST — account ID + API key
  • AI Gateway binding — env.AI.gateway(id)
  • AI Gateway REST — account ID + gateway ID

Here's what's available:

createWorkersAiChat — Streaming Chat with All the Trimmings

This is the flagship adapter and it's loaded. You get streaming chat completions via the OpenAI-compatible API, but that's just the start:

  • Tool calling with full round-trip support — call a tool, get the result back, continue the conversation
  • Structured output via json_schema — enforce response shapes at the model level
  • Reasoning text streaming — STEP_STARTED/STEP_FINISHED AG-UI events for thinking models like QwQ, DeepSeek R1, and Kimi K2.5

Under the hood, a custom fetch shim translates OpenAI SDK calls to env.AI.run() when you're in binding mode, with a stream transformer that handles both Workers AI's native format and the OpenAI-compatible format. You don't have to think about any of this — it just works.

Beyond Chat

The adapter coverage goes well beyond text generation:

AdapterWhat it doesModels
createWorkersAiImageText-to-image generationStable Diffusion and others
createWorkersAiTranscriptionSpeech-to-textWhisper, Deepgram Nova-3
createWorkersAiTtsText-to-speechDeepgram Aura-1
createWorkersAiSummarizeText summarizationBART-large-CNN
createWorkersAiEmbeddingText embeddings(Implemented but not yet exported — waiting on TanStack AI's BaseEmbeddingAdapter)

Notable detail: Deepgram Nova-3 transcription automatically detects the model name and switches to raw binary audio via REST instead of JSON. No special configuration needed.

Route Any Provider Through AI Gateway

This is where things get really interesting. The library includes adapters for five third-party providers, all routable through Cloudflare AI Gateway for caching, rate limiting, and unified billing:

  • OpenAI — Chat, summarize, image, transcription, TTS, video (createOpenAi*)
  • Anthropic — Chat, summarize (createAnthropic*)
  • Gemini — Chat, summarize, image, TTS (createGemini*) — credentials-only since the Google GenAI SDK doesn't support custom fetch
  • Grok — Chat, summarize, image (createGrok*)
  • OpenRouter — Chat, summarize, image (createOpenRouter*) — accepts any model string

Each adapter injects a custom fetch (or httpOptions for Gemini) that handles both binding and credential-based gateway configurations. You get AI Gateway's observability and cost controls without changing how you call the provider.

The shared createGatewayFetch factory powers all of this, with support for cache control headers including skipCache, cacheTtl, customCacheKey, and metadata.

Streams That Don't Leave You Hanging

One of the most frustrating things about working with streaming AI responses is silent failures. The Cloudflare team clearly felt this pain and built in serious robustness features:

  • Premature stream termination detection — If Workers AI truncates a response or the connection drops without a finish_reason, the adapter emits proper closing events so your consumers don't hang indefinitely. This alone is worth the upgrade if you've been bitten by zombie connections.
  • Graceful non-streaming fallback — Some models return a complete response even when you pass stream: true. Instead of crashing, the binding shim wraps it into a valid streaming response.
  • Tool call ID sanitization — Workers AI bindings enforce a strict [a-zA-Z0-9]{9} validation on tool call IDs. The adapter handles this automatically so you don't get cryptic validation errors.

Utilities You'll Actually Use

Beyond the adapters themselves, there's a solid set of utilities:

  • Config detection helpers — isDirectBindingConfig, isDirectCredentialsConfig, isGatewayConfig use structural typing to discriminate between env.AI, env.AI.gateway(id), and credential-based configs. Handy if you're building your own abstractions on top.
  • createWorkersAiBindingFetch — The fetch shim that makes env.AI look like an OpenAI endpoint, including stream transformation. Useful standalone if you want binding mode without the full adapter.
  • Binary utilities — Normalize Workers AI responses (Uint8Array, ArrayBuffer, ReadableStream, JSON wrapper) to base64. If you've dealt with image or audio responses from Workers AI, you know why this matters.

Tested Against Real Models, Not Just Mocks

The testing story here is impressive for an initial release:

  • 186 unit tests covering all adapters, config modes, stream transformation, message building, tool calling, reasoning events, premature termination, and the public API surface
  • E2E integration tests against real Workers AI APIs — both binding and REST paths — across 12 chat models, 4 transcription models, plus image, TTS, and summarize capabilities
  • The E2E suite validates chat, multi-turn conversations, tool calling, tool round-trips, structured output, reasoning, and all non-chat capabilities

The package ships with tree-shakeable exports and per-adapter entry points for both ESM and CJS, so you only pay for what you import.

Get Started

Install the package:

npm install @cloudflare/tanstack-ai

If you're using a binding configuration, you're ready to go with your existing env.AI. For REST mode, you'll need your Cloudflare account ID and API key. For AI Gateway, pass your gateway ID along with either approach.

Check out the source on GitHub and the PR #389 for full implementation details.


This is a 0.1.0 release, so the API may evolve — but the scope and quality of what's here at launch is substantial. If you're building AI features on Cloudflare Workers with TanStack, this is the integration layer you've been waiting for.