Skip to content

streaming - get run_id from toolCallDone event #1260

Open
@carmen0208

Description

Confirm this is a feature request for the Node library and not the underlying OpenAI API.

  • This is a feature request for the Node library

Describe the feature or improvement you're requesting

Current Behavior

When handling tool outputs using submitToolOutputsStream(), we currently need to track the run_id through the runStepCreated event; it would be better if the run_id is available in the toolCallDone event's payload.

Current pattern:

 const stream: AssistantStream = await openaiClient.beta.threads.runs.stream(
      thread.id,
      {
        assistant_id: assistant.id,
      }
    );

    let runId: string | undefined;
    stream
      .on("runStepCreated", (runStep) => {
        runId = runStep.run_id;
      })
      .on("toolCallDone", async (toolCall) => {
        console.log("toolCallDone", toolCall);
        if (toolCall.type == "function") {
          // call function...
          if (runId) {
              await openaiClient.beta.threads.runs.submitToolOutputsStream(
                thread.id,
                runId,
                {
                  tool_outputs: [
                    ...
                  ],
                }
              );
          }
        }
      })

Proposed Enhancement

The toolCallDone event handler should receive the run_id directly in its payload, eliminating the need for the separate runStepCreated event handler. This would simplify the code and remove potential race conditions.

Suggested pattern:

outputStream
  .on("toolCallDone", async (toolCall: ToolCall, runId: string) => {
     // call function...
    await openaiClient.beta.threads.runs.submitToolOutputsStream(
        thread.id,
        runId,
        {
          tool_outputs: [...],
        }
      );
}

Benefits

  1. Simpler, more straightforward code
  2. Eliminates potential race conditions between events
  3. Reduces unnecessary state management
  4. More intuitive API design

Additional Context

This enhancement would be particularly useful when chaining multiple tool calls, as it would remove the need to maintain run ID state between event handlers.

Environment

  • OpenAI Node.js Library Version: [4.77.3]

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions