Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runTools() without stream:true incorrectly emits "message" events for messages passed in #994

Closed
1 task done
Macil opened this issue Aug 14, 2024 · 1 comment
Closed
1 task done
Labels
bug Something isn't working

Comments

@Macil
Copy link

Macil commented Aug 14, 2024

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

  1. Call openai.beta.chat.completions.runTools() with one or more messages, without specifying stream: true.
  2. Add a "message" event listener on the returned runner.
  3. Observe that it emits events for all the messages including the ones passed in originally, not just the messages generated by the runTools() call.
  4. Change the runTools() call to set the stream: true option 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

@RobertCraigie
Copy link
Collaborator

RobertCraigie commented Sep 3, 2024

Thanks for the detailed bug report, this will be fixed in the next release #1028.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants