Skip to content

Commit 89b36ad

Browse files
giles17Copilot
authored andcommitted
fix: strip function_call and text_reasoning from cross-agent workflow handoff
When a reasoning model (e.g. gpt-5-mini) runs as Agent 1 in a workflow, its response includes text_reasoning items (with server-scoped IDs like rs_XXXX) and function_call items. Forwarding these to Agent 2 in a fresh conversation caused API errors because the reasoning/call IDs are scoped to the original stored response context. Changes: - Strip 'function_call', 'text_reasoning', 'function_approval_request', and 'function_approval_response' from handoff messages in _agent_executor.py - Keep 'function_result' so the actual tool output content is preserved for the next agent's context - Update unit tests to reflect that function_result messages survive handoff (messages grow from 2→3: user, tool(result), assistant(summary)) - Fix incorrect test assertions in test_function_invocation_stop_clears_* that assumed the client layer updates session.service_session_id - Also fixed _extract_function_calls to search all messages with call_id deduplication, and the error-limit stop path to submit function_call_output items before halting (via tool_choice=none cleanup call) Relates to: microsoft#4047 Co-authored-by: Copilot <[email protected]>
1 parent 6fd5046 commit 89b36ad

7 files changed

Lines changed: 446 additions & 29 deletions

File tree

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,10 +1761,26 @@ def _get_result_hooks_from_stream(stream: Any) -> list[Callable[[Any], Any]]:
17611761

17621762

17631763
def _extract_function_calls(response: ChatResponse) -> list[Content]:
1764-
function_results = {it.call_id for it in response.messages[0].contents if it.type == "function_result"}
1765-
return [
1766-
it for it in response.messages[0].contents if it.type == "function_call" and it.call_id not in function_results
1767-
]
1764+
function_results = {
1765+
item.call_id
1766+
for message in response.messages
1767+
for item in message.contents
1768+
if item.type == "function_result" and item.call_id
1769+
}
1770+
seen_call_ids: set[str] = set()
1771+
function_calls: list[Content] = []
1772+
for message in response.messages:
1773+
for item in message.contents:
1774+
if item.type != "function_call":
1775+
continue
1776+
if item.call_id and item.call_id in function_results:
1777+
continue
1778+
if item.call_id and item.call_id in seen_call_ids:
1779+
continue
1780+
if item.call_id:
1781+
seen_call_ids.add(item.call_id)
1782+
function_calls.append(item)
1783+
return function_calls
17681784

17691785

17701786
def _prepend_fcc_messages(response: ChatResponse, fcc_messages: list[Message]) -> None:
@@ -1822,27 +1838,22 @@ def _handle_function_call_results(
18221838

18231839
if had_errors:
18241840
errors_in_a_row += 1
1825-
if errors_in_a_row >= max_errors:
1841+
reached_error_limit = errors_in_a_row >= max_errors
1842+
if reached_error_limit:
18261843
logger.warning(
18271844
"Maximum consecutive function call errors reached (%d). "
18281845
"Stopping further function calls for this request.",
18291846
max_errors,
18301847
)
1831-
return {
1832-
"action": "stop",
1833-
"errors_in_a_row": errors_in_a_row,
1834-
"result_message": None,
1835-
"update_role": None,
1836-
"function_call_results": None,
1837-
}
18381848
else:
18391849
errors_in_a_row = 0
1850+
reached_error_limit = False
18401851

18411852
result_message = Message(role="tool", contents=function_call_results)
18421853
response.messages.append(result_message)
18431854
fcc_messages.extend(response.messages)
18441855
return {
1845-
"action": "continue",
1856+
"action": "stop" if reached_error_limit else "continue",
18461857
"errors_in_a_row": errors_in_a_row,
18471858
"result_message": result_message,
18481859
"update_role": "tool",
@@ -2025,6 +2036,7 @@ def get_response(
20252036
middleware_pipeline=function_middleware_pipeline,
20262037
)
20272038
filtered_kwargs = {k: v for k, v in kwargs.items() if k != "session"}
2039+
20282040
# Make options mutable so we can update conversation_id during function invocation loop
20292041
mutable_options: dict[str, Any] = dict(options) if options else {}
20302042
# Remove additional_function_arguments from options passed to underlying chat client
@@ -2090,7 +2102,9 @@ async def _get_response() -> ChatResponse:
20902102
if result["action"] == "return":
20912103
return response
20922104
if result["action"] == "stop":
2093-
break
2105+
# Error threshold reached: force a final non-tool turn so
2106+
# function_call_output items are submitted before exit.
2107+
mutable_options["tool_choice"] = "none"
20942108
errors_in_a_row = result["errors_in_a_row"]
20952109

20962110
# When tool_choice is 'required', reset tool_choice after one iteration to avoid infinite loops
@@ -2157,6 +2171,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
21572171
)
21582172
errors_in_a_row = approval_result["errors_in_a_row"]
21592173
if approval_result["action"] == "stop":
2174+
mutable_options["tool_choice"] = "none"
21602175
return
21612176

21622177
inner_stream = await _ensure_response_stream(
@@ -2205,7 +2220,11 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
22052220
contents=result["function_call_results"] or [],
22062221
role=role,
22072222
)
2208-
if result["action"] != "continue":
2223+
if result["action"] == "stop":
2224+
# Error threshold reached: submit collected function_call_output
2225+
# items once more with tools disabled.
2226+
mutable_options["tool_choice"] = "none"
2227+
elif result["action"] != "continue":
22092228
return
22102229

22112230
# When tool_choice is 'required', reset the tool_choice after one iteration to avoid infinite loops

‎python/packages/core/agent_framework/_workflows/_agent_executor.py‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import logging
44
import sys
5-
from collections.abc import Awaitable, Callable, Mapping
5+
from collections.abc import Awaitable, Callable, Mapping, Sequence
66
from dataclasses import dataclass
7-
from typing import Any, cast
7+
from typing import Any, ClassVar, cast
88

99
from typing_extensions import Never
1010

@@ -144,12 +144,43 @@ async def from_response(
144144
immediately run the agent to produce a new response.
145145
"""
146146
# Replace cache with full conversation if available, else fall back to agent_response messages.
147-
if prior.full_conversation is not None:
148-
self._cache = list(prior.full_conversation)
149-
else:
150-
self._cache = list(prior.agent_response.messages)
147+
# Filter out tool-call internals that are not replay-safe across agent boundaries.
148+
source_messages = (
149+
prior.full_conversation if prior.full_conversation is not None else prior.agent_response.messages
150+
)
151+
self._cache = self._prepare_handoff_messages(source_messages)
151152
await self._run_agent_and_emit(ctx)
152153

154+
@classmethod
155+
def _prepare_handoff_messages(cls, messages: Sequence[Message]) -> list[Message]:
156+
"""Prepare cross-executor handoff messages for replay safety.
157+
158+
Cross-agent workflow handoff should preserve user intent and assistant outcomes,
159+
but avoid replaying internal tool-call protocol messages that can be invalid
160+
outside their original response loop.
161+
"""
162+
prepared: list[Message] = []
163+
for message in messages:
164+
filtered_contents = [
165+
content for content in message.contents if content.type not in cls._HANDOFF_EXCLUDED_CONTENT_TYPES
166+
]
167+
if not filtered_contents:
168+
continue
169+
if len(filtered_contents) == len(message.contents):
170+
prepared.append(message)
171+
continue
172+
prepared.append(
173+
Message(
174+
role=message.role,
175+
contents=filtered_contents,
176+
author_name=message.author_name,
177+
message_id=message.message_id,
178+
additional_properties=dict(message.additional_properties),
179+
raw_representation=message.raw_representation,
180+
)
181+
)
182+
return prepared
183+
153184
@handler
154185
async def from_str(
155186
self, text: str, ctx: WorkflowContext[AgentExecutorResponse, AgentResponse | AgentResponseUpdate]
@@ -466,3 +497,10 @@ def _prepare_agent_run_args(raw_run_kwargs: dict[str, Any]) -> tuple[dict[str, A
466497
options["additional_function_arguments"] = additional_args
467498

468499
return run_kwargs, options or None
500+
501+
_HANDOFF_EXCLUDED_CONTENT_TYPES: ClassVar[frozenset[str]] = frozenset({
502+
"function_call",
503+
"function_approval_request",
504+
"function_approval_response",
505+
"text_reasoning",
506+
})

‎python/packages/core/tests/azure/test_azure_responses_client.py‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
Content,
1919
Message,
2020
SupportsChatGetResponse,
21+
WorkflowBuilder,
22+
WorkflowEvent,
2123
tool,
2224
)
2325
from agent_framework.azure import AzureOpenAIResponsesClient
24-
from agent_framework.exceptions import ServiceInitializationError
26+
from agent_framework.exceptions import ServiceInitializationError, ServiceResponseException
2527

2628
skip_if_azure_integration_tests_disabled = pytest.mark.skipif(
2729
os.getenv("RUN_INTEGRATION_TESTS", "false").lower() != "true"
@@ -253,6 +255,70 @@ def test_serialize(azure_openai_unit_test_env: dict[str, str]) -> None:
253255

254256
# region Integration Tests
255257

258+
WORKFLOW_REASONING_DEPLOYMENT_NAME = os.getenv(
259+
"AZURE_OPENAI_WORKFLOW_REASONING_DEPLOYMENT_NAME",
260+
os.getenv("AZURE_OPENAI_REASONING_DEPLOYMENT_NAME", "gpt-5-mini"),
261+
)
262+
WORKFLOW_NON_REASONING_DEPLOYMENT_NAME = os.getenv(
263+
"AZURE_OPENAI_WORKFLOW_NON_REASONING_DEPLOYMENT_NAME",
264+
os.getenv("AZURE_OPENAI_NON_REASONING_DEPLOYMENT_NAME", "gpt-4.1-nano"),
265+
)
266+
267+
268+
async def _run_minimal_handoff_workflow(client: AzureOpenAIResponsesClient) -> list[WorkflowEvent]:
269+
first_agent = client.as_agent(
270+
id="minimal-handoff-first",
271+
name="minimal-handoff-first",
272+
instructions="Use get_weather exactly once for Seattle, then provide a short summary.",
273+
tools=[get_weather],
274+
default_options={"tool_choice": {"mode": "required", "required_function_name": "get_weather"}},
275+
)
276+
second_agent = client.as_agent(
277+
id="minimal-handoff-second",
278+
name="minimal-handoff-second",
279+
instructions="Summarize the prior weather result in one sentence.",
280+
)
281+
282+
workflow = WorkflowBuilder(start_executor=first_agent).add_edge(first_agent, second_agent).build()
283+
284+
events: list[WorkflowEvent] = []
285+
async for event in workflow.run("Check weather for Seattle and pass result onward.", stream=True):
286+
events.append(event)
287+
288+
return events
289+
290+
291+
@pytest.mark.timeout(600)
292+
@pytest.mark.flaky
293+
@skip_if_azure_integration_tests_disabled
294+
@pytest.mark.parametrize(
295+
"deployment_name",
296+
[
297+
param(WORKFLOW_REASONING_DEPLOYMENT_NAME, id="reasoning_gpt_5_mini"),
298+
param(WORKFLOW_NON_REASONING_DEPLOYMENT_NAME, id="non_reasoning_gpt_4_1_nano"),
299+
],
300+
)
301+
async def test_integration_minimal_workflow_handoff_reasoning_vs_non_reasoning(deployment_name: str) -> None:
302+
"""Smallest workflow handoff repro should pass across model classes."""
303+
client = AzureOpenAIResponsesClient(credential=AzureCliCredential(), deployment_name=deployment_name)
304+
client.function_invocation_configuration["max_iterations"] = 3
305+
306+
try:
307+
events = await _run_minimal_handoff_workflow(client)
308+
except ServiceResponseException as ex:
309+
error_text = str(ex).lower()
310+
if (
311+
"deploymentnotfound" in error_text
312+
or "deployment for this resource does not exist" in error_text
313+
or ("deployment" in error_text and "not found" in error_text)
314+
):
315+
pytest.skip(f"Deployment '{deployment_name}' is unavailable in this environment: {ex}")
316+
raise AssertionError(
317+
f"Minimal workflow handoff failed unexpectedly for deployment '{deployment_name}': {ex}"
318+
) from ex
319+
320+
assert any(event.type == "output" for event in events), "Expected workflow output event for workflow run"
321+
256322

257323
@pytest.mark.flaky
258324
@skip_if_azure_integration_tests_disabled

0 commit comments

Comments
 (0)