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

Malformed JSON Response from Assistant API: Extra Bracket at the Start of Response #1265

Open
1 task done
mostafa-gado opened this issue Jan 15, 2025 · 0 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@mostafa-gado
Copy link

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

I created the Assistant with response_format: { type: "json_object" }. Also In the Assistant instructions I informed it to respond with valid JSON only.

The issue is that the response is always returned with an extra characters in the beginning before the actual JSON object. Sometimes it's a {" or [.

After some debugging I found that this extra character is returned in the "textCreated" stream event when running the thread.

To Reproduce

  1. create a function that runs a thread this.openai.beta.threads.runs.stream()
  2. Print the output in all the events.

Code snippets

const stream = await new Promise<string>((resolve, reject) => {
        let finalResult = ""; // Variable to store the response
        const stream = this.openai.beta.threads.runs
          .stream(thread, {
            assistant_id: assistantId,
          })
          .on("textCreated", (text) => {
            Logger.log("textCreated: " + JSON.stringify(text.value));
            // finalResult += text.value; //>>>>>>>>>The issue happens here. I fixed it by commenting this line
          })
          .on("textDelta", (textDelta, snapshot) => {
            if (textDelta.value) {
              Logger.log("textDelta: " + JSON.stringify(textDelta.value));
              finalResult += textDelta.value;
            }
          })
          .on("end", () => {
            Logger.log("Thread run completed.");
            resolve(finalResult.trim()); // Resolve the promise with the final output
          })
          .on("error", (error) => {
            Logger.error(
              "Error during thread run: " + (error as Error).message
            );
            reject(error); // Reject the promise on error
          });
      });
      Logger.log("Chat with assistant response:\n" + stream);

      return stream;

OS

Windows 10

Node version

Node v22.13.0

Library version

openai v4.78.1

@mostafa-gado mostafa-gado added the bug Something isn't working label Jan 15, 2025
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

1 participant