You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importOpenAIfrom'openai';import{OpenAIStream,StreamingTextResponse}from'ai';// some request handler functionconsthandler=async()=>{constopenai=newOpenAI();constoptions={messages: [/* some messages */],tools: [/* some tools */]};constresponse=awaitopenai.chat.completions.create(options);conststream=OpenAIStream(response,{experimental_onToolCall: async(payload,append)=>{console.log(payload);return'Thinking...';}});returnnewStreamingTextResponse(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?
The text was updated successfully, but these errors were encountered:
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.
Description
When using the
experimental_onToolCall
callback withOpenAIStream
, 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 istrue
when thefinish_reason
is'stop'
, which is what is being returned fortool_calls
. The JSON value is incorrect and thetool_calls
array is not being closed properly.Code example
Additional context
I tried setting a
type
variable when the response string is being created for either thefunction_call
or thetool_calls
, then I used that in theelse if
expressions to determine how the response string should be closed and that worked.Are you excepting PRs for this?
The text was updated successfully, but these errors were encountered: