openai/openai-node

NextJS navigator is not defined - azure v4 stream true

UXDart opened this issue · 8 comments

Describe the bug

using last v4 server side, there is an error when calling for example chat.create

here

if (!navigator || typeof navigator === 'undefined') {

you can see code:
if (!navigator || typeof navigator === 'undefined') {

it is checking if it is undefined "after" trying to use it with !navigator

To Reproduce

calling chat.completions.create with stream true. but using it on Azure from this sample

// The name of your Azure OpenAI Resource.

Code snippets

No response

OS

macOS

Node version

node 18

Library version

v4.0.0

Hi @UXDart , thanks for reporting this. You list your Node version as 18; can you share more details of your environment?

A preceding call should have early-returned, and I'm surprised it didn't:

if (Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]') {

What does Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) return in your system?

Hi, oh I see, did a few tests, it returns "[object Object]" when doing

console.log(Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0))

and

console.log(typeof process)
returns

"object"

this is NextJS, MacOS, M1, "running local" with "npm run dev" / node 18.16.0

HTH

did one more test... console log "process.constructor.name" returns "Object". using Firebase also, maybe another package is doing a change somewhere?

ok the problem seems to be "sass" package. or at least that one is setting process. anyone else is using postcss+sass in nextjs and openai package? thanks

sorry to spam this thread... sass is not the reason, I can see sass is setting a new process, but it has name = 'process'. this is happening inside a route in nextjs (where name is not 'process'). there is no other package or anything setting process var again that I can see / nvm! one file was adding a polyfill

Interesting… thank you for sharing these details; we'd like to get to the bottom of this. Could you share your next config, the contents of the file which does this, and your package.json?

sure, I think nothing needs to be changed IMO but the package was node-polyfill-webpack-plugin I have no idea what other package needed it... but when using it "process" was an Object. who knows which other problems was causing it. I removed it :) Thanks! everything works now

Ah, that makes sense – thank you for sharing, and I'm glad you were able to fix on your end. I've filed Richienb/node-polyfill-webpack-plugin#44 with that project accordingly. We have also fixed this bug in this library, and expect to have it released in the next day or two.

Out of curiosity, do you remember why installed that package?