mastra-ai/mastra · error

No response body for agent controller session stream

Error message

No response body for agent controller session stream

What it means

In agent-controller.ts requestStream(), the client POSTs to the agent controller session /stream endpoint with stream:true and throws 'No response body for agent controller session stream' when response.body is missing, because the caller needs a ReadableStream to process the session stream. It is invoked by firstResponse and run, and the surrounding code also sets up reconnect handling for dropped streams.

Source

Thrown at client-sdks/client-js/src/resources/agent-controller.ts:383

      if (reconnectTimer) {
        clearTimeout(reconnectTimer);
        reconnectTimer = undefined;
      }
      const resolve = delayResolve;
      delayResolve = undefined;
      resolve?.();
    };

    const delay = (ms: number) =>
      new Promise<void>(resolve => {
        delayResolve = resolve;
        reconnectTimer = setTimeout(settleDelay, ms);
      });

    const requestStream = async (): Promise<Response> => {
      const response = (await this.request(this.url(`${this.base()}/stream`), { stream: true })) as Response;
      if (!response.body) {
        throw new Error('No response body for agent controller session stream');
      }
      return response;
    };

    const streamEndedError = () => new Error('Agent controller session stream ended unexpectedly');

    const findFrameSeparator = (text: string): { index: number; length: number } | null => {
      const candidates = [
        { index: text.indexOf('\r\n\r\n'), length: 4 },
        { index: text.indexOf('\n\n'), length: 2 },
        { index: text.indexOf('\r\r'), length: 2 },
      ].filter(candidate => candidate.index !== -1);
      if (candidates.length === 0) return null;
      return candidates.reduce((earliest, candidate) => (candidate.index < earliest.index ? candidate : earliest));
    };

    type PumpResult =
      | { kind: 'done' }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Confirm the server streams the session endpoint (curl -N).
  2. Fix fetch mocks to include a ReadableStream body with the expected encoded chunks.
  3. Use a runtime with native streaming fetch support; avoid polyfills for this code path.
  4. Check intermediary proxies/load balancers are not buffering or emptying streaming responses.
  5. Rely on the existing reconnect logic: catch the error and let the controller retry the stream.

Example fix

// before
const res = await fetch(url, { method: 'POST' }); // mock without body
// after
const res = new Response(new ReadableStream({ start(c) { c.enqueue(encoder.encode('data: {...}\n\n')); c.close(); } }), { status: 200 });
Defensive patterns

Strategy: try-catch

Validate before calling

const probe = await fetch(`${base}/stream`, { method: 'POST' });
if (probe.ok && !probe.body) throw new Error('Controller /stream returns no body in this environment');

Type guard

function hasBody(res: Response): res is Response & { body: ReadableStream<Uint8Array> } { return res.body != null; }

Try / catch

try {
  await controller.run(params);
} catch (err) {
  if ((err as Error).message.startsWith('No response body for agent controller session stream')) {
    console.error('Session stream had no body; relying on controller reconnect');
  }
  throw err;
}

Prevention

When it happens

Trigger: GET/POST to {base}/stream returns ok but no body: fetch mocks, buffering proxies, runtimes lacking streaming body support, or server error responses swallowed into a bodyless response.

Common situations: Testing the agent controller with mocked fetch lacking a body; reverse proxies or serverless platforms that don't pass through SSE/chunked streams; Node/runtime fetch polyfills without ReadableStream response bodies.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/fbb848d7403ea151. Report an issue: GitHub.