diff --git a/.changeset/export-stream-http-error.md b/.changeset/export-stream-http-error.md new file mode 100644 index 0000000000..e7f5c188fd --- /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. 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 6ab84c38bd..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-10-22" + "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 8f4f036650..0bba47fc85 100644 --- a/docs/transports/overview.md +++ b/docs/transports/overview.md @@ -152,6 +152,37 @@ export function ChatError() { If the connection drops in the middle of a line, the stream transports throw `StreamTruncatedError`. +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. ### 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-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, 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,