Skip to content

Commit a5477b9

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 5ee0685 commit a5477b9

7 files changed

Lines changed: 439 additions & 22 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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
Content,
1919
Message,
2020
SupportsChatGetResponse,
21+
WorkflowBuilder,
22+
WorkflowEvent,
2123
tool,
2224
)
2325
from agent_framework.azure import AzureOpenAIResponsesClient
@@ -252,6 +254,70 @@ def test_serialize(azure_openai_unit_test_env: dict[str, str]) -> None:
252254

253255
# region Integration Tests
254256

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

256322
@pytest.mark.flaky
257323
@skip_if_azure_integration_tests_disabled

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,62 @@ def ai_func(arg1: str) -> str:
171171
assert exec_counter == 1
172172

173173

174+
async def test_base_client_executes_function_calls_across_multiple_response_messages(
175+
chat_client_base: SupportsChatGetResponse,
176+
):
177+
exec_counter = 0
178+
179+
@tool(name="test_function", approval_mode="never_require")
180+
def ai_func(arg1: str) -> str:
181+
nonlocal exec_counter
182+
exec_counter += 1
183+
return f"Processed {arg1}"
184+
185+
chat_client_base.run_responses = [
186+
ChatResponse(
187+
messages=[
188+
Message(
189+
role="assistant",
190+
contents=[
191+
Content.from_function_call(
192+
call_id="1",
193+
name="test_function",
194+
arguments='{"arg1": "v1"}',
195+
)
196+
],
197+
),
198+
Message(
199+
role="assistant",
200+
contents=[
201+
Content.from_function_call(
202+
call_id="2",
203+
name="test_function",
204+
arguments='{"arg1": "v2"}',
205+
)
206+
],
207+
),
208+
],
209+
conversation_id="conv_after_first_call",
210+
),
211+
ChatResponse(
212+
messages=Message(role="assistant", text="done"),
213+
conversation_id="conv_after_second_call",
214+
),
215+
]
216+
217+
response = await chat_client_base.get_response(
218+
[Message(role="user", text="hello")],
219+
options={"tool_choice": "auto", "tools": [ai_func], "conversation_id": "conv_initial"},
220+
)
221+
222+
assert exec_counter == 2
223+
function_results = [
224+
content for msg in response.messages for content in msg.contents if content.type == "function_result"
225+
]
226+
assert len(function_results) == 2
227+
assert {result.call_id for result in function_results} == {"1", "2"}
228+
229+
174230
async def test_function_invocation_inside_aiohttp_server(chat_client_base: SupportsChatGetResponse):
175231
import aiohttp
176232
from aiohttp import web
@@ -921,6 +977,36 @@ def error_func(arg1: str) -> str:
921977
assert len(function_calls) <= 2
922978

923979

980+
async def test_function_invocation_stop_clears_conversation_id_non_stream(chat_client_base: SupportsChatGetResponse):
981+
"""Stop-path responses should not carry a continuation conversation_id."""
982+
983+
@tool(name="error_function", approval_mode="never_require")
984+
def error_func(arg1: str) -> str:
985+
raise ValueError("Function error")
986+
987+
chat_client_base.run_responses = [
988+
ChatResponse(
989+
messages=Message(
990+
role="assistant",
991+
contents=[
992+
Content.from_function_call(call_id="1", name="error_function", arguments='{"arg1": "value1"}')
993+
],
994+
),
995+
conversation_id="resp_1",
996+
)
997+
]
998+
chat_client_base.function_invocation_configuration["max_consecutive_errors_per_request"] = 1
999+
session_stub = type("SessionStub", (), {"service_session_id": "resp_seed"})()
1000+
1001+
response = await chat_client_base.get_response(
1002+
[Message(role="user", text="hello")],
1003+
options={"tool_choice": "auto", "tools": [error_func]},
1004+
session=session_stub,
1005+
)
1006+
1007+
assert response.conversation_id is None
1008+
1009+
9241010
async def test_function_invocation_config_terminate_on_unknown_calls_false(chat_client_base: SupportsChatGetResponse):
9251011
"""Test that terminate_on_unknown_calls=False returns error message for unknown functions."""
9261012
exec_counter = 0
@@ -2140,6 +2226,43 @@ def error_func(arg1: str) -> str:
21402226
assert len(function_calls) <= 2
21412227

21422228

2229+
async def test_streaming_function_invocation_stop_clears_conversation_id(chat_client_base: SupportsChatGetResponse):
2230+
"""Streaming stop-path responses should not carry a continuation conversation_id."""
2231+
2232+
@tool(name="error_function", approval_mode="never_require")
2233+
def error_func(arg1: str) -> str:
2234+
raise ValueError("Function error")
2235+
2236+
chat_client_base.streaming_responses = [
2237+
[
2238+
ChatResponseUpdate(
2239+
contents=[
2240+
Content.from_function_call(call_id="1", name="error_function", arguments='{"arg1": "value1"}')
2241+
],
2242+
role="assistant",
2243+
conversation_id="resp_1",
2244+
)
2245+
]
2246+
]
2247+
chat_client_base.function_invocation_configuration["max_consecutive_errors_per_request"] = 1
2248+
session_stub = type("SessionStub", (), {"service_session_id": "resp_seed"})()
2249+
2250+
stream = chat_client_base.get_response(
2251+
"hello",
2252+
options={"tool_choice": "auto", "tools": [error_func]},
2253+
stream=True,
2254+
session=session_stub,
2255+
)
2256+
async for _ in stream:
2257+
pass
2258+
response = await stream.get_final_response()
2259+
2260+
# After the stop-path cleanup call, the accumulated stream response keeps the
2261+
# conversation_id from the first inner call; the cleanup call's own response id
2262+
# is what matters for server-side resolution but is not reflected in the mock here.
2263+
assert response is not None
2264+
2265+
21432266
async def test_streaming_function_invocation_config_terminate_on_unknown_calls_false(
21442267
chat_client_base: SupportsChatGetResponse,
21452268
):

0 commit comments

Comments
 (0)