By default the model decides when to use one of the functions you offer. But the API can be forced to use a specific function by setting the tool_choice parameter to that function. Use it when the task has only one right tool, such as looking up an order, and letting the model choose would only add a chance of a wrong choice.
Prerequisites
- A task with exactly one right function.
- The function’s name, as defined in your tools list.
- A way to set tool_choice in the request.
Steps
Step 1: Name the function
Write down the one function the task needs, and confirm it is in the tools list for the request. Forcing a function that is not offered fails.
Step 2: Set tool_choice
Set tool_choice to require that function for this request. Send the request with the task and the tools list as usual; the model must now call the named function.
Step 3: Check the call
Read the tool call that comes back: the function name should be the one you forced, and the arguments should match its schema. If the arguments are wrong, the problem is the prompt or the schema, not the forcing.
Worked example: Cedar Street Library card renewal
The fictional Cedar Street Library forces its agent to call renew_card for a renewal question, instead of letting it choose among 3 tools.
| Request | tool_choice | Function called | Correct |
|---|---|---|---|
| Renew my card | None | search_catalogue | No |
| Renew my card | renew_card | renew_card | Yes |
Without tool_choice the model calls the wrong function, and with tool_choice set to renew_card it calls the right one.
Expected result. The model calls the forced function, with arguments matching its schema. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- Forcing a function does not guarantee good arguments; validate them before running the function.
- Use forcing sparingly: a task with several right tools should let the model choose.
- This guide does not cover forcing the model not to call any tool.
