TypeError: messageList.map is not a function

I have a .cjs file that, when run with node, returns all urls it is crawling(that part works fine) but has a few errors I need to fix.

Here are two errors of the same kind:

    Error generating and outputting text: TypeError: messageList.map is not a function                                                                             
        at /home/legomyego/scraping/openai/node_modules/@langchain/core/dist/language_models/                                chat_models.cjs:243:72                                                
    at Array.map (<anonymous>)                                                                                                                                 
    at ChatOpenAI.generate (/home/legomyego/scraping/openai/node_modules/@langchain/core/dist/        language_models/chat_models.cjs:243:39)                          
    at generateAndOutputText (/home/legomyego/scraping/openai/aiScrape6.cjs:60:41)                                                                             
    at crawl (/home/legomyego/scraping/openai/aiScrape6.cjs:37:15)                                                                                             
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)                                                                              
    at async /home/legomyego/scraping/openai/aiScrape6.cjs:85:9                                                                                                                                                                                                                                                         
    Error generating and outputting text: TypeError: messageList.map is not a function                                                                             
    at /home/legomyego/scraping/openai/node_modules/@langchain/core/dist/language_models/    chat_models.cjs:243:72                                                
    at Array.map (<anonymous>)                                                                                                                                 
    at ChatOpenAI.generate (/home/legomyego/scraping/openai/node_modules/@langchain/core/dist/     language_models/chat_models.cjs:243:39)                          
    at generateAndOutputText (/home/legomyego/scraping/openai/aiScrape6.cjs:60:41)                                                                             
    at CsvParserStream.<anonymous> (/home/legomyego/scraping/openai/aiScrape6.cjs:95:23)                                                                       
    at CsvParserStream.emit (node:events:514:28)                                                                                                               
    at CsvParserStream.emit (/home/legomyego/scraping/openai/node_modules/@fast-csv/parse/build/                               src/CsvParserStream.js:50:23)                                  
    at endReadableNT (node:internal/streams/readable:1376:12)                                                                                                  
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)   

I think that this function below is the reason for the error. This is because “messages” is of type “object” and not an array before sent to ‘const generatedText’. However changing it to an array did not work.
Here is the function:


     async function generateAndOutputText(userPrompt, context) {
    try {
        const messages = [
            { role: "system", content: "You are a helpful assistant." },
            { role: "user", content: userPrompt },
            { role: "assistant", content: context },
        ];
    console.log(typeof messages);
        const generatedText = await llm.generate(messages);

        console.log(generatedText);

        // Save generated text to a CSV file
        fs.writeFile('generated_text.csv', generatedText, (err) => {
            if (err) {
                console.error('Error saving generated text to file:', err);
            } else {
                console.log('Generated text saved to generated_text.csv');
            }
        });
    } catch (error) {
        console.error('Error generating and outputting text:', error);
    }
    }

Finally here is a pastebin of the full code: pastebin(.)com/RGDhqLdW