-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
toolChoice: 'required' and { toolName: 'x', type: 'tool' } send ai vercel SDK into endless loop when using streamText #3944
Comments
Tool choice required means that you force tool calls every time the LLM is called. Together with maxSteps this enters such a loop. This is working as expected. Can you describe what you want to achieve? there may be other approaches that could work here. |
If you explicitly control that they want to use websearch through a flag, I recommend using a RAG approach vs a tool call approach. Pseudo-code backend: if (websearch) {
const searchResult = await runMyWebsearch(...);
prompt = buildMyPrompt(searchResult)
} else {
prompt = myDefaultPrompt
}
return streamText( ... , with system or prompt influenced by the above, or by augmenting the last user msg) |
Do you happen to have any real-world examples of this? |
@lgrammel after talking through this with folks on Discord, I really feel like this is a bug, or at least a limitation of AI vercel SDK. The fact that AI sdk does not incorporate the results and just keeps looping in cycle feels broken. |
The issue is that you specify the |
do we know if there is a way to turn it off after the first turn? |
Ended up rewriting without using AI SDK, and just using OpenAI SDK. I swear, the code is about 100x times easier to reason about now. |
I encountered the same problem. https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling This document describes how maxSteps can be used to invoke tools and allow the large model to analyze and summarize based on the tool results, but the toolChoice parameter disrupts this usage. Perhaps a parameter could be added to ensure a specific tool must be called (at least once) without disrupting the use of maxSteps? There are some scenarios where you cannot rely on the model to determine whether to call a specific tool, such as when the user manually selects to use a private knowledge base to answer questions. |
if possible please have a feature that solves this problem of calling the tools no matter what, for now i have just tried a prompt, but i do think it will be good in long run |
Description
toolChoice: 'required' and { toolName: 'x', type: 'tool' } send ai vercel SDK into endless loop when using streamText
Code example
AI provider
@ai-sdk/openai
Additional context
Logs:
Notice how it starts over with a new
tool-call
for the same question.The same pattern repeats indefinitely.
The text was updated successfully, but these errors were encountered: