Skip to content

Commit 09a3d0d

Browse files
authored
Python: Strip server-issued response item IDs under storage (microsoft#3295) (microsoft#5690)
Fixes microsoft#3295. When the OpenAI Responses chat client sends a request that carries previous_response_id / conversation_id / conversation, the server already has the prior turn's response items and rejects duplicates with "Duplicate item found with id fc_xxx". The chat client was re-sending them inline whenever the input messages still carried the items in additional_properties (workflow replay, history providers, etc.), which broke any tool-using agent with persistent history. Decisions: - Single chokepoint: _prepare_message_for_openai. When the resulting request uses service-side storage, drop function_call, reasoning, approval-request/response, and local-shell-call items from the wire input. Keep function_result with its call_id; the server pairs it to the prior function_call via that key. - function_result is preserved unconditionally except for the local-shell variant, which carries its own server-issued item id. - No public API change. Wire format change is subtractive and only on requests that would otherwise 400. - Re-pointed the strict-xfail in test_full_conversation.py from microsoft#4047 to microsoft#3295. Kept xfail because the test asserts executor-level session-id clearing, which is the defense-in-depth half tracked by 3295-03; this slice closes the wire-level half. Files: - python/packages/openai/agent_framework_openai/_chat_client.py: strip rule applied alongside the existing reasoning-item branch. - python/packages/openai/tests/openai/test_openai_chat_client.py: four new tests pin the contract (function_call, approval, local-shell-call stripped under storage; everything kept without storage). Updated pre-existing tests that exercised the storage-on path to either pass request_uses_service_side_storage=False explicitly or assert the new strip behavior. - python/packages/foundry/tests/foundry/test_foundry_chat_client.py: same explicit storage-off opt-in for the inherited test. - python/packages/core/tests/workflow/test_full_conversation.py: re-pointed xfail reason to microsoft#3295 and the executor-level follow-up. Notes for next iteration: - 3295-01 (HITL wire-format validation against live OpenAI/Foundry) was not run; it requires the user's API credentials. The PRD design is locked but the empirical confirmation is still pending. If script 3 fails on either provider, this slice may need to be revisited. - 3295-03 (clear service_session_id in AgentExecutor on full-history replay) remains open. After it lands the xfail in test_full_conversation.py can be removed. - pytest was not run in this iteration because uv-based pytest commands required interactive approval. Validation rests on careful reading; next iteration should run the openai + core test suites.
1 parent ab09246 commit 09a3d0d

4 files changed

Lines changed: 319 additions & 200 deletions

File tree

‎python/packages/core/tests/workflow/test_full_conversation.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,12 @@ async def handle(
429429

430430

431431
@pytest.mark.xfail(
432-
reason="reset_service_session support not yet implemented — see #4047",
432+
reason=(
433+
"Tracks the executor-layer half of #3295: AgentExecutor should clear service_session_id "
434+
"when handed a full prior conversation. The wire-level 'Duplicate item' API error is "
435+
"already closed by the chat-client strip in #3295; this xfail covers the defense-in-depth "
436+
"follow-up that makes the executor wiring reflect intent."
437+
),
433438
strict=True,
434439
)
435440
async def test_run_request_with_full_history_clears_service_session_id() -> None:

‎python/packages/foundry/tests/foundry/test_foundry_chat_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ async def test_chat_message_parsing_with_function_calls() -> None:
435435
Message(role="tool", contents=[function_result]),
436436
]
437437

438-
prepared_messages = client._prepare_messages_for_openai(messages)
438+
prepared_messages = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=False)
439439

440440
assert prepared_messages == [
441441
{

‎python/packages/openai/agent_framework_openai/_chat_client.py‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,29 +1409,31 @@ def _prepare_message_for_openai(
14091409
}
14101410
additional_properties = message.additional_properties
14111411
replays_local_storage = "_attribution" in additional_properties
1412-
uses_service_side_storage = request_uses_service_side_storage and not replays_local_storage
1413-
# Reasoning items are only valid in input when they directly preceded a function_call
1414-
# in the same response. Including a reasoning item that preceded a text response
1415-
# (i.e. no function_call in the same message) causes an API error:
1416-
# "reasoning was provided without its required following item."
1417-
#
1418-
# Local storage is stricter: response-scoped reasoning items (rs_*) cannot be replayed
1419-
# back to the service unless that message is using service-side storage.
1420-
# In that mode we omit reasoning items and rely on function call + tool output replay.
1421-
has_function_call = any(c.type == "function_call" for c in message.contents)
1412+
# Server-issued response item identities (function_call fc_*, reasoning rs_*, approval IDs,
1413+
# local-shell-call IDs) must not be re-sent inline when the request carries
1414+
# previous_response_id / conversation_id / conversation: the server already has them via
1415+
# the prior response and rejects duplicates with "Duplicate item found with id ...".
1416+
# function_result keeps its call_id and the server pairs it to the prior function_call via
1417+
# that key. See microsoft/agent-framework#3295. The strip is gated on the request-level
1418+
# flag, not a message-level one: HistoryProvider-attributed messages
1419+
# (replays_local_storage) still need stripping when the request also carries a continuation
1420+
# marker, since the server-stored items would otherwise duplicate the inline ones. Without
1421+
# storage, standalone reasoning items are invalid per the API ("reasoning was provided
1422+
# without its required following item"), so the reasoning branch always drops.
14221423
for content in message.contents:
14231424
match content.type:
14241425
case "text_reasoning":
1425-
if not uses_service_side_storage or not has_function_call:
1426-
continue # reasoning not followed by a function_call is invalid in input
1427-
reasoning = self._prepare_content_for_openai(
1428-
message.role,
1429-
content,
1430-
replays_local_storage=replays_local_storage,
1431-
)
1432-
if reasoning:
1433-
all_messages.append(reasoning)
1426+
continue
14341427
case "function_result":
1428+
if request_uses_service_side_storage:
1429+
props = content.additional_properties or {}
1430+
# Local-shell variant serializes as `local_shell_call` carrying a server-issued id;
1431+
# plain function_call_output pairs by call_id and is safe under storage.
1432+
if (
1433+
props.get(OPENAI_SHELL_OUTPUT_TYPE_KEY) == OPENAI_SHELL_OUTPUT_TYPE_LOCAL_SHELL_CALL
1434+
and props.get(OPENAI_LOCAL_SHELL_CALL_ITEM_ID_KEY)
1435+
):
1436+
continue
14351437
new_args: dict[str, Any] = {}
14361438
new_args.update(
14371439
self._prepare_content_for_openai(
@@ -1443,6 +1445,8 @@ def _prepare_message_for_openai(
14431445
if new_args:
14441446
all_messages.append(new_args)
14451447
case "function_call":
1448+
if request_uses_service_side_storage:
1449+
continue
14461450
function_call = self._prepare_content_for_openai(
14471451
message.role,
14481452
content,
@@ -1451,6 +1455,8 @@ def _prepare_message_for_openai(
14511455
if function_call:
14521456
all_messages.append(function_call)
14531457
case "function_approval_response" | "function_approval_request":
1458+
if request_uses_service_side_storage:
1459+
continue
14541460
prepared = self._prepare_content_for_openai(
14551461
message.role,
14561462
content,
@@ -1463,6 +1469,12 @@ def _prepare_message_for_openai(
14631469
# top-level mcp_call input item; the result side emits an
14641470
# internal marker that `_prepare_messages_for_openai`
14651471
# coalesces onto the matching call (or drops if unmatched).
1472+
# The mcp_call item carries the model-emitted call_id as its
1473+
# server-side `id`, so under continuation it would duplicate
1474+
# the prior response's items (#3295). Drop the call here; the
1475+
# orphan result is dropped by the coalesce step that follows.
1476+
if request_uses_service_side_storage:
1477+
continue
14661478
prepared_mcp = self._prepare_content_for_openai(
14671479
message.role,
14681480
content,

0 commit comments

Comments
 (0)