vercel / ai

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

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

Closed Klingefjord closed 3 months ago

Klingefjord commented 11 months ago

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 commented 11 months ago

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

Klingefjord commented 11 months ago

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

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

MaxLeiter commented 11 months ago

Are you using node 18+?

Klingefjord commented 11 months ago

Are you using node 18+?

Yes, 18.7

jacobhjkim commented 10 months ago

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

Klingefjord commented 10 months ago

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
MaxLeiter commented 10 months ago

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

maxfi commented 5 months ago

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

peterje commented 4 months ago

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.

nsijwali commented 4 months ago

is this issue resolved?? facing same

MaxLeiter commented 3 months ago

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