fix tool choice
Currently, tool_choice is not usable when set to "required" or a specific function. This PR ensures tool_choice is reset to "auto" or "none" temporarily when passing back tool response to the LLM, so it can be used practically.
✅ Changeset File Detected
The following changeset entries were found:
patch-livekit-agents
Change description: fix tool choice (#2332)
Currently, tool_choice is not usable when set to "required" or a specific function.
what do you mean by this? could you provide any examples that would illustrate the problem?
Currently, tool_choice is not usable when set to "required" or a specific function.
what do you mean by this? could you provide any examples that would illustrate the problem?
When using function calling, we typically need two LLM calls:
- First call: the user query → LLM decides which function to call.
- Second call: we pass the function result back to LLM for final response.
If tool_choice="required" or a specific function is set, then every call to the LLM will invoke the tool again, including the second call, where we just want it to continue the conversation using the tool's output. This leads to redundant or incorrect behavior.
Example:
llm = openai.LLM(tool_choice="required", tools=[get_weather])
Even after we call get_weather() and pass its output back to the model, it'll still try to call get_weather() again, instead of generating a final response.
This PR avoids that by resetting tool_choice to "auto" or "none" after the tool has been executed.