KEMBAR78
Add `tool_choice` parameter to `ChatCompletionClient` `create` and `create_stream` methods by Copilot · Pull Request #6697 · microsoft/autogen · GitHub
Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 19, 2025

Summary

Implements the tool_choice parameter for ChatCompletionClient interface as requested in #6696. This allows users to restrict which tools the model can choose from when multiple tools are available.

Changes

Core Interface

  • Core Interface: Added tool_choice: Tool | Literal["auto", "required", "none"] = "auto" parameter to ChatCompletionClient.create() and create_stream() methods
  • Model Implementations: Updated client implementations to support the new parameter, for now, only the following model clients are supported:
    • OpenAI
    • Anthropic
    • Azure AI
    • Ollama
  • LlamaCppChatCompletionClient currently not supported

Features

  • "auto" (default): Let the model choose whether to use tools, when there is no tool, it has no effect.
  • "required": Force the model to use at least one tool
  • "none": Disable tool usage completely
  • Tool object: Force the model to use a specific tool

@ekzhu
Copy link
Collaborator

ekzhu commented Jun 20, 2025

@copilot can you continue working

…mplementations

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI changed the title [WIP] Add tool_choice to ChatCompletionClient create and create_stream method Add tool_choice parameter to ChatCompletionClient create and create_stream methods Jun 20, 2025
Copilot finished work on behalf of ekzhu June 20, 2025 12:47
Copy link
Collaborator

@ekzhu ekzhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit tests for AnthropicChatCompletionClient and OpenAIChatCompletionClient to test the use of tool_choice parameter using mocks.

Copilot AI and others added 2 commits June 22, 2025 07:44
…auto"

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 22, 2025

Add unit tests for AnthropicChatCompletionClient and OpenAIChatCompletionClient to test the use of tool_choice parameter using mocks.

Added comprehensive unit tests for both AnthropicChatCompletionClient and OpenAIChatCompletionClient to test the tool_choice parameter using mocks. Tests cover specific tool selection, "auto" mode, None mode, and validation errors. Commit 717c05b

@copilot can you continue working

Completed the implementation as requested. Updated the tool_choice parameter type, refactored all client implementations, and added comprehensive unit tests for OpenAI and Anthropic clients. All changes are now complete and committed.

Copilot AI requested a review from ekzhu June 22, 2025 07:48
Copilot finished work on behalf of ekzhu June 22, 2025 07:48
Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI requested a review from ekzhu June 23, 2025 13:11
Copilot finished work on behalf of ekzhu June 23, 2025 13:11
@ekzhu ekzhu marked this pull request as ready for review June 23, 2025 13:13
@ekzhu
Copy link
Collaborator

ekzhu commented Jun 30, 2025

@copilot update the PR description, do not modify the code.

Copy link
Contributor Author

Copilot AI commented Jun 30, 2025

@copilot update the PR description, do not modify the code.

Updated the PR description to reflect the current implementation with the simplified tool_choice parameter type and behavior.

Copilot finished work on behalf of ekzhu June 30, 2025 04:45
@ekzhu
Copy link
Collaborator

ekzhu commented Jun 30, 2025

@copilot write me a new PR description, remove the existing one.

Copy link
Contributor Author

Copilot AI commented Jun 30, 2025

@copilot write me a new PR description, remove the existing one.

Created a new PR description that accurately reflects the current implementation with the simplified tool_choice parameter design and comprehensive testing coverage.

Copilot finished work on behalf of ekzhu June 30, 2025 04:49
@codecov
Copy link

codecov bot commented Jun 30, 2025

Codecov Report

Attention: Patch coverage is 64.86486% with 39 lines in your changes missing coverage. Please review.

Project coverage is 80.25%. Comparing base (6f15270) to head (6bd777b).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../autogen_ext/models/anthropic/_anthropic_client.py 45.65% 25 Missing ⚠️
...t/models/llama_cpp/_llama_cpp_completion_client.py 22.22% 7 Missing ⚠️
...xt/src/autogen_ext/models/openai/_openai_client.py 87.50% 3 Missing ⚠️
...xt/models/replay/_replay_chat_completion_client.py 60.00% 2 Missing ⚠️
...els/semantic_kernel/_sk_chat_completion_adapter.py 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6697      +/-   ##
==========================================
+ Coverage   79.85%   80.25%   +0.40%     
==========================================
  Files         232      232              
  Lines       17540    17643     +103     
==========================================
+ Hits        14006    14160     +154     
+ Misses       3534     3483      -51     
Flag Coverage Δ
unittests 80.25% <64.86%> (+0.40%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ekzhu ekzhu merged commit c150f85 into main Jun 30, 2025
66 of 67 checks passed
@ekzhu ekzhu deleted the copilot/fix-6696 branch June 30, 2025 05:15
@reqyou
Copy link

reqyou commented Jul 18, 2025

Whats your plan with wiring it up to the AssistantAgent and down to the _call_llm?

@ekzhu
Copy link
Collaborator

ekzhu commented Jul 18, 2025

Whats your plan with wiring it up to the AssistantAgent and down to the _call_llm?

It's already being used by AssistantAgent for the reflection step.

@reqyou
Copy link

reqyou commented Jul 23, 2025

That is all well and good, but what about the rest? When _tools is not empty, as a user, I would expect that either on model_client.create_stream(..) or model_client.create(..) in for example _call_llm, the tool_choice parameter is set to required by default, or that tool_choice is exposed as an assistant agent parameter to have better control and e.g. leave it to None to still allow the agent to decide whether to call the tool or just create a result.

So imho this MR only addresses half of the work.

@ekzhu
Copy link
Collaborator

ekzhu commented Jul 23, 2025

That is all well and good, but what about the rest? When _tools is not empty, as a user, I would expect that either on model_client.create_stream(..) or model_client.create(..) in for example _call_llm, the tool_choice parameter is set to required by default, or that tool_choice is exposed as an assistant agent parameter to have better control and e.g. leave it to None to still allow the agent to decide whether to call the tool or just create a result.

So imho this MR only addresses half of the work.

That's rather strong assumption on the agent behaviour. I think it will also conflict with the max_tool_iteration parameter. If you set that parameter, then the tool calling loop only ends when the agent stops calling tool or reaching maximum iteration. If the tool choice is also set to required, then you end up with forced tool calling when it is not needed.

I think in many cases AssistantAgent is not suitable. It's already a kitchen sink. See my note on the documentation: https://microsoft.github.io/autogen/stable/user-guide/agentchat-user-guide/tutorial/agents.html#assistant-agent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants