agents icon indicating copy to clipboard operation
agents copied to clipboard

fix tool choice

Open jayeshp19 opened this issue 7 months ago • 3 comments

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.

jayeshp19 avatar May 19 '25 22:05 jayeshp19

✅ Changeset File Detected

The following changeset entries were found:

  • patch - livekit-agents

Change description: fix tool choice (#2332)

github-actions[bot] avatar May 19 '25 22:05 github-actions[bot]

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?

davidzhao avatar May 20 '25 05:05 davidzhao

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:

  1. First call: the user query → LLM decides which function to call.
  2. 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.

jayeshp19 avatar May 20 '25 05:05 jayeshp19