Skip to content

Python: Add function_choice_behavior support to Azure AI and OpenAI Assistant agents#14057

Open
SergeyMenshykh wants to merge 4 commits into
microsoft:mainfrom
SergeyMenshykh:add-fcb-and-tools-override
Open

Python: Add function_choice_behavior support to Azure AI and OpenAI Assistant agents#14057
SergeyMenshykh wants to merge 4 commits into
microsoft:mainfrom
SergeyMenshykh:add-fcb-and-tools-override

Conversation

@SergeyMenshykh
Copy link
Copy Markdown
Contributor

@SergeyMenshykh SergeyMenshykh commented Jun 5, 2026

Description

Adds function_choice_behavior parameter to Azure AI and OpenAI Assistant agents, aligning them with the existing Responses agent and Chat Completion agent APIs.

Changes

  • Add function_choice_behavior parameter to invoke/invoke_stream/get_response on both AzureAIAgent and OpenAIAssistantAgent public APIs
  • Add function_choice_behavior parameter to internal AzureAIAgentThreadActions and AssistantThreadActions invoke/invoke_stream methods
  • Support tools parameter override for SDK-level tools (CodeInterpreter, FileSearch, etc.)
  • Filter kernel functions based on FunctionChoiceBehavior configuration
  • Validate that only Auto type with auto-invoke enabled is supported
  • Fix _get_tools in OpenAI path to use passed kernel parameter instead of agent.kernel
  • Add comprehensive unit tests for both thread actions and public agent APIs

…ssistant agents

Add function_choice_behavior parameter to invoke() and invoke_stream() methods
in Azure AI and OpenAI Assistant agent thread actions, aligning them with the
existing Responses agent and Chat Completion agent APIs.

Changes:
- Add function_choice_behavior parameter to invoke/invoke_stream in both
  AzureAIAgentThreadActions and AssistantThreadActions
- Support tools parameter override for SDK-level tools (CodeInterpreter,
  FileSearch, etc.)
- Filter kernel functions based on FunctionChoiceBehavior configuration
- Validate that only Auto type with auto_invoke enabled is supported
- Fix _get_tools in OpenAI path to use passed kernel parameter
- Add comprehensive unit tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@SergeyMenshykh SergeyMenshykh requested a review from a team as a code owner June 5, 2026 19:51
Copilot AI review requested due to automatic review settings June 5, 2026 19:51
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Jun 5, 2026
@SergeyMenshykh SergeyMenshykh self-assigned this Jun 5, 2026
@SergeyMenshykh SergeyMenshykh moved this to In Review in Agent Framework Jun 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 5, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
agents/azure_ai
   agent_thread_actions.py44920255%178, 180, 217–223, 263–264, 267–271, 273–275, 279, 282–284, 293, 298–299, 361, 365, 370, 373, 376–377, 381, 384, 387–388, 392, 395, 398–399, 403–404, 408–409, 413–414, 418–419, 423, 426, 535, 537, 578–579, 603, 605–607, 609–611, 613, 615–617, 619–620, 622–627, 629–637, 641–642, 645–648, 652, 655–656, 659–660, 663, 666, 669–670, 673–674, 677–683, 685–686, 690, 693–694, 702–703, 708, 712–715, 717–718, 725, 727, 731, 734–739, 741–743, 748–749, 752–755, 758–760, 770–771, 778, 780, 784, 786–787, 791–800, 802–810, 861, 864, 898–899, 906–910, 970, 976–979, 1049–1051, 1065, 1082–1084, 1088–1089, 1104–1105, 1187–1190, 1192–1193, 1198, 1202–1203, 1208
   azure_ai_agent.py36318050%100, 102–105, 107–109, 111–116, 118, 120, 122–124, 126, 143, 145–148, 150–155, 157, 162–163, 168–171, 176–180, 182–185, 187–193, 198, 200–203, 205–207, 209–212, 214, 216, 226–227, 229–232, 234–235, 240, 242–248, 250, 283, 294–295, 300–301, 304, 309–314, 321–322, 324, 329–330, 341–347, 399, 401, 407, 418, 421, 423, 425, 427, 461, 492–494, 496, 498–499, 501–504, 507–509, 511–512, 515, 518–527, 529, 538–539, 542–544, 546–547, 557–558, 560, 578, 580, 583, 585–586, 588–589, 591, 600–601, 603, 605–608, 611, 613, 616–618, 622, 694, 804, 912, 986
agents/open_ai
   assistant_thread_actions.py3565783%110, 113, 119, 207, 209, 248–254, 438, 496, 509, 523, 624–625, 627–628, 634–635, 637–638, 640–643, 645–646, 648–649, 651–652, 666–670, 673–675, 724, 726–727, 731–735, 737–738, 752–753, 824, 829, 861
   openai_assistant_agent.py3657180%77, 113–116, 121, 125–126, 159, 176–177, 185–190, 197–198, 201, 206–207, 218–224, 402–403, 410–411, 413–414, 416–417, 419–424, 426, 474–475, 478, 481, 484–493, 495, 505, 529–530, 568, 584, 594, 610, 623, 659, 806, 919, 1030
TOTAL28693563180% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3966 23 💤 0 ❌ 0 🔥 1m 59s ⏱️

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds per-invocation function_choice_behavior support to the Azure AI and OpenAI Assistant agent thread actions so callers can restrict kernel function availability consistently with the existing Responses and Chat Completion agent APIs, and also adds a per-invocation override for SDK-level tools.

Changes:

  • Add function_choice_behavior to invoke() / invoke_stream() and validate only Auto with auto-invoke enabled is accepted.
  • Add tools override support for SDK-level tools and filter exposed kernel functions based on FunctionChoiceBehavior config.
  • Add new unit tests covering validation, tools override, kernel tool filtering, and passing behavior into kernel invocation.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
python/semantic_kernel/agents/open_ai/assistant_thread_actions.py Adds function_choice_behavior, validates it, supports SDK-tool overrides, and filters kernel function tools.
python/semantic_kernel/agents/azure_ai/agent_thread_actions.py Mirrors function_choice_behavior + SDK-tool override support and function-tool filtering for Azure AI agents.
python/tests/unit/agents/openai_assistant/test_assistant_thread_actions.py Adds unit tests for validation, tool override behavior, filtering, and kernel invocation parameter passing.
python/tests/unit/agents/azure_ai_agent/test_agent_thread_actions.py Adds unit tests for the Azure AI thread actions covering the same scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread python/semantic_kernel/agents/open_ai/assistant_thread_actions.py
Comment thread python/semantic_kernel/agents/open_ai/assistant_thread_actions.py
Comment thread python/semantic_kernel/agents/azure_ai/agent_thread_actions.py
Comment thread python/tests/unit/agents/openai_assistant/test_assistant_thread_actions.py Outdated
Comment thread python/tests/unit/agents/azure_ai_agent/test_agent_thread_actions.py Outdated
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 89%

✓ Correctness

The PR correctly adds function_choice_behavior support to Azure AI and OpenAI Assistant agents. The parameter is properly threaded through all code paths (invoke, invoke_stream, streaming requires_action handling, function call invocation). The validation logic, tools filtering, and the bug fix to use the passed kernel parameter instead of agent.kernel are all correct. The kernel.invoke_function_call API accepts the function_behavior kwarg and uses it for allowlist validation. No correctness issues found.

✓ Security Reliability

The PR properly validates function_choice_behavior (only Auto with auto_invoke=True passes), correctly filters kernel functions in _get_tools based on FCB configuration, consistently plumbs the parameter through all code paths (invoke, invoke_stream, _handle_streaming_requires_action, _invoke_function_calls), and fixes a legitimate bug where the OpenAI path used agent.kernel instead of the passed kernel parameter. No critical security or reliability issues found.

✓ Test Coverage

The PR adds good unit test coverage for the individual helper methods (_validate_function_choice_behavior, _get_tools, _invoke_function_calls). However, there are notable gaps: (1) no test verifies that the streaming path (_handle_streaming_requires_action) actually threads function_choice_behavior through to _invoke_function_calls, and (2) no integration-level test confirms that invoke()/invoke_stream() call _validate_function_choice_behavior before proceding. The existing test_handle_streaming_requires_action_returns_result does not pass or assert on the new function_choice_behavior parameter.

✓ Design Approach

The OpenAI assistant changes look aligned with the existing Responses-agent approach, but the Azure path introduces one design regression: function filtering is applied before validating preconfigured Azure function tools, so a caller can no longer use function_choice_behavior to narrow an agent that already has FunctionToolDefinitions on its definition — the invocation fails instead of exposing a reduced tool set.

Suggestions

  • For Azure agents, either validate function-tool registration against the full kernel registry before applying function_choice_behavior, or drop/filter FunctionToolDefinition entries from tools to match the filtered kernel metadata. This would preserve the PR's stated per-invocation restriction behavior and match the existing Responses-agent pattern in responses_agent_thread_actions.py:1207-1237.
  • When FunctionChoiceBehavior.Auto(enable_kernel_functions=False) is passed with no filters, kernel.invoke_function_call skips allowlist validation because function_behavior.filters is falsy. If the model hallucinated a function call for a kernel function not in the tools list, it would still be executed. Adding an explicit guard in _invoke_function_calls or kernel.invoke_function_call to reject calls when enable_kernel_functions=False would provide stronger defense-in-depth.

Automated review by SergeyMenshykh's agents

SergeyMenshykh and others added 2 commits June 5, 2026 21:07
Add function_choice_behavior parameter to get_response(), invoke(), and
invoke_stream() on AzureAIAgent and OpenAIAssistantAgent, forwarding it
to the thread actions layer. Add unit tests to verify the parameter is
passed through correctly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@SergeyMenshykh SergeyMenshykh force-pushed the add-fcb-and-tools-override branch from 7f244e2 to 69a7598 Compare June 5, 2026 20:19
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

3 participants