Skip to content

Commit d98ac29

Browse files
atty57claude
andauthored
Python: Fix duplicate function call on approval round-trip (#7267) (#7271)
* Python: Fix duplicate function call on approval round-trip (#7267) `_replace_approval_contents_with_results` deduped restored function calls against only the message currently being scanned. On an approval round-trip the hosting layer replays the stored `function_call` item and its `mcp_approval_request` item as two separate assistant messages, so the per-message check never fired and the approval request restored a second copy of the call. Only one copy received the function result; the orphaned copy was left unanswered, which the Responses API rejects with "No tool output found for function call call_<id>". Collect existing call ids across all messages instead, and add a restored call to that set so two approval requests for the same call cannot both expand. Co-Authored-By: Claude Opus 4.8 <[email protected]> * Refactor approval placeholder result handling Refactor approval handling logic to improve clarity and maintainability. * Refactor test to support reused call IDs after completion Updated the test to allow reused call IDs after completion, ensuring that a completed call does not suppress later approval requests with the same ID. Adjusted assertions to reflect the new behavior. --------- Co-authored-by: Claude Opus 4.8 <[email protected]>
1 parent 040e270 commit d98ac29

2 files changed

Lines changed: 68 additions & 11 deletions

File tree

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,12 @@ def _collect_approval_responses(
20962096
return fcc_todo
20972097

20982098

2099+
def _is_approval_placeholder_result(content: Content) -> bool:
2100+
"""Whether a function_result is the stand-in emitted while approval is pending."""
2101+
result = getattr(content, "result", None)
2102+
return isinstance(result, str) and "[APPROVAL_PENDING]" in result
2103+
2104+
20992105
def _replace_approval_contents_with_results(
21002106
messages: list[Message],
21012107
fcc_todo: dict[str, Content],
@@ -2119,12 +2125,30 @@ def _replace_approval_contents_with_results(
21192125
# Track which call_ids had their placeholders replaced
21202126
placeholders_replaced: set[str] = set()
21212127

2122-
for msg in messages:
2123-
# First pass - collect existing function call IDs to avoid duplicates
2124-
existing_call_ids = {
2125-
content.call_id for content in msg.contents if content.type == "function_call" and content.call_id
2126-
}
2128+
# Collect *pending* function call IDs across all messages to avoid duplicates. The
2129+
# function call and its approval request are frequently carried in separate messages
2130+
# (e.g. when a hosting layer replays them as separate items on an approval round trip),
2131+
# so scoping this per-message would let the same call_id be restored twice and leave
2132+
# the copy without a result unanswered.
2133+
#
2134+
# Calls that already carry a real result are excluded: reusing a call_id for a later
2135+
# invocation is supported, and a completed pair must not suppress the fresh request —
2136+
# that would drop the new call and attach its result to the old one. Placeholder
2137+
# results still count as pending, since the call they answer is the one being restored.
2138+
answered_call_ids = {
2139+
content.call_id
2140+
for msg in messages
2141+
for content in msg.contents
2142+
if content.type == "function_result" and content.call_id and not _is_approval_placeholder_result(content)
2143+
}
2144+
existing_call_ids = {
2145+
content.call_id
2146+
for msg in messages
2147+
for content in msg.contents
2148+
if content.type == "function_call" and content.call_id and content.call_id not in answered_call_ids
2149+
}
21272150

2151+
for msg in messages:
21282152
# Track approval requests that should be removed (duplicates)
21292153
contents_to_remove: list[int] = []
21302154

@@ -2140,6 +2164,8 @@ def _replace_approval_contents_with_results(
21402164
elif content.function_call is not None:
21412165
# Put back the function call content only if it doesn't exist
21422166
msg.contents[content_idx] = content.function_call
2167+
if content.function_call.call_id:
2168+
existing_call_ids.add(content.function_call.call_id)
21432169
elif content.type == "function_approval_response":
21442170
# Skip hosted tool approvals — they must pass through to the API unchanged
21452171
if _is_hosted_tool_approval(content):
@@ -2169,12 +2195,7 @@ def _replace_approval_contents_with_results(
21692195
msg.role = "tool"
21702196
elif content.type == "function_result":
21712197
# Check if this is a placeholder result that should be replaced
2172-
if (
2173-
hasattr(content, "result")
2174-
and isinstance(content.result, str)
2175-
and "[APPROVAL_PENDING]" in content.result
2176-
and content.call_id in result_by_call_id
2177-
):
2198+
if _is_approval_placeholder_result(content) and content.call_id in result_by_call_id:
21782199
# Replace placeholder with actual result
21792200
msg.contents[content_idx] = result_by_call_id[content.call_id]
21802201
placeholders_replaced.add(content.call_id)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,42 @@ def test_replace_approval_contents_with_results_uses_result_call_ids_without_pla
23382338
]
23392339

23402340

2341+
def test_replace_approval_contents_with_results_allows_reused_call_id_after_completion() -> None:
2342+
"""A completed call must not suppress a later approval request that reuses its id.
2343+
2344+
Re-approving the same ``(call_id, function)`` is supported behaviour. If the dedupe
2345+
matched every occurrence of the id, the fresh request would be dropped and its result
2346+
attached to the already-answered call, leaving one call with two results.
2347+
"""
2348+
from agent_framework._tools import _collect_approval_responses, _replace_approval_contents_with_results
2349+
2350+
completed_call = Content.from_function_call(call_id="call_reused", name="run_skill_script", arguments="{}")
2351+
completed_result = Content.from_function_result(call_id="call_reused", result="first output")
2352+
_, request, response = _build_approved_tool_roundtrip(
2353+
call_id="call_reused", approval_id="approval_2", tool_name="run_skill_script"
2354+
)
2355+
2356+
messages = [
2357+
Message(role="assistant", contents=[completed_call]),
2358+
Message(role="tool", contents=[completed_result]),
2359+
Message(role="assistant", contents=[request]),
2360+
Message(role="user", contents=[response]),
2361+
]
2362+
2363+
_replace_approval_contents_with_results(
2364+
messages,
2365+
_collect_approval_responses(messages),
2366+
[Content.from_function_result(call_id="call_reused", result="second output")],
2367+
)
2368+
2369+
function_calls = [c for m in messages for c in m.contents if c.type == "function_call"]
2370+
assert [c.call_id for c in function_calls] == ["call_reused", "call_reused"]
2371+
results = [c for m in messages for c in m.contents if c.type == "function_result"]
2372+
assert [(c.call_id, c.result) for c in results] == [
2373+
("call_reused", "first output"),
2374+
("call_reused", "second output"),
2375+
]
2376+
23412377
def test_replace_approval_contents_with_results_uses_result_call_ids_for_placeholders() -> None:
23422378
from agent_framework._tools import _collect_approval_responses, _replace_approval_contents_with_results
23432379

0 commit comments

Comments
 (0)