-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Confirm this is a Node library issue and not an underlying OpenAI API issue
- This is an issue with the Node library
Describe the bug
The documentation for the "message" event on the runner returned by openai.beta.chat.completions.runTools() says "The event fired when a new message is either sent or received from the API. Does not fire for the messages sent as the parameter to either .runTools() or .stream()". This works correctly if you pass stream: true in the options to runTools(), but if you don't pass stream: true, the runner always emits "message" events for all of the messages including those passed in as parameters.
This is an issue for me because I want to record all the messages (including tool calls and tool results) generated by a runTools() call and add them to the list of existing messages so that they're visible to GPT when processing future messages from the user, and from the docs it looks like the "message" events are the proper way to do this, but because of this issue it seems like there's no clean way to do what I need unless I pass in stream: true, which doesn't seem like it's meant to affect this behavior. (I have no issue with using stream: true but it took me a while to think to try it.)
I initially wondered if this bug was tied to how I was using runTools(), but I run into this same exact issue with the in-repo example at https://github.com/openai/openai-node/blob/master/examples/function-call-helpers.ts (okay that uses the older runFunctions() method instead but I'm assuming it's meant to work the same).
To Reproduce
- Call
openai.beta.chat.completions.runTools()with one or more messages, without specifyingstream: true. - Add a "message" event listener on the returned runner.
- Observe that it emits events for all the messages including the ones passed in originally, not just the messages generated by the
runTools()call. - Change the
runTools()call to set thestream: trueoption and see that it correctly emits "message" events only for new messages.
Code snippets
import OpenAI from "openai";
const openai = new OpenAI();
const runner = openai.beta.chat.completions.runTools({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a helpful assistant.",
},
{
role: "user",
content: "Call the log function with an ice cream flavor please.",
},
],
tools: [
{
type: "function",
function: {
name: "log",
description: "Logs the input",
function(args) {
console.log("logged args", args);
},
parameters: {
type: "object",
properties: {
text: { type: "string" },
},
},
},
},
],
});
runner.on("message", (message) => {
console.log("runner.on message", message);
});
await runner.done();OS
Windows
Node version
Node v21.7.3, Deno 1.45.5
Library version
openai v4.55.7