ai icon indicating copy to clipboard operation
ai copied to clipboard

Remix – ReadableStream type mismatch: The "transform.readable" property must be an instance of ReadableStream. Received an instance of ReadableStream

Open Klingefjord opened this issue 1 year ago • 10 comments

I am using Remix (2.0.1) and Vercel AI (2.2.13).

I am getting this error when submitting a chat message in production (deployed on Vercel using the Remix template).

The error does not occur locally:

{ url: '/chat/completions' }
TypeError [ERR_INVALID_ARG_TYPE]: The "transform.readable" property must be an instance of ReadableStream. Received an instance of ReadableStream
    at new NodeError (node:internal/errors:405:5)
    at ReadableStream.pipeThrough (node:internal/webstreams/readablestream:363:13)
    at AIStream (/var/task/node_modules/ai/dist/index.js:139:29)
    at OpenAIStream (/var/task/node_modules/ai/dist/index.js:360:14)
    at action4 (/var/task/build/build-nodejs-eyJydW50aW1lIjoibm9kZWpzIn0.js:12965:73)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.callRouteActionRR (/var/task/node_modules/@remix-run/server-runtime/dist/data.js:35:16)
    at async callLoaderOrAction (/var/task/node_modules/@remix-run/router/dist/router.cjs.js:3903:16)
    at async submit (/var/task/node_modules/@remix-run/router/dist/router.cjs.js:3275:16)
    at async queryImpl (/var/task/node_modules/@remix-run/router/dist/router.cjs.js:3233:22) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Code to replicate:

/api/chat

import { ChatCompletionRequestMessage, OpenAIApi, Configuration } from "openai-edge"
import {
  OpenAIStream,
  StreamingTextResponse,
} from "ai"

export const action: ActionFunction = async ({
  request,
}: ActionFunctionArgs): Promise<Response> => {
  const json = await request.json()
  const { messages } = json
  const openai = new OpenAIApi(
     new Configuration({
       apiKey: xxxx
     })
  )
  const response = await openai.createChatCompletion({
      model: "gpt-3.5-turbo",
      messages: messages,
      temperature: 0.7,
      stream: true,
    })
  const stream = OpenAIStream(response)
  return new StreamingTextResponse(stream)
}

Klingefjord avatar Sep 27 '23 05:09 Klingefjord

Also added here: https://github.com/remix-run/remix/issues/7547

Klingefjord avatar Sep 27 '23 06:09 Klingefjord

Actually, probably not a problem with AI package, nor Remix, but the remix build tooling from Vercel.

https://github.com/vercel/remix/issues/60

Klingefjord avatar Oct 03 '23 22:10 Klingefjord

Are you using node 18+?

MaxLeiter avatar Oct 04 '23 22:10 MaxLeiter

Are you using node 18+?

Yes, 18.7

Klingefjord avatar Oct 06 '23 09:10 Klingefjord

Is this resolved? https://github.com/vercel/remix/issues/60 and https://github.com/vercel/remix/pull/56 seems to have resolved it.

jacobhjkim avatar Nov 12 '23 09:11 jacobhjkim

vercel/remix#60

Nope, still an issue! Tried upgrading to latest remix and vercel ai, deployed anew on vercel, still the same error:

TypeError [ERR_INVALID_ARG_TYPE]: The "transform.readable" property must be an instance of ReadableStream. Received an instance of ReadableStream

Klingefjord avatar Nov 13 '23 17:11 Klingefjord

Hey @Klingefjord, could you please create a reproduction so we can check this out?

MaxLeiter avatar Nov 13 '23 20:11 MaxLeiter

Probably related: https://github.com/vercel/remix/issues/62

maxfi avatar Apr 07 '24 05:04 maxfi

Omitting the call to installGlobals resolved this locally for me. That function is supposed to be the solution to these problems by polyfilling the fetch standard, but seems to have the opposite effect here.

peterje avatar Apr 23 '24 10:04 peterje

is this issue resolved?? facing same

nsijwali avatar May 14 '24 18:05 nsijwali

@nsijwali did you try removing installGlobals? I'm closing this as it seems like a Remix-related bug more than an AI SDK one.

MaxLeiter avatar Jun 11 '24 00:06 MaxLeiter