Python: Add function_choice_behavior support to Azure AI and OpenAI Assistant agents#14057
Python: Add function_choice_behavior support to Azure AI and OpenAI Assistant agents#14057SergeyMenshykh wants to merge 4 commits into
Conversation
…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>
There was a problem hiding this comment.
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_behaviortoinvoke()/invoke_stream()and validate onlyAutowith auto-invoke enabled is accepted. - Add
toolsoverride support for SDK-level tools and filter exposed kernel functions based onFunctionChoiceBehaviorconfig. - 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.
There was a problem hiding this comment.
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_behaviorto narrow an agent that already hasFunctionToolDefinitions 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
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>
7f244e2 to
69a7598
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Description
Adds
function_choice_behaviorparameter to Azure AI and OpenAI Assistant agents, aligning them with the existing Responses agent and Chat Completion agent APIs.Changes
function_choice_behaviorparameter toinvoke/invoke_stream/get_responseon bothAzureAIAgentandOpenAIAssistantAgentpublic APIsfunction_choice_behaviorparameter to internalAzureAIAgentThreadActionsandAssistantThreadActionsinvoke/invoke_streammethodstoolsparameter override for SDK-level tools (CodeInterpreter, FileSearch, etc.)FunctionChoiceBehaviorconfigurationAutotype with auto-invoke enabled is supported_get_toolsin OpenAI path to use passedkernelparameter instead ofagent.kernel