vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.02k stars 1.49k forks source link

`Error: failed to pipe response` when trying to respond with `fullStream` #1619

Closed baptisteArno closed 5 months ago

baptisteArno commented 5 months ago

Description

I am failing to send fullStream in a Next.js function response. This throws a server error:

  ⨯ Error: failed to pipe response
     at pipeToNodeResponse (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/pipe-readable.js:111:15)
     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
     at async sendResponse (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/send-response.js:40:13)
     at async doRender (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1363:25)
     at async cacheEntry.responseCache.get.routeKind (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1543:40)
     at async DevServer.renderToResponseWithComponentsImpl (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1463:28)
     at async DevServer.renderPageComponent (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1856:24)
     at async DevServer.renderToResponseImpl (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:1894:32)
     at async DevServer.pipeImpl (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:911:25)
     at async NextNodeServer.handleCatchallRenderRequest (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/next-server.js:271:17)
     at async DevServer.handleRequestImpl (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/base-server.js:807:17)
     at async /Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/dev/next-dev-server.js:331:20
     at async Span.traceAsyncFn (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/trace/trace.js:151:20)
     at async DevServer.handleRequest (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/dev/next-dev-server.js:328:24)
     at async invokeRender (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/lib/router-server.js:163:21) {
   [cause]: Error: failed to write chunk to response
       at Object.write (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/pipe-readable.js:75:23)
       at ensureIsPromise (node:internal/webstreams/util:185:19)
       ... 9 lines matching cause stack trace ...
       at Object.transform (webpack-internal:///(rsc)/../../node_modules/.pnpm/ai@3.1.9_openai@4.38.3_react@18.2.0_solid-js@1.7.8_svelte@4.2.12_vue@3.4.21_zod@3.22.4/node_modules/ai/dist/index.mjs:1616:36)
       at ensureIsPromise (node:internal/webstreams/util:185:19)
       at transformStreamDefaultControllerPerformTransform (node:internal/webstreams/transformstream:505:18)
       at transformStreamDefaultSinkWriteAlgorithm (node:internal/webstreams/transformstream:555:10) {
     [cause]: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object
         at write_ (node:_http_outgoing:905:11)
         at ServerResponse.write (node:_http_outgoing:864:15)
         at ServerResponse.write (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/compression/index.js:22:610)
         at Object.write (/Users/baptistearno/Dev/typebot.io/node_modules/.pnpm/next@14.1.0_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/pipe-readable.js:60:32)
         at ensureIsPromise (node:internal/webstreams/util:185:19)
         at writableStreamDefaultControllerProcessWrite (node:internal/webstreams/writablestream:1111:5)
         at writableStreamDefaultControllerAdvanceQueueIfNeeded (node:internal/webstreams/writablestream:1226:5)
         at writableStreamDefaultControllerWrite (node:internal/webstreams/writablestream:1100:3)
         at writableStreamDefaultWriterWrite (node:internal/webstreams/writablestream:990:3)
         at [kChunk] (node:internal/webstreams/readablestream:1553:31)
         at readableStreamFulfillReadRequest (node:internal/webstreams/readablestream:2095:24)
         at readableStreamDefaultControllerEnqueue (node:internal/webstreams/readablestream:2286:5)
         at transformStreamDefaultControllerEnqueue (node:internal/webstreams/transformstream:486:5)
         at TransformStreamDefaultController.enqueue (node:internal/webstreams/transformstream:306:5)
         at Object.transform (webpack-internal:///(rsc)/../../node_modules/.pnpm/ai@3.1.9_openai@4.38.3_react@18.2.0_solid-js@1.7.8_svelte@4.2.12_vue@3.4.21_zod@3.22.4/node_modules/ai/dist/index.mjs:1616:36) {
       code: 'ERR_INVALID_ARG_TYPE'
     }
   }
 }

Code example

export async function POST(req: Request) {
  const { fullStream } = await streamText({
    /**/
  })

  return new StreamingTextResponse(fullStream, {
    headers: responseHeaders,
  })
}

Additional context

Tried with @ai-sdk/mistral version 0.0.11. ai is version 3.1.9

lgrammel commented 5 months ago

fullStream is not intended to be used that way. Please use .toAIStreamResponse() instead:

  const result = await streamText({
    /**/
  });

  return result.toAIStreamResponse( {
    headers: responseHeaders,
  });
}