Error creating thread with Bubble API via plugin

This requires js knowledge but asking here because I have this same code working in a nodejs console app on my computer, but for some reason it’s giving me an error when trying to run from a Bubble plugin.

Here’s the code:

async function(properties, context) {
	const OpenAI = require('openai');
    const fs = require('fs');
    const { Readable } = require('stream');
    
    const fileID = properties.selection_id
    
    
    try {
        
        const openai = new OpenAI({ "apiKey": context.keys["OPENAI API KEY"] });
        
        const thread = await openai.beta.threads.create();
        
        return {
            success: true,
            message: "Created empty thread"
        }
  } catch (err) {
        return {
            success: false,
            message: "Message: " + err
        }
    }
}

The error happens on the openai.beta.threads.create():

TypeError: Cannot read properties of undefined (reading 'create')

I’m fairly certain openai is defined at that point since I’ve run a test, and I’m not sure what else could be undefined. Also not sure why this would give an error in Bubble and not in nodejs locally.

Any ideas what the issue could be?

are you sure the package installed by bubble for openai is the latest version? it’s always better to specify the dependency file manually because when bubble does it automatically is usually unreliable. also double check the dependencies quite often because even if you specify them bubble likes to overwrite them with its own automatic version

I thought it was the latest version, since the package.json said "openai": "latest" but after your comment I entered the version number on the npm page "openai": "4.62.1" and it’s working now!

1 Like