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

chunkToText in OpenAIStream is mishandling tool_calls #1608

Closed
doowb opened this issue May 16, 2024 · 2 comments
Closed

chunkToText in OpenAIStream is mishandling tool_calls #1608

doowb opened this issue May 16, 2024 · 2 comments
Labels

Comments

@doowb
Copy link

doowb commented May 16, 2024

Description

When using the experimental_onToolCall callback with OpenAIStream, I am consistently receiving a JSON parsing error.
I tracked it down to how the chunkToText function is handling the finish_reason.

The else if highlighted above is true when the finish_reason is 'stop', which is what is being returned for tool_calls. The JSON value is incorrect and the tool_calls array is not being closed properly.

Code example

import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';

// some request handler function
const handler = async () => {
  const openai = new OpenAI();
  const options = {
    messages: [/* some messages */],
    tools: [/* some tools */]
  };
  
  const response = await openai.chat.completions.create(options);
  const stream = OpenAIStream(response, {
    experimental_onToolCall: async (payload, append) => {
      console.log(payload);
      return 'Thinking...';
    }
  });
  
  return new StreamingTextResponse(stream);
}; 

Additional context

I tried setting a type variable when the response string is being created for either the function_call or the tool_calls, then I used that in the else if expressions to determine how the response string should be closed and that worked.

Are you excepting PRs for this?

@lgrammel lgrammel added the ai/ui label May 16, 2024
@lgrammel
Copy link
Collaborator

Thanks for investigating. It's hard to tell for me how involved and risky the fix is without seeing it. That code area is fairly complex.

Please note that the OpenAIStream approach with onToolCall is outdated and we recommend using AI SDK Core now:
https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling

@doowb
Copy link
Author

doowb commented May 16, 2024

Thanks for the quick response. I noticed in the docs that OpenAIStream isn't mentioned as much, so I started looking for what replaced it. I was hoping for a similar decoupling of making the request to the "provider" and parsing the stream.

I'm going to close this, but if I make a fix, I'll submit a PR.

@doowb doowb closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants