fix(infer): raise the intended ValueError when tool_choice names an unknown tool - #10250
Merged
Merged
Conversation
hjh0119
approved these changes
Sep 28, 2026
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.
PR type
PR information
A
swift deployserver answers HTTP 500 Internal Server Error for a request that selects a tool which is not intools, even though the code has a message prepared for exactly that case.ChatCompletionRequestMixin.__post_init__filters the tool list by name:next(generator)with no default raisesStopIterationas soon as the generator is exhausted, sotoolis neverNoneand theValueErrorbelow it is unreachable.__post_init__runs while the request object is being built, i.e. beforecreate_chat_completion(swift/pipelines/infer/deploy.py:272) gets a chance to run its ownexcept Exception→ 400 handler, so the exception escapes the handler entirely.Measured on the current
main(5256c62e), constructing the request directly:and end to end, with a FastAPI app that registers the same
create_chat_completion(request: ChatCompletionRequest, raw_request: Request)signature asdeploy.py:272and a body of{"model": "m", "messages": [...], "tools": [{"type": "function", "function": {"name": "get_weather"}}], "tool_choice": {"type": "function", "function": {"name": "no_such_tool"}}}:500 Internal Server Error, bodyInternal Server Error— the client is told nothing about what it sent wrong;422with{"type": "value_error", ..., "msg": "Value error, Tool choice 'no_such_tool' not found in tools."}— the message the author wrote.tool_choicenaming a specific function is part of the OpenAI chat-completions request format that this endpoint is compatible with, so a client that misspells a tool name is a normal case, not a crafted one. A wrong name should not look like a server fault.The fix is one argument: give
next()the default the followingifalready expects.Tested with the new
tests/general/test_protocol_tool_choice.py, which pins the failing case plus two cases that must keep working (a name that is intoolsnarrowsrequest.toolsto just that tool; a stringtool_choicesuch as'auto'leaves the list untouched):mainbefore the fix:Ran 3 tests ... FAILED (errors=1), the error being theStopIterationabove;Ran 3 tests ... OK, and the same route probe now returns the422/ValueErrormessage;flake8,isort --check-onlyandyapf --diffreport nothing on either file.Disclosure: this PR was prepared, tested and submitted by an AI agent working on behalf of the account owner.