ai icon indicating copy to clipboard operation
ai copied to clipboard

Properly handle client function/tool calls that appear partway through the response stream.

Open rajdtta opened this issue 3 months ago • 1 comments

The Issue

img

Currently, if a LLM response starts with regular content and then calls a tool or function partway through, the entire response will still be put into the message content field rather than populating the appropriate function_call or tool_calls field of the message (which will trigger the client callbacks to handle the function/tool calls).

Diagnosis / Root Cause

When l looked at the underlying implementation, I saw that callChatApi() was only checking whether the streamed response started with the function/tool call prefix (and not whether they were present midway though).

Proposed Solution

I added some extra logic to detect whether an intermediate chunk starts with the tool/function call prefix.

By default, all streamed chunks will populate the content field. The moment a function/tool call prefix is detected, a flag is set to parse the remainder of the message as either a function or tool call.

This change seems to solve the issue entirely, and I'm no longer able to replicate the original bug on my local environment despite my best efforts.

rajdtta avatar Mar 11 '24 19:03 rajdtta

Hey @lgrammel,

Apologies if you're not the right person to tag for this, but I was wondering when someone would be able to take a look through (and hopefully approve) the PR.

rajdtta avatar Mar 29 '24 03:03 rajdtta

Thanks for the PR. We are planning to switch to the StreamData protocol in the next major release and remove this part of the code: https://github.com/vercel/ai/pull/1192

In the meantime, you can already use experimental_streamData: true to activate the stream data protocol which should not have these issues.

lgrammel avatar Apr 09 '24 06:04 lgrammel