Commit 67ce1ba
Python: fix reasoning model workflow handoff and history serialization (#4083)
* 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: #4047
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* fix: reasoning model workflow handoff and history serialization
Fixes multiple related issues when using reasoning models (gpt-5-mini,
gpt-5.2) in multi-agent workflows that chain agents via from_response
or replay full conversation history via AgentExecutorRequest.
## Reasoning items always emitted on output_item.added
When a reasoning model produces encrypted or hidden reasoning (no
visible text), the Responses API still fires a reasoning output item
without any reasoning_text.delta events. Previously no text_reasoning
Content was emitted in that case, making it invisible to downstream
logic. Both the non-streaming (_parse_response_from_openai) and
streaming (output_item.added) paths now always emit at least one
text_reasoning Content — with empty text if no content is available —
so co-occurrence detection and serialization guards work reliably.
## Reasoning items only serialized when paired with a function_call
The Responses API only accepts reasoning items in input when they
directly preceded a function_call in the original response. Sending a
reasoning item that preceded a text response (no tool call) causes:
"reasoning was provided without its required following item"
_prepare_message_for_openai now checks has_function_call per message
and skips text_reasoning serialization when there is no accompanying
function_call.
## summary field is an array, not an object
The reasoning item summary field sent to the Responses API must be an
array of objects ([{"type": "summary_text", "text": ...}]), not a
single object. Fixed _prepare_content_for_openai accordingly.
## service_session_id cleared when explicit history is provided
When a workflow coordinator replays a full conversation (including
function calls from a previous agent run) back to an executor via
AgentExecutorRequest or from_response, the executor's session still
held a service_session_id (previous_response_id) from the prior run.
The API then received the same function-call items twice — once from
previous_response_id (server-stored) and once from the explicit input —
causing: "Duplicate item found with id fc_...".
AgentExecutor.run (when should_respond=True) and from_response now
reset self._session.service_session_id = None before running so that
explicit input is the sole source of conversation context.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* small improvements in text reasoning
* refactor: add reset_service_session to AgentExecutorRequest for explicit history replay
Replace the implicit 'always clear service_session_id when should_respond=True'
with an explicit opt-in field on AgentExecutorRequest.
The old approach used should_respond=True as a proxy for 'full history replay',
but that conflates two distinct intents:
- Orchestrations group chat sends should_respond=True with an empty/single-message
list (not a full replay) — unnecessarily clearing service_session_id.
- HITL / feedback coordinators send the full prior conversation and truly need
a fresh service session ID to avoid duplicate-item API errors.
Changes:
- Add AgentExecutorRequest.reset_service_session: bool = False
- AgentExecutor.run only clears service_session_id when this flag is True
- AgentExecutor.from_response unchanged (always clears; always full conversation)
- Set reset_service_session=True in all full-history-replay call sites:
agents_with_HITL.py, azure_chat_agents_tool_calls_with_feedback.py,
autogen-migration round-robin coordinator, tau2 runner
- Update _FullHistoryReplayCoordinator test helper to pass the flag
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* comment update
* fixes from feedback
* fix test
* reverted changes to agent executor
* fix: remove reset_service_session from tau2 runner
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* two other reverts
* fix sample
---------
Co-authored-by: Giles Odigwe <79032838+giles17@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 2cb4137 commit 67ce1ba
11 files changed
Lines changed: 445 additions & 66 deletions
File tree
- python
- packages/core
- agent_framework
- _workflows
- openai
- tests
- core
- openai
- workflow
- samples
- 02-agents/conversations
- 03-workflows
- agents
- human-in-the-loop
- autogen-migration/orchestrations
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1761 | 1761 | | |
1762 | 1762 | | |
1763 | 1763 | | |
1764 | | - | |
1765 | | - | |
1766 | | - | |
1767 | | - | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
| 1771 | + | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
| 1777 | + | |
| 1778 | + | |
| 1779 | + | |
| 1780 | + | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
1768 | 1784 | | |
1769 | 1785 | | |
1770 | 1786 | | |
| |||
1822 | 1838 | | |
1823 | 1839 | | |
1824 | 1840 | | |
1825 | | - | |
| 1841 | + | |
| 1842 | + | |
1826 | 1843 | | |
1827 | 1844 | | |
1828 | 1845 | | |
1829 | 1846 | | |
1830 | 1847 | | |
1831 | | - | |
1832 | | - | |
1833 | | - | |
1834 | | - | |
1835 | | - | |
1836 | | - | |
1837 | | - | |
1838 | 1848 | | |
1839 | 1849 | | |
| 1850 | + | |
1840 | 1851 | | |
1841 | 1852 | | |
1842 | 1853 | | |
1843 | 1854 | | |
1844 | 1855 | | |
1845 | | - | |
| 1856 | + | |
1846 | 1857 | | |
1847 | 1858 | | |
1848 | 1859 | | |
| |||
2025 | 2036 | | |
2026 | 2037 | | |
2027 | 2038 | | |
| 2039 | + | |
2028 | 2040 | | |
2029 | 2041 | | |
2030 | 2042 | | |
| |||
2090 | 2102 | | |
2091 | 2103 | | |
2092 | 2104 | | |
2093 | | - | |
| 2105 | + | |
| 2106 | + | |
| 2107 | + | |
2094 | 2108 | | |
2095 | 2109 | | |
2096 | 2110 | | |
| |||
2157 | 2171 | | |
2158 | 2172 | | |
2159 | 2173 | | |
| 2174 | + | |
2160 | 2175 | | |
2161 | 2176 | | |
2162 | 2177 | | |
| |||
2205 | 2220 | | |
2206 | 2221 | | |
2207 | 2222 | | |
2208 | | - | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
2209 | 2228 | | |
2210 | 2229 | | |
2211 | 2230 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
531 | 531 | | |
532 | 532 | | |
533 | 533 | | |
| 534 | + | |
534 | 535 | | |
535 | 536 | | |
536 | 537 | | |
| |||
540 | 541 | | |
541 | 542 | | |
542 | 543 | | |
| 544 | + | |
543 | 545 | | |
544 | 546 | | |
545 | 547 | | |
| |||
Lines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| |||
311 | 311 | | |
312 | 312 | | |
313 | 313 | | |
314 | | - | |
| 314 | + | |
315 | 315 | | |
316 | 316 | | |
317 | 317 | | |
| |||
Lines changed: 57 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
908 | 908 | | |
909 | 909 | | |
910 | 910 | | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
911 | 916 | | |
912 | 917 | | |
913 | 918 | | |
914 | | - | |
915 | | - | |
| 919 | + | |
| 920 | + | |
916 | 921 | | |
917 | 922 | | |
918 | 923 | | |
| |||
961 | 966 | | |
962 | 967 | | |
963 | 968 | | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
968 | | - | |
969 | | - | |
970 | | - | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
971 | 972 | | |
972 | 973 | | |
973 | | - | |
974 | | - | |
975 | 974 | | |
976 | 975 | | |
977 | 976 | | |
978 | | - | |
979 | | - | |
980 | | - | |
981 | | - | |
| 977 | + | |
982 | 978 | | |
983 | 979 | | |
| 980 | + | |
| 981 | + | |
984 | 982 | | |
985 | 983 | | |
986 | 984 | | |
| |||
1189 | 1187 | | |
1190 | 1188 | | |
1191 | 1189 | | |
1192 | | - | |
1193 | | - | |
1194 | | - | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
1195 | 1193 | | |
1196 | | - | |
1197 | | - | |
1198 | 1194 | | |
1199 | 1195 | | |
1200 | 1196 | | |
1201 | 1197 | | |
| 1198 | + | |
1202 | 1199 | | |
1203 | 1200 | | |
1204 | 1201 | | |
1205 | 1202 | | |
1206 | 1203 | | |
1207 | | - | |
1208 | | - | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
1209 | 1207 | | |
1210 | 1208 | | |
| 1209 | + | |
1211 | 1210 | | |
1212 | 1211 | | |
1213 | | - | |
1214 | 1212 | | |
1215 | 1213 | | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
1216 | 1229 | | |
1217 | 1230 | | |
1218 | 1231 | | |
| |||
1427 | 1440 | | |
1428 | 1441 | | |
1429 | 1442 | | |
| 1443 | + | |
1430 | 1444 | | |
1431 | 1445 | | |
1432 | | - | |
1433 | 1446 | | |
1434 | 1447 | | |
1435 | 1448 | | |
1436 | 1449 | | |
1437 | 1450 | | |
1438 | 1451 | | |
| 1452 | + | |
1439 | 1453 | | |
1440 | 1454 | | |
1441 | | - | |
1442 | 1455 | | |
1443 | 1456 | | |
1444 | 1457 | | |
1445 | 1458 | | |
1446 | 1459 | | |
1447 | 1460 | | |
| 1461 | + | |
1448 | 1462 | | |
1449 | 1463 | | |
1450 | | - | |
1451 | 1464 | | |
1452 | 1465 | | |
1453 | 1466 | | |
1454 | 1467 | | |
1455 | 1468 | | |
1456 | 1469 | | |
| 1470 | + | |
1457 | 1471 | | |
1458 | 1472 | | |
1459 | | - | |
1460 | 1473 | | |
1461 | 1474 | | |
1462 | 1475 | | |
| |||
1630 | 1643 | | |
1631 | 1644 | | |
1632 | 1645 | | |
| 1646 | + | |
1633 | 1647 | | |
1634 | 1648 | | |
1635 | 1649 | | |
1636 | | - | |
1637 | | - | |
1638 | 1650 | | |
1639 | 1651 | | |
1640 | 1652 | | |
| |||
1643 | 1655 | | |
1644 | 1656 | | |
1645 | 1657 | | |
| 1658 | + | |
1646 | 1659 | | |
1647 | 1660 | | |
1648 | 1661 | | |
1649 | 1662 | | |
1650 | 1663 | | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
1651 | 1679 | | |
1652 | 1680 | | |
1653 | 1681 | | |
| |||
0 commit comments