vercel/ai · error · Error
The response body is empty.
Error message
The response body is empty.
What it means
The Svelte structured-object client checks response.body after a successful status; if the response has no readable stream body it throws 'The response body is empty.'. The library expects a streamed JSON response to incrementally parse into a partial object.
Source
Thrown at packages/svelte/src/structured-object.svelte.ts:178
const response = await actualFetch(this.#options.api, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...this.#options.headers,
},
credentials: this.#options.credentials,
signal: abortController.signal,
body: JSON.stringify(input),
});
if (!response.ok) {
throw new Error(
(await response.text()) ?? 'Failed to fetch the response.',
);
}
if (response.body == null) {
throw new Error('The response body is empty.');
}
let accumulatedText = '';
let latestObject: DeepPartial<RESULT> | undefined = undefined;
await response.body.pipeThrough(new TextDecoderStream()).pipeTo(
new WritableStream<string>({
write: async chunk => {
if (abortController?.signal.aborted) {
throw new DOMException('Stream aborted', 'AbortError');
}
accumulatedText += chunk;
const { value } = await parsePartialJson(accumulatedText);
const currentObject = value as DeepPartial<RESULT>;
if (!isDeepEqualData(latestObject, currentObject)) {
latestObject = currentObject;View on GitHub (pinned to 69428b1f8b)
Solutions
- Make the API endpoint return a streamed body (200 with content).
- Check for 204/redirect responses from your route and fix the handler.
- Run in a runtime that supports streaming Response.body (modern browsers/Node 18+).
- Add a server-side check that the structured-generation handler actually streams the result.
Example fix
// before (endpoint)
return new Response(null, { status: 204 });
// after
return new Response(JSON.stringify(resultStream), { status: 200 }); Defensive patterns
Strategy: validation
Validate before calling
// server-side guard
export function POST() {
const body = buildStructuredStream();
if (!body) return new Response('no body', { status: 500 });
return new Response(body, { status: 200 });
} Type guard
null
Try / catch
try {
startGeneration();
} catch (e) {
if (e?.message === 'The response body is empty.') {
console.error('Endpoint returned no stream body — check the API handler.');
} else throw e;
} Prevention
- Ensure the endpoint returns 200 with a body.
- Avoid 204/empty responses from generation routes.
- Test the route with curl to confirm a streamed body.
- Use a runtime supporting streaming Response.body.
When it happens
Trigger: The fetch resolves with !response.ok false but response.body === null — e.g. HTTP 204 responses, opaque/no-store contexts, or environments without streaming response bodies (some runtimes/polyfills).
Common situations: Endpoint returns 204 No Content or an empty-body redirect; running in an environment where Response.body is not implemented; a misconfigured proxy that strips the body.
Related errors
- Failed to fetch the response.
- Failed to fetch the response.
- The response body is empty.
- AbortError
- 'element streams in no-schema mode' functionality not suppor
AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30).
Data as JSON: /api/errors/70fea231bb638ee8.
Report an issue: GitHub.