Skip to content

Commit d3c94e7

Browse files
committed
fix: preserve unknown-call termination after approval resume
1 parent 47f1912 commit d3c94e7

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎python/packages/core/agent_framework/_tools.py‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,41 +1777,38 @@ async def _try_execute_function_call_groups(
17771777
)
17781778
declaration_only_tool_names = {tool_name for tool_name, tool in tool_map.items() if tool.declaration_only}
17791779
additional_tool_names = {tool.name for tool in config.get("additional_tools") or []}
1780-
actionable_calls = [
1781-
function_call for function_call in function_calls if _is_actionable_function_call(function_call)
1782-
]
1783-
17841780
# Classify the entire batch first: any required user interaction pauses the batch before execution.
17851781
# Scan every call before deciding so classification travels with each call, not with its position in
17861782
# the batch. Priority (highest first): approval pause > declaration-only user-input > unknown-call
17871783
# termination. A user-input pause therefore takes precedence over unknown-call termination in mixed batches.
17881784
requires_approval = False
17891785
has_declaration_only_call = False
1786+
unknown_call_found = False
17901787
unknown_call_name: str | None = None
1791-
for function_call in actionable_calls:
1792-
function_name = function_call.name
1788+
for function_call in function_calls:
1789+
source_function_call = _underlying_function_call(function_call)
1790+
function_name = source_function_call.name
17931791
logger.debug(
17941792
"Checking function call: type=%s, name=%s, in approval_tools=%s",
17951793
function_call.type,
17961794
function_name,
17971795
function_name in approval_tool_names,
17981796
)
1799-
if function_name in approval_tool_names:
1797+
if _is_actionable_function_call(function_call) and function_name in approval_tool_names:
18001798
logger.debug("Approval needed for function: %s", function_name)
18011799
requires_approval = True
18021800
continue
1803-
if function_name in declaration_only_tool_names or function_name in additional_tool_names:
1801+
if _is_actionable_function_call(function_call) and (
1802+
function_name in declaration_only_tool_names or function_name in additional_tool_names
1803+
):
18041804
has_declaration_only_call = True
18051805
continue
1806-
if (
1807-
unknown_call_name is None
1808-
and config.get("terminate_on_unknown_calls", False)
1809-
and function_name not in tool_map
1810-
):
1806+
if not unknown_call_found and config.get("terminate_on_unknown_calls", False) and function_name not in tool_map:
1807+
unknown_call_found = True
18111808
unknown_call_name = function_name
18121809
# Defer unknown-call termination until the whole batch is classified so a higher-priority approval or
18131810
# declaration-only pause anywhere in the batch is not skipped by an earlier unknown call.
1814-
if not requires_approval and not has_declaration_only_call and unknown_call_name is not None:
1811+
if not requires_approval and not has_declaration_only_call and unknown_call_found:
18151812
raise KeyError(f'Error: Requested function "{unknown_call_name}" not found.')
18161813
if requires_approval:
18171814
# Surface only the approvals the host must decide; session-backed safe siblings wait for that resume.

‎python/packages/core/tests/core/test_function_invocation_logic.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,6 +2895,26 @@ def approval_func(arg1: str) -> str:
28952895
]
28962896
assert len(approval_requests) >= 1, "approval pause must take precedence over unknown-call termination"
28972897

2898+
approval_responses = [request.to_function_approval_response(approved=True) for request in approval_requests]
2899+
with pytest.raises(KeyError, match='Requested function "unknown_function" not found'):
2900+
await chat_client_base.get_response(
2901+
[Message(role="user", contents=approval_responses)],
2902+
options={"tool_choice": "auto", "tools": [approval_func]},
2903+
)
2904+
2905+
2906+
async def test_nameless_call_honors_unknown_call_termination():
2907+
"""A nameless call is still unknown and must terminate when configured to do so."""
2908+
from agent_framework._tools import _try_execute_function_call_groups
2909+
2910+
with pytest.raises(KeyError, match='Requested function "None" not found'):
2911+
await _try_execute_function_call_groups(
2912+
custom_args={},
2913+
function_calls=[Content("function_call", call_id="nameless-call", arguments="{}")],
2914+
tools=[],
2915+
config={"terminate_on_unknown_calls": True},
2916+
)
2917+
28982918

28992919
async def test_function_invocation_config_additional_tools(chat_client_base: SupportsChatGetResponse):
29002920
"""Test that additional_tools are available but treated as declaration_only."""

0 commit comments

Comments
 (0)