[router][grpc] Implement tool_choice support for Responses API #12668
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.
Motivation
The Responses API (
/v1/responses) did not support thetool_choiceparameter, which is a critical feature for controlling tool calling behavior according to OpenAI's Responses API specification. This limitation prevented users from:tool_choice: {type: "function", function: {name: "..."}})tool_choice: "required")tool_choice: {type: "allowed_tools", ...})tool_choice: "none")This PR implements full
tool_choicesupport for the Responses API in both Harmony and Regular routers, following the same pattern as the existing Chat Completion API implementation.Modifications
1. Harmony Router - Responses API tool_choice Support
File:
src/routers/grpc/harmony/stages/preparation.rstool_choiceconstraint generation for Responses API requestsResponseTools(schemas populated before pipeline)triggered_tagsformat for tool constraints2. Regular Router - Responses API tool_choice Support
Files:
src/routers/grpc/regular/responses/conversions.rssrc/routers/grpc/regular/responses/tool_loop.rsFlow:
conversions.rs: Extracts function tools and passes throughtool_choiceunchangedtool_choiceconstraints appliedtool_choice, iteration 1+ uses "auto" to prevent infinite loops3. ToolReference Protocol Enhancement
File:
src/protocols/common.rsConverted
ToolReferencefrom a simple struct to a tagged enum to properly support different tool types:Benefits:
identifier(),function_name()4. Chat API Validation
File:
src/protocols/chat.rsAdded validation that Chat Completion API ONLY accepts
Functiontype ToolReference in tool_choice. Rejects MCP and hosted tools with clear error messages, enforcing the API contract.5. Code Deduplication
File:
src/routers/grpc/common/responses/utils.rsCreated shared utility function
extract_tools_from_response_tools()to eliminate duplication:include_mcpparameter controls whether to extract MCP tools6. Helper Function for Chat Requests
File:
src/routers/grpc/regular/responses/tool_loop.rsCreated
prepare_chat_tools_and_choice()helper to:tool_choicebased on iteration (user's choice on iteration 0, "auto" on iteration 1+)7. Bug Fixes
pub use: Changedpub use crate::tokenizer::StopSequenceDecoderto regularuseinutils.rstests/spec/chat_completion.rsto use new ToolReference enum syntax8. Documentation
Added comprehensive comments explaining:
conversions.rs(why MCP path "wastes" initial extraction)tool_choiceAccuracy Tests
Benchmarking and Profiling
Checklist