|
| 1 | +--- |
| 2 | +title: React Native & Expo |
| 3 | +id: non-streaming-runtimes |
| 4 | +order: 4 |
| 5 | +description: "Run TanStack AI on React Native, Expo, and other runtimes that can't emit ReadableStream responses — using toJSONResponse on the server and fetchJSON on the client." |
| 6 | +keywords: |
| 7 | + - tanstack ai |
| 8 | + - react native |
| 9 | + - expo |
| 10 | + - expo router |
| 11 | + - metro bundler |
| 12 | + - non-streaming |
| 13 | + - toJSONResponse |
| 14 | + - fetchJSON |
| 15 | + - edge runtime |
| 16 | +--- |
| 17 | + |
| 18 | +You have a React Native or Expo app and you want to add AI chat, but the usual `toServerSentEventsResponse()` helper crashes on Expo's server runtime with: |
| 19 | + |
| 20 | +``` |
| 21 | +TypeError: Cannot read properties of undefined (reading 'statusText') |
| 22 | +``` |
| 23 | + |
| 24 | +…and Metro refuses to resolve `@tanstack/ai/adapters` at all. By the end of this guide, you'll have a working chat flow on Expo/React Native using a JSON-array fallback path. The same approach works for any deployment target that can't stream `ReadableStream` responses (some edge proxies, legacy serverless runtimes, etc.). |
| 25 | + |
| 26 | +## What's actually going wrong |
| 27 | + |
| 28 | +Two separate problems show up on React Native / Expo: |
| 29 | + |
| 30 | +1. **Module resolution.** `@tanstack/ai` and `@tanstack/ai-client` ship dual ESM + CJS builds with `main`/`module`/`exports` all wired up. If your version is new enough, Metro resolves them out of the box. If you're stuck on an older version, upgrade — older releases were ESM-only and Metro can't consume them. |
| 31 | + |
| 32 | +2. **Response shape.** Expo's `@expo/server` runtime (and a few edge proxies) can't emit a `ReadableStream` body, which is what `toServerSentEventsResponse` and `toHttpResponse` return. The request silently fails on the client side and `isLoading` flips back to `false` immediately. |
| 33 | + |
| 34 | +The fix for (2) is to drain the chat stream on the server, send the collected chunks as a single JSON array, and replay them on the client. You lose incremental rendering — the UI sees every chunk at once when the request resolves — but every other piece of the chat pipeline keeps working as-is. |
| 35 | + |
| 36 | +## Step 1: Return a JSON-array response on the server |
| 37 | + |
| 38 | +Swap `toServerSentEventsResponse` for `toJSONResponse` in your API route. On Expo Router: |
| 39 | + |
| 40 | +```typescript |
| 41 | +// app/api/chat+api.ts |
| 42 | +import { chat, toJSONResponse } from "@tanstack/ai"; |
| 43 | +import { openaiText } from "@tanstack/ai-openai"; |
| 44 | + |
| 45 | +export async function POST(request: Request) { |
| 46 | + const { messages } = await request.json(); |
| 47 | + |
| 48 | + const stream = chat({ |
| 49 | + adapter: openaiText("gpt-5.2"), |
| 50 | + messages, |
| 51 | + }); |
| 52 | + |
| 53 | + return toJSONResponse(stream); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +`toJSONResponse` iterates the whole stream, collects each `StreamChunk` into an array, and returns a plain `Response` with `Content-Type: application/json`. It accepts the same `init` options as `toServerSentEventsResponse` (including `abortController`) and honours any `Content-Type` you pass in `headers`. |
| 58 | + |
| 59 | +## Step 2: Use `fetchJSON` as the connection adapter on the client |
| 60 | + |
| 61 | +Swap `fetchServerSentEvents` for `fetchJSON` in your `useChat` call: |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { useChat } from "@tanstack/ai-react"; |
| 65 | +import { fetchJSON } from "@tanstack/ai-client"; |
| 66 | + |
| 67 | +export function ChatScreen() { |
| 68 | + const { messages, sendMessage, isLoading } = useChat({ |
| 69 | + connection: fetchJSON("/api/chat"), |
| 70 | + }); |
| 71 | + |
| 72 | + // messages and isLoading behave identically to the streaming path — |
| 73 | + // they just update all at once when the request resolves. |
| 74 | + return <ChatUI messages={messages} onSend={sendMessage} busy={isLoading} />; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +`fetchJSON` accepts the same `url` + `options` signature as the other connection adapters (static string or function, headers, credentials, custom `fetchClient`, extra body, abort signal). It POSTs the usual `{ messages, data }` body, decodes the response as a `StreamChunk[]`, and replays each chunk into the normal `ChatClient` pipeline — tool calls, approvals, thinking content, errors all behave the same way they do with SSE. |
| 79 | + |
| 80 | +## Step 3: Expect no incremental rendering |
| 81 | + |
| 82 | +The one thing you give up: the UI won't update character-by-character. The request hangs until the server finishes the whole run, then the full message — including tool calls, results, and the final assistant turn — appears at once. |
| 83 | + |
| 84 | +If this becomes a problem, the answer is to move to a runtime that supports streaming responses (Hono on Node, Next.js, TanStack Start, a real SSE endpoint proxied through a CDN that doesn't buffer) rather than to work around the limitation further. The JSON-array path is a pragmatic escape hatch, not the intended happy path. |
| 85 | + |
| 86 | +## Going back to streaming when you can |
| 87 | + |
| 88 | +If you later deploy your server code to a runtime that *does* support streaming, you only need to change two call sites — `toJSONResponse` → `toServerSentEventsResponse` and `fetchJSON` → `fetchServerSentEvents`. Everything downstream (messages, tool calls, approvals, `useChat` state, error handling) is identical between the two paths, so there's no cleanup to chase through the app. |
| 89 | + |
| 90 | +## Next Steps |
| 91 | + |
| 92 | +- [Streaming](./streaming) — the normal incremental-rendering path |
| 93 | +- [Connection Adapters](./connection-adapters) — full list of client-side adapters, including `fetchJSON` |
| 94 | +- [API Reference: `toJSONResponse`](../api/ai#tojsonresponsestream-init) — server-side helper reference |
| 95 | +- [API Reference: `fetchJSON`](../api/ai-client#fetchjsonurl-options) — client-side adapter reference |
0 commit comments