From 22b8df6c47d769f01688fe596d5e3df83fea8d2b Mon Sep 17 00:00:00 2001 From: Joshua Martin Date: Tue, 24 Nov 2026 13:48:41 -0800 Subject: [PATCH 1/3] feat(ai-client): export StreamHttpError from the client and framework packages A failed HTTP stream connect throws StreamHttpError, which carries status and retryAfterMs. The class was not exported, so consumers could not discriminate between this and other errors via instanceof. This error is now exported from ai-react and the various client framework packages. --- .changeset/export-stream-http-error.md | 13 +++++++++++++ docs/config.json | 2 +- docs/transports/overview.md | 13 +++++++++++++ packages/ai-angular/src/index.ts | 1 + packages/ai-client/src/connection-adapters.ts | 2 +- packages/ai-client/src/index.ts | 1 + packages/ai-octane/src/index.ts | 1 + packages/ai-preact/src/index.ts | 1 + packages/ai-react/src/index.ts | 1 + packages/ai-remix/src/index.ts | 1 + packages/ai-solid/src/index.ts | 1 + packages/ai-svelte/src/index.ts | 1 + packages/ai-vue/src/index.ts | 1 + 13 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .changeset/export-stream-http-error.md diff --git a/.changeset/export-stream-http-error.md b/.changeset/export-stream-http-error.md new file mode 100644 index 0000000000..e3e24b271a --- /dev/null +++ b/.changeset/export-stream-http-error.md @@ -0,0 +1,13 @@ +--- +'@tanstack/ai-client': minor +'@tanstack/ai-react': minor +'@tanstack/ai-solid': minor +'@tanstack/ai-preact': minor +'@tanstack/ai-vue': minor +'@tanstack/ai-octane': minor +'@tanstack/ai-remix': minor +'@tanstack/ai-svelte': minor +'@tanstack/ai-angular': minor +--- + +Export `StreamHttpError` from `@tanstack/ai-client` and re-export it from the framework packages (`@tanstack/ai-react`, `@tanstack/ai-solid`, `@tanstack/ai-preact`, `@tanstack/ai-vue`, `@tanstack/ai-octane`, `@tanstack/ai-remix`, `@tanstack/ai-svelte`, `@tanstack/ai-angular`). A failed HTTP stream connect throws this class, which has `status` and `retryAfterMs`. Import it to check the status with `instanceof` instead of parsing the error message. diff --git a/docs/config.json b/docs/config.json index 6ab84c38bd..1902d0cf90 100644 --- a/docs/config.json +++ b/docs/config.json @@ -303,7 +303,7 @@ "label": "Overview", "to": "transports/overview", "addedAt": "2026-10-08", - "updatedAt": "2026-10-22" + "updatedAt": "2026-11-24" }, { "label": "Server-Sent Events", diff --git a/docs/transports/overview.md b/docs/transports/overview.md index 8f4f036650..043cb38b2c 100644 --- a/docs/transports/overview.md +++ b/docs/transports/overview.md @@ -152,6 +152,19 @@ export function ChatError() { If the connection drops in the middle of a line, the stream transports throw `StreamTruncatedError`. +A failed HTTP response throws `StreamHttpError`. Its `status` property is the HTTP status code. Its `retryAfterMs` property is the wait from the `Retry-After` header, in milliseconds. It is `undefined` when the server does not send that header. Import the class from `@tanstack/ai-client`, or from your framework package (for example `@tanstack/ai-react`), then check the error with `instanceof`: + +```ts +import { StreamHttpError } from "@tanstack/ai-client"; + +function describeError(err: unknown): string { + if (err instanceof StreamHttpError && err.status === 401) { + return "The session ended. Sign in again."; + } + return "The chat failed."; +} +``` + In a custom transport, do not catch and hide an `AbortError`. Let it propagate, so that the client knows that the abort worked. ### Rules for a custom transport diff --git a/packages/ai-angular/src/index.ts b/packages/ai-angular/src/index.ts index e4aee4f40f..554278bca9 100644 --- a/packages/ai-angular/src/index.ts +++ b/packages/ai-angular/src/index.ts @@ -110,6 +110,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, diff --git a/packages/ai-client/src/connection-adapters.ts b/packages/ai-client/src/connection-adapters.ts index 0ca2f26699..758d497400 100644 --- a/packages/ai-client/src/connection-adapters.ts +++ b/packages/ai-client/src/connection-adapters.ts @@ -107,7 +107,7 @@ const TEMPORARY_HTTP_STATUSES = new Set([408, 429, 502, 503, 504]) * A non-2xx response. A reconnect retries it when the status is temporary * (408, 429, 502, 503, 504), and waits for its `Retry-After` when present. */ -class StreamHttpError extends Error { +export class StreamHttpError extends Error { readonly retryAfterMs: number | undefined constructor( message: string, diff --git a/packages/ai-client/src/index.ts b/packages/ai-client/src/index.ts index 10b64faf7f..027b1abc8c 100644 --- a/packages/ai-client/src/index.ts +++ b/packages/ai-client/src/index.ts @@ -177,6 +177,7 @@ export { webSocket, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, createReconnectTracker, type ReconnectTracker, diff --git a/packages/ai-octane/src/index.ts b/packages/ai-octane/src/index.ts index f2608053a0..fe6a74544c 100644 --- a/packages/ai-octane/src/index.ts +++ b/packages/ai-octane/src/index.ts @@ -93,6 +93,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, diff --git a/packages/ai-preact/src/index.ts b/packages/ai-preact/src/index.ts index 974a9e6c98..0472144370 100644 --- a/packages/ai-preact/src/index.ts +++ b/packages/ai-preact/src/index.ts @@ -38,6 +38,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, diff --git a/packages/ai-react/src/index.ts b/packages/ai-react/src/index.ts index 4da6d8f4e8..2ed6d8cb8d 100644 --- a/packages/ai-react/src/index.ts +++ b/packages/ai-react/src/index.ts @@ -97,6 +97,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, diff --git a/packages/ai-remix/src/index.ts b/packages/ai-remix/src/index.ts index 7f41f6f7cd..ea78a2e4b2 100644 --- a/packages/ai-remix/src/index.ts +++ b/packages/ai-remix/src/index.ts @@ -91,6 +91,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, diff --git a/packages/ai-solid/src/index.ts b/packages/ai-solid/src/index.ts index 6cb65d9578..208034d334 100644 --- a/packages/ai-solid/src/index.ts +++ b/packages/ai-solid/src/index.ts @@ -90,6 +90,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, diff --git a/packages/ai-svelte/src/index.ts b/packages/ai-svelte/src/index.ts index b495d21ce1..1cb186c5e9 100644 --- a/packages/ai-svelte/src/index.ts +++ b/packages/ai-svelte/src/index.ts @@ -96,6 +96,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, diff --git a/packages/ai-vue/src/index.ts b/packages/ai-vue/src/index.ts index 80566fc629..49c6306310 100644 --- a/packages/ai-vue/src/index.ts +++ b/packages/ai-vue/src/index.ts @@ -90,6 +90,7 @@ export { createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, + StreamHttpError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, From fb3ecb0bde2df9b18099ead6b9f741adc9286144 Mon Sep 17 00:00:00 2001 From: Joshua Martin Date: Tue, 24 Nov 2026 14:29:56 -0800 Subject: [PATCH 2/3] docs: simplify the StreamHttpError note --- docs/transports/overview.md | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/docs/transports/overview.md b/docs/transports/overview.md index 043cb38b2c..d9ac6b1ce2 100644 --- a/docs/transports/overview.md +++ b/docs/transports/overview.md @@ -152,18 +152,7 @@ export function ChatError() { If the connection drops in the middle of a line, the stream transports throw `StreamTruncatedError`. -A failed HTTP response throws `StreamHttpError`. Its `status` property is the HTTP status code. Its `retryAfterMs` property is the wait from the `Retry-After` header, in milliseconds. It is `undefined` when the server does not send that header. Import the class from `@tanstack/ai-client`, or from your framework package (for example `@tanstack/ai-react`), then check the error with `instanceof`: - -```ts -import { StreamHttpError } from "@tanstack/ai-client"; - -function describeError(err: unknown): string { - if (err instanceof StreamHttpError && err.status === 401) { - return "The session ended. Sign in again."; - } - return "The chat failed."; -} -``` +A failed HTTP response can throw `StreamHttpError`. It has a `status` property with the HTTP status code, and a `retryAfterMs` property with the `Retry-After` delay in milliseconds. Import it from `@tanstack/ai-client`, or from your framework package, such as `@tanstack/ai-react`. In a custom transport, do not catch and hide an `AbortError`. Let it propagate, so that the client knows that the abort worked. From 7e514322340d08db254a19007492e561b8468e58 Mon Sep 17 00:00:00 2001 From: Tom Beckenham <34339192+tombeckenham@users.noreply.github.com> Date: Fri, 4 Dec 2026 13:15:45 +1100 Subject: [PATCH 3/3] docs(ai-client): document StreamHttpError in onError and the API reference Show the instanceof check in onError, where the instance arrives. The error state holds a plain Error. List the class in the API reference pages, tighten the changeset, and pin status and retryAfterMs with a test. --- .changeset/export-stream-http-error.md | 2 +- docs/api/ai-angular.md | 2 +- docs/api/ai-client.md | 1 + docs/api/ai-octane.md | 2 +- docs/api/ai-preact.md | 2 +- docs/api/ai-react.md | 2 +- docs/api/ai-remix.md | 2 +- docs/api/ai-solid.md | 2 +- docs/api/ai-svelte.md | 2 +- docs/api/ai-vue.md | 2 +- docs/config.json | 20 ++++++------ docs/transports/overview.md | 31 ++++++++++++++++++- .../tests/connection-adapters.test.ts | 28 ++++++++++++++++- 13 files changed, 77 insertions(+), 21 deletions(-) diff --git a/.changeset/export-stream-http-error.md b/.changeset/export-stream-http-error.md index e3e24b271a..e7f5c188fd 100644 --- a/.changeset/export-stream-http-error.md +++ b/.changeset/export-stream-http-error.md @@ -10,4 +10,4 @@ '@tanstack/ai-angular': minor --- -Export `StreamHttpError` from `@tanstack/ai-client` and re-export it from the framework packages (`@tanstack/ai-react`, `@tanstack/ai-solid`, `@tanstack/ai-preact`, `@tanstack/ai-vue`, `@tanstack/ai-octane`, `@tanstack/ai-remix`, `@tanstack/ai-svelte`, `@tanstack/ai-angular`). A failed HTTP stream connect throws this class, which has `status` and `retryAfterMs`. Import it to check the status with `instanceof` instead of parsing the error message. +Export `StreamHttpError` from `@tanstack/ai-client` and re-export it from the framework packages. The fetch and XHR chat transports throw this class when the response status is not 2xx. It has `status`, and `retryAfterMs` when the response has a valid `Retry-After` header. Import it to check the status with `instanceof` in `onError` instead of parsing the error message. diff --git a/docs/api/ai-angular.md b/docs/api/ai-angular.md index 78057c5706..cd2fc1570d 100644 --- a/docs/api/ai-angular.md +++ b/docs/api/ai-angular.md @@ -332,7 +332,7 @@ You can import these from `@tanstack/ai-angular` directly. See [`@tanstack/ai-cl | Persistence | `localStoragePersistence`, `sessionStoragePersistence`, `indexedDBPersistence` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP and MCP Apps | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools`, `createMcpAppBridge` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError`, `StorageUnavailableError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError`, `StorageUnavailableError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | BYOK helpers (`defineByok`, `defaultByokStorage`, and more) are on the `@tanstack/ai-angular/byok` subpath. diff --git a/docs/api/ai-client.md b/docs/api/ai-client.md index a6c94da9fd..cb3f25d13c 100644 --- a/docs/api/ai-client.md +++ b/docs/api/ai-client.md @@ -263,6 +263,7 @@ const connection = stream((_messages, _data, signal, input) => - `UnsupportedResponseStreamError`: a fetch adapter found no `Response.body`, `getReader()`, or `TextDecoder`. - `StreamTruncatedError`: an SSE or NDJSON stream ended in the middle of a line. A server, a proxy, or the network cut the connection. +- `StreamHttpError`: a fetch or XHR adapter got a response with a status that is not 2xx. It has `status` and `retryAfterMs`. See [Transports](../transports/overview). - `DurableStreamIncompleteError` and `StreamReconnectLimitError`: a resumable stream did not finish. See [Resumable Streams](../resumable-streams/overview). ## Persistence adapters diff --git a/docs/api/ai-octane.md b/docs/api/ai-octane.md index 753af1378b..342b5dd767 100644 --- a/docs/api/ai-octane.md +++ b/docs/api/ai-octane.md @@ -315,7 +315,7 @@ You can import these from `@tanstack/ai-octane` directly. See [`@tanstack/ai-cli | Typed options | `createChatClientOptions`, `clientTools` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP and MCP Apps | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools`, `createMcpAppBridge` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | The browser persistence adapters (`localStoragePersistence` and the others) are not re-exported here. Import them from `@tanstack/ai-client`. diff --git a/docs/api/ai-preact.md b/docs/api/ai-preact.md index 6ebced8e1a..af0525d33b 100644 --- a/docs/api/ai-preact.md +++ b/docs/api/ai-preact.md @@ -269,7 +269,7 @@ You can import these from `@tanstack/ai-preact` directly. See [`@tanstack/ai-cli | Persistence | `localStoragePersistence`, `sessionStoragePersistence`, `indexedDBPersistence` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP and MCP Apps | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools`, `createMcpAppBridge` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError`, `StorageUnavailableError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError`, `StorageUnavailableError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | BYOK helpers (`defineByok`, `defaultByokStorage`, and more) are on the `@tanstack/ai-preact/byok` subpath. diff --git a/docs/api/ai-react.md b/docs/api/ai-react.md index 18c8ad754f..f3c2e1fbca 100644 --- a/docs/api/ai-react.md +++ b/docs/api/ai-react.md @@ -367,7 +367,7 @@ You can import these from `@tanstack/ai-react` directly. See [`@tanstack/ai-clie | Persistence | `localStoragePersistence`, `sessionStoragePersistence`, `indexedDBPersistence` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP and MCP Apps | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools`, `createMcpAppBridge` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError`, `StorageUnavailableError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError`, `StorageUnavailableError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | For React Native or Expo, use an absolute server URL and `xhrHttpStream()` with a route that returns `toHttpResponse()`. If the host cannot stream, use `fetchJson()` with `toJsonResponse()`. See [React Native](../transports/react-native). diff --git a/docs/api/ai-remix.md b/docs/api/ai-remix.md index 00de407d47..a2be8ef02b 100644 --- a/docs/api/ai-remix.md +++ b/docs/api/ai-remix.md @@ -382,7 +382,7 @@ You can import these from `@tanstack/ai-remix` directly. See [`@tanstack/ai-clie | Typed options | `createChatClientOptions`, `clientTools` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | The browser persistence adapters (`localStoragePersistence` and the others) are not re-exported here. Import them from `@tanstack/ai-client`. BYOK helpers (`defineByok`, `defaultByokStorage`, and more) are on the `@tanstack/ai-remix/byok` subpath. diff --git a/docs/api/ai-solid.md b/docs/api/ai-solid.md index 9ddac5fc29..91d96c404c 100644 --- a/docs/api/ai-solid.md +++ b/docs/api/ai-solid.md @@ -316,7 +316,7 @@ You can import these from `@tanstack/ai-solid` directly. See [`@tanstack/ai-clie | Persistence | `localStoragePersistence`, `sessionStoragePersistence`, `indexedDBPersistence` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP and MCP Apps | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools`, `createMcpAppBridge` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError`, `StorageUnavailableError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError`, `StorageUnavailableError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | BYOK helpers (`defineByok`, `defaultByokStorage`, and more) are on the `@tanstack/ai-solid/byok` subpath. diff --git a/docs/api/ai-svelte.md b/docs/api/ai-svelte.md index ab73abbb31..110d646ccd 100644 --- a/docs/api/ai-svelte.md +++ b/docs/api/ai-svelte.md @@ -290,7 +290,7 @@ You can import these from `@tanstack/ai-svelte` directly. See [`@tanstack/ai-cli | Persistence | `localStoragePersistence`, `sessionStoragePersistence`, `indexedDBPersistence` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP and MCP Apps | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools`, `createMcpAppBridge` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError`, `StorageUnavailableError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError`, `StorageUnavailableError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | BYOK helpers (`defineByok`, `defaultByokStorage`, and more) are on the `@tanstack/ai-svelte/byok` subpath. diff --git a/docs/api/ai-vue.md b/docs/api/ai-vue.md index 53e6d08086..bca2fe6ea7 100644 --- a/docs/api/ai-vue.md +++ b/docs/api/ai-vue.md @@ -286,7 +286,7 @@ You can import these from `@tanstack/ai-vue` directly. See [`@tanstack/ai-client | Persistence | `localStoragePersistence`, `sessionStoragePersistence`, `indexedDBPersistence` | | Clients | `ChatClient`, `GenerationClient`, `VideoGenerationClient`, `RealtimeClient`, `AudioRecorder`, `InterruptManager` | | WebMCP and MCP Apps | `registerWebMCPTools`, `getWebMCPTools`, `subscribeWebMCPTools`, `createMcpAppBridge` | -| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamReconnectLimitError`, `StorageUnavailableError` | +| Errors | `UnsupportedResponseStreamError`, `StreamTruncatedError`, `DurableStreamIncompleteError`, `StreamHttpError`, `StreamReconnectLimitError`, `StorageUnavailableError` | | Stream helpers | `StreamProcessor`, chunk strategies, `parsePartialJSON`, message converters | BYOK helpers (`defineByok`, `defaultByokStorage`, and more) are on the `@tanstack/ai-vue/byok` subpath. diff --git a/docs/config.json b/docs/config.json index 1902d0cf90..a04986c977 100644 --- a/docs/config.json +++ b/docs/config.json @@ -303,7 +303,7 @@ "label": "Overview", "to": "transports/overview", "addedAt": "2026-10-08", - "updatedAt": "2026-11-24" + "updatedAt": "2026-12-04" }, { "label": "Server-Sent Events", @@ -2224,55 +2224,55 @@ "label": "@tanstack/ai-client", "to": "api/ai-client", "addedAt": "2026-04-15", - "updatedAt": "2026-11-24" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-react", "to": "api/ai-react", "addedAt": "2026-04-15", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-solid", "to": "api/ai-solid", "addedAt": "2026-04-15", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-preact", "to": "api/ai-preact", "addedAt": "2026-04-15", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-vue", "to": "api/ai-vue", "addedAt": "2026-04-15", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-svelte", "to": "api/ai-svelte", "addedAt": "2026-04-15", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-angular", "to": "api/ai-angular", "addedAt": "2026-06-15", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-octane", "to": "api/ai-octane", "addedAt": "2026-08-21", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-remix", "to": "api/ai-remix", "addedAt": "2026-09-01", - "updatedAt": "2026-11-21" + "updatedAt": "2026-12-04" }, { "label": "@tanstack/ai-effect", diff --git a/docs/transports/overview.md b/docs/transports/overview.md index d9ac6b1ce2..0bba47fc85 100644 --- a/docs/transports/overview.md +++ b/docs/transports/overview.md @@ -152,7 +152,36 @@ export function ChatError() { If the connection drops in the middle of a line, the stream transports throw `StreamTruncatedError`. -A failed HTTP response can throw `StreamHttpError`. It has a `status` property with the HTTP status code, and a `retryAfterMs` property with the `Retry-After` delay in milliseconds. Import it from `@tanstack/ai-client`, or from your framework package, such as `@tanstack/ai-react`. +When the response status is not 2xx, the fetch and XHR transports throw `StreamHttpError`. Check for it in `onError`: + +```tsx +import { + useChat, + fetchServerSentEvents, + StreamHttpError, +} from "@tanstack/ai-react"; + +export function ChatRateLimit() { + const { error } = useChat({ + connection: fetchServerSentEvents("/api/chat"), + threadId: "support-chat", + onError: (err) => { + if (err instanceof StreamHttpError && err.status === 429) { + console.warn("Rate limited. Retry in", err.retryAfterMs, "ms"); + } + }, + }); + + return error ?

{error.message}

: null; +} +``` + +- `status`: the HTTP status code. +- `retryAfterMs`: the `Retry-After` delay in milliseconds. It is `undefined` when the response has no valid `Retry-After` header. + +The `error` state holds a plain `Error` with the same message. Do the `instanceof` check in `onError`. + +You can also import `StreamHttpError` from `@tanstack/ai-client`. In a custom transport, do not catch and hide an `AbortError`. Let it propagate, so that the client knows that the abort worked. diff --git a/packages/ai-client/tests/connection-adapters.test.ts b/packages/ai-client/tests/connection-adapters.test.ts index e8b467aef4..a5358a4f24 100644 --- a/packages/ai-client/tests/connection-adapters.test.ts +++ b/packages/ai-client/tests/connection-adapters.test.ts @@ -9,7 +9,7 @@ import { rpcStream, stream, } from '../src/connection-adapters' -import { UnsupportedResponseStreamError } from '../src' +import { StreamHttpError, UnsupportedResponseStreamError } from '../src' import { ChatClient } from '../src/chat-client' import { createUIMessage } from './test-utils' import type { RunAgentInput, StreamChunk } from '@tanstack/ai/client' @@ -377,6 +377,32 @@ describe('connection-adapters', () => { ).rejects.toThrow('HTTP error! status: 500 Internal Server Error') }) + it('should throw a StreamHttpError with status and retryAfterMs', async () => { + fetchMock.mockResolvedValue({ + ok: false, + status: 429, + statusText: 'Too Many Requests', + headers: new Headers({ 'retry-after': '2' }), + } as any) + + const run = (async () => { + for await (const _ of fetchServerSentEvents('/api/chat').connect( + [{ role: 'user', content: 'Hello' }], + undefined, + undefined, + RUN_CONTEXT, + )) { + // Consume + } + })() + + await expect(run).rejects.toBeInstanceOf(StreamHttpError) + await expect(run).rejects.toMatchObject({ + status: 429, + retryAfterMs: 2000, + }) + }) + it('should handle missing response body', async () => { const mockResponse = { ok: true,