Skip to content

Commit cbb6356

Browse files
moonbox3Copilot
andcommitted
Fix duplicate tool_calls messages in HandoffBuilder with tool approval (microsoft#4411)
HandoffAgentExecutor._clone_chat_agent() created cloned agents with empty context_providers, causing Agent._prepare_run_context() to auto-inject an InMemoryHistoryProvider. This provider independently loaded stored messages alongside the full conversation replay from _full_conversation, producing duplicate assistant tool_calls messages without matching tool responses. The OpenAI Chat Completions API rejects this with a 400 error. Fix: Suppress history providers in cloned handoff agents by replacing them with a disabled InMemoryHistoryProvider (load_messages=False, store_inputs=False, store_outputs=False). This prevents auto-injection while preserving non-history context providers. Co-authored-by: Copilot <[email protected]>
1 parent 02ba274 commit cbb6356

2 files changed

Lines changed: 104 additions & 3 deletions

File tree

‎python/packages/orchestrations/agent_framework_orchestrations/_handoff.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
from agent_framework import Agent, SupportsAgentRun
4141
from agent_framework._middleware import FunctionInvocationContext, FunctionMiddleware
42-
from agent_framework._sessions import AgentSession
42+
from agent_framework._sessions import AgentSession, BaseContextProvider, BaseHistoryProvider, InMemoryHistoryProvider
4343
from agent_framework._tools import FunctionTool, tool
4444
from agent_framework._types import AgentResponse, AgentResponseUpdate, Content, Message
4545
from agent_framework._workflows._agent_executor import AgentExecutor, AgentExecutorRequest, AgentExecutorResponse
@@ -390,12 +390,21 @@ def _clone_chat_agent(self, agent: Agent) -> Agent:
390390
"user": options.get("user"),
391391
}
392392

393+
# Handoff workflows manage full conversation state via _full_conversation.
394+
# Suppress history providers to prevent duplicate messages on approval resume.
395+
context_providers: list[BaseContextProvider] = [
396+
p for p in agent.context_providers if not isinstance(p, BaseHistoryProvider)
397+
]
398+
context_providers.append(
399+
InMemoryHistoryProvider(load_messages=False, store_inputs=False, store_outputs=False)
400+
)
401+
393402
return Agent(
394403
client=agent.client,
395404
id=agent.id,
396405
name=agent.name,
397406
description=agent.description,
398-
context_providers=agent.context_providers,
407+
context_providers=context_providers,
399408
middleware=middleware,
400409
default_options=cloned_options, # type: ignore[arg-type]
401410
)

‎python/packages/orchestrations/tests/test_handoff.py‎

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,99 @@ async def _get() -> ChatResponse:
472472
assert client.resume_validated is True
473473

474474

475-
async def test_handoff_replay_serializes_handoff_function_results() -> None:
475+
async def test_handoff_tool_approval_does_not_duplicate_tool_calls_messages() -> None:
476+
"""InMemoryHistoryProvider must not cause duplicate tool_calls on approval resume (#4411)."""
477+
478+
@tool(name="submit_refund", approval_mode="always_require")
479+
def submit_refund() -> str:
480+
return "ok"
481+
482+
class DuplicateDetectingClient(ChatMiddlewareLayer[Any], FunctionInvocationLayer[Any], BaseChatClient[Any]):
483+
def __init__(self) -> None:
484+
ChatMiddlewareLayer.__init__(self)
485+
FunctionInvocationLayer.__init__(self)
486+
BaseChatClient.__init__(self)
487+
self._call_index = 0
488+
self.resume_validated = False
489+
490+
def _inner_get_response(
491+
self,
492+
*,
493+
messages: Sequence[Message],
494+
stream: bool,
495+
options: Mapping[str, Any],
496+
**kwargs: Any,
497+
) -> Awaitable[ChatResponse] | ResponseStream[ChatResponseUpdate, ChatResponse]:
498+
del options
499+
del kwargs
500+
501+
if self._call_index == 0:
502+
contents = [
503+
Content.from_function_call(
504+
call_id="refund-call-1",
505+
name="submit_refund",
506+
arguments={},
507+
)
508+
]
509+
else:
510+
# Each assistant message with tool_calls must have a matching tool response.
511+
# Duplicate tool_calls without responses trigger an OpenAI 400 error.
512+
tool_call_ids: list[str] = []
513+
tool_result_ids: set[str] = set()
514+
for msg in messages:
515+
for content in msg.contents:
516+
if content.type == "function_call" and content.call_id:
517+
tool_call_ids.append(content.call_id)
518+
elif content.type == "function_result" and content.call_id:
519+
tool_result_ids.add(content.call_id)
520+
unmatched = [cid for cid in tool_call_ids if cid not in tool_result_ids]
521+
if unmatched:
522+
raise AssertionError(
523+
f"Assistant tool_calls without matching tool response: {unmatched}. "
524+
"This would cause a 400 error from the OpenAI Chat Completions API."
525+
)
526+
self.resume_validated = True
527+
contents = [Content.from_text(text="Refund submitted.")]
528+
529+
self._call_index += 1
530+
531+
if stream:
532+
533+
async def _stream() -> AsyncIterable[ChatResponseUpdate]:
534+
yield ChatResponseUpdate(contents=contents, role="assistant", finish_reason="stop")
535+
536+
return ResponseStream(_stream(), finalizer=lambda updates: ChatResponse.from_updates(updates))
537+
538+
async def _get() -> ChatResponse:
539+
return ChatResponse(
540+
messages=[Message(role="assistant", contents=contents)],
541+
response_id="dup-detect",
542+
)
543+
544+
return _get()
545+
546+
client = DuplicateDetectingClient()
547+
agent = Agent(
548+
id="refund_agent",
549+
name="refund_agent",
550+
client=client,
551+
tools=[submit_refund],
552+
)
553+
workflow = (
554+
HandoffBuilder(participants=[agent], termination_condition=lambda _: False).with_start_agent(agent).build()
555+
)
556+
557+
first_events = await _drain(workflow.run("Refund order 123", stream=True))
558+
approval_requests = [
559+
event for event in first_events if event.type == "request_info" and isinstance(event.data, Content)
560+
]
561+
assert approval_requests
562+
first_request = approval_requests[0]
563+
564+
approval_response = first_request.data.to_function_approval_response(True)
565+
await _drain(workflow.run(stream=True, responses={first_request.request_id: approval_response}))
566+
567+
assert client.resume_validated is True
476568
"""Returning to the same agent must not replay dict tool outputs."""
477569

478570
class ReplaySafeHandoffClient(ChatMiddlewareLayer[Any], FunctionInvocationLayer[Any], BaseChatClient[Any]):

0 commit comments

Comments
 (0)