feat: enforce tool_choice parameter in chat/completions#992
Open
eloe wants to merge 2 commits intoBlaizzy:mainfrom
Open
feat: enforce tool_choice parameter in chat/completions#992eloe wants to merge 2 commits intoBlaizzy:mainfrom
eloe wants to merge 2 commits intoBlaizzy:mainfrom
Conversation
Apply tool_choice policy before passing tools to the chat template:
- "none": strip tools entirely (no tool calls possible)
- "auto": pass tools unchanged (default, current behavior)
- "required": keep tools + inject system instruction to force tool use
- {"type":"function","function":{"name":"X"}}: filter to specific tool
New helper: resolve_tool_choice() returns filtered tools and optional
system instruction based on the policy.
Adds tool_choice and tools fields to ChatRequest model.
Adds 9 tests: 7 unit tests for resolve_tool_choice, 2 endpoint tests.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Validate tool_choice: reject invalid string values with 400 - Unknown tool name now returns 400 instead of falling back - Skip tool parsing when tool_choice="none" - Use == instead of is for equality checks in tests Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
tool_choiceenforcement to/v1/chat/completionsand/responses, per the OpenAI API spec.Supported values
"auto"(default)"none""required"{"type": "function", "function": {"name": "X"}}Implementation
resolve_tool_choice()returns a filtered tool list and an optional system instruction. This approach works with any chat template without requiring model-specific tool_choice handling.Tests
test_tool_choice_none_strips_toolstest_tool_choice_required_injects_instructiontest_tool_choice_specific_functiontest_tool_choice_auto_passes_through