Skip to content

Add tool_choice parameter to ChatCompletionClient create and create_stream methods #6697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 30, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot 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 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 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 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
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
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.

2 participants