chat.completions.create return null on browser
Problem
Confirm this is a Node library issue and not an underlying OpenAI API issue - [X] This is an issue with the Node library Describe the bug While developing an web app on localhost Works like a charm with 3.6 but after upgrading to v4 and adapating the code chat.completions.create return always null with dangerouslyAllowBrowser: true Works well in back with the same code To Reproduce 1. Use demo code in browser 2. Add dangerouslyAllowBrowser to initiator option 3. Run and get error Code snippets _No response_ OS macOS Node version Node v18.16 Library version openai v4
Error Output
error ### Code snippets
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix chat.completions.create returning null in Node library v4
The issue arises due to changes in the OpenAI Node library v4 that affect how the 'dangerouslyAllowBrowser' option interacts with the API when called from a browser environment. This option may not be fully compatible with the new library version, leading to null responses.
Awaiting Verification
Be the first to verify this fix
- 1
Check OpenAI API Key Configuration
Ensure that the OpenAI API key is correctly set up in your environment variables or configuration file. The library may return null if the API key is missing or invalid.
javascriptconst openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); - 2
Revert to Previous Library Version
If the issue persists, consider reverting to the previous version (v3.6) of the OpenAI library where the functionality was confirmed to work. This can be done using npm.
bashnpm install openai@3.6 - 3
Update API Call Syntax
Review and update the API call syntax to ensure compatibility with v4. The method of calling chat.completions.create may have changed, requiring adjustments in parameters or structure.
javascriptconst response = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'user', content: 'Hello!' }], dangerouslyAllowBrowser: true }); - 4
Test in Different Environments
Run the application in different environments (e.g., local server vs. production) to determine if the issue is environment-specific. This can help isolate the problem.
- 5
Consult OpenAI Documentation
Review the latest OpenAI API documentation for any updates or breaking changes related to the Node library v4. This may provide insights into the new requirements or configurations needed.
Validation
Confirm the fix by running the application again and checking if chat.completions.create returns a valid response instead of null. Additionally, verify that the API key is valid and that the updated code adheres to the latest library specifications.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep