You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python: fix(ollama): omit tools when tool_choice is none - #8987
OllamaChatClient drops tool_choice but still sends tools, so tool_choice="none" has no effect. This also affects callers who never set it: when the function invocation limit is reached, the loop makes a final request with tool_choice="none" to get a text answer. With Ollama the tools are offered again, the model may return another tool call, and the caller gets the fallback text instead of a model answer.
Description & Review Guide
What are the major changes?_prepare_options now omits tools from the request when the tool mode is none. Ollama has no tool choice parameter, so this is the only way to honor it. BedrockChatClient handles none the same way. The two tool_choice docstrings are updated to match.
What is the impact of these changes? With tool_choice="none", Ollama is no longer offered tools. Other tool choices behave as before: the tools are sent and tool_choice itself is not forwarded. tool_choice now goes through validate_tool_mode, so an invalid value raises ContentError instead of being ignored.
What do you want reviewers to focus on? Whether omitting the tools is the behavior you want for Ollama, given tool_choice is documented as unsupported there. The tests are offline with a mocked AsyncClient.chat; I have not run this against a live Ollama server.
This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.
The reason will be displayed to describe this comment to others. Learn more.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Fixes Ollama connector behavior so tool_choice="none" is honored by omitting tools from the request (since Ollama lacks a native tool_choice parameter), aligning behavior with other connectors and preventing extra tool-call loops.
Changes:
Update OllamaChatClient._prepare_options to drop tools when validated tool mode is none and to validate tool_choice via validate_tool_mode.
Update tool_choice docstrings to document the special handling of none.
Add unit tests covering omission/retention of tools and the function-invocation-limit final request behavior.
The newly supported none mode is still rejected by the exported OllamaChatOptions type: tool_choice: None prevents typed callers from passing either {"tool_choice": "none"} or {"tool_choice": {"mode": "none"}}. Remove this override to inherit ChatOptions.tool_choice, which already accepts both forms. The class-level documentation can retain the explanation of Ollama's behavior.
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
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 & Context
OllamaChatClientdropstool_choicebut still sendstools, sotool_choice="none"has no effect. This also affects callers who never set it: when the function invocation limit is reached, the loop makes a final request withtool_choice="none"to get a text answer. With Ollama the tools are offered again, the model may return another tool call, and the caller gets the fallback text instead of a model answer.Description & Review Guide
_prepare_optionsnow omitstoolsfrom the request when the tool mode isnone. Ollama has no tool choice parameter, so this is the only way to honor it.BedrockChatClienthandlesnonethe same way. The twotool_choicedocstrings are updated to match.tool_choice="none", Ollama is no longer offered tools. Other tool choices behave as before: the tools are sent andtool_choiceitself is not forwarded.tool_choicenow goes throughvalidate_tool_mode, so an invalid value raisesContentErrorinstead of being ignored.tool_choiceis documented as unsupported there. The tests are offline with a mockedAsyncClient.chat; I have not run this against a live Ollama server.Related Issue
Fixes #8986
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.