Skip to content

Commit 9ca4d14

Browse files
committed
Python: Strip server-issued response item IDs under storage (microsoft#3295)
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 6853f64 commit 9ca4d14

4 files changed

Lines changed: 180 additions & 31 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,13 @@ def _prepare_message_for_openai(
13801380
additional_properties = message.additional_properties
13811381
replays_local_storage = "_attribution" in additional_properties
13821382
uses_service_side_storage = request_uses_service_side_storage and not replays_local_storage
1383+
# Server-issued response item identities (function_call fc_*, reasoning rs_*, approval IDs,
1384+
# local-shell-call IDs) must not be re-sent inline when the request carries
1385+
# previous_response_id / conversation_id / conversation. The server already has them via
1386+
# the prior response and rejects duplicates with "Duplicate item found with id ...".
1387+
# The function_result keeps its call_id and the server pairs it to the prior function_call
1388+
# via that key. See microsoft/agent-framework#3295.
1389+
strip_server_items = uses_service_side_storage
13831390
# Reasoning items are only valid in input when they directly preceded a function_call
13841391
# in the same response. Including a reasoning item that preceded a text response
13851392
# (i.e. no function_call in the same message) causes an API error:
@@ -1392,6 +1399,8 @@ def _prepare_message_for_openai(
13921399
for content in message.contents:
13931400
match content.type:
13941401
case "text_reasoning":
1402+
if strip_server_items:
1403+
continue
13951404
if not uses_service_side_storage or not has_function_call:
13961405
continue # reasoning not followed by a function_call is invalid in input
13971406
reasoning = self._prepare_content_for_openai(
@@ -1402,6 +1411,12 @@ def _prepare_message_for_openai(
14021411
if reasoning:
14031412
all_messages.append(reasoning)
14041413
case "function_result":
1414+
if strip_server_items and content.additional_properties:
1415+
shell_output_type = content.additional_properties.get(OPENAI_SHELL_OUTPUT_TYPE_KEY)
1416+
if shell_output_type == OPENAI_SHELL_OUTPUT_TYPE_LOCAL_SHELL_CALL and (
1417+
content.additional_properties.get(OPENAI_LOCAL_SHELL_CALL_ITEM_ID_KEY)
1418+
):
1419+
continue
14051420
new_args: dict[str, Any] = {}
14061421
new_args.update(
14071422
self._prepare_content_for_openai(
@@ -1413,6 +1428,8 @@ def _prepare_message_for_openai(
14131428
if new_args:
14141429
all_messages.append(new_args)
14151430
case "function_call":
1431+
if strip_server_items:
1432+
continue
14161433
function_call = self._prepare_content_for_openai(
14171434
message.role,
14181435
content,
@@ -1421,6 +1438,8 @@ def _prepare_message_for_openai(
14211438
if function_call:
14221439
all_messages.append(function_call)
14231440
case "function_approval_response" | "function_approval_request":
1441+
if strip_server_items:
1442+
continue
14241443
prepared = self._prepare_content_for_openai(
14251444
message.role,
14261445
content,

‎python/packages/openai/tests/openai/test_openai_chat_client.py‎

Lines changed: 154 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ async def test_chat_message_parsing_with_function_calls() -> None:
438438
Message(role="tool", contents=[function_result]),
439439
]
440440

441-
prepared_messages = client._prepare_messages_for_openai(messages)
441+
prepared_messages = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=False)
442442

443443
assert prepared_messages == [
444444
{
@@ -1754,7 +1754,7 @@ def test_prepare_message_for_openai_with_function_approval_response() -> None:
17541754

17551755
message = Message(role="user", contents=[approval_response])
17561756

1757-
result = client._prepare_message_for_openai(message)
1757+
result = client._prepare_message_for_openai(message, request_uses_service_side_storage=False)
17581758

17591759
# FunctionApprovalResponseContent is added directly, not nested in args with role
17601760
assert len(result) == 1
@@ -1786,16 +1786,20 @@ def test_prepare_message_for_openai_includes_reasoning_with_function_call() -> N
17861786

17871787
message = Message(role="assistant", contents=[reasoning, function_call])
17881788

1789-
result = client._prepare_message_for_openai(message)
1790-
1791-
# Both reasoning and function_call should be present as top-level items
1792-
types = [item["type"] for item in result]
1793-
assert "reasoning" in types, "Reasoning items must be included for reasoning models"
1794-
assert "function_call" in types
1789+
# Storage-on path strips both server-issued reasoning (rs_*) and function_call items
1790+
# because the server already has them via previous_response_id (#3295).
1791+
storage_on_result = client._prepare_message_for_openai(message, request_uses_service_side_storage=True)
1792+
storage_on_types = [item["type"] for item in storage_on_result]
1793+
assert "reasoning" not in storage_on_types
1794+
assert "function_call" not in storage_on_types
17951795

1796-
reasoning_item = next(item for item in result if item["type"] == "reasoning")
1797-
assert reasoning_item["summary"][0]["text"] == "Let me analyze the request"
1798-
assert reasoning_item["id"] == "rs_abc123", "Reasoning id must be preserved for the API"
1796+
# Storage-off path keeps function_call inline so the server sees the call. Reasoning items
1797+
# cannot be replayed inline against a server that has no record of the prior response, so
1798+
# they remain dropped on this path as well.
1799+
storage_off_result = client._prepare_message_for_openai(message, request_uses_service_side_storage=False)
1800+
storage_off_types = [item["type"] for item in storage_off_result]
1801+
assert "function_call" in storage_off_types
1802+
assert "reasoning" not in storage_off_types
17991803

18001804

18011805
def test_prepare_messages_for_openai_full_conversation_with_reasoning() -> None:
@@ -1840,27 +1844,20 @@ def test_prepare_messages_for_openai_full_conversation_with_reasoning() -> None:
18401844
),
18411845
]
18421846

1843-
result = client._prepare_messages_for_openai(messages)
1847+
# Storage-off path: function_call kept inline (server has no record of it),
1848+
# function_call_output kept. Reasoning is still dropped because rs_* response-scoped IDs
1849+
# cannot be replayed against a server that has no record of the originating response.
1850+
result = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=False)
18441851

18451852
types = [item.get("type") for item in result]
18461853
assert "message" in types, "User/assistant messages should be present"
1847-
assert "reasoning" in types, "Reasoning items must be present"
1848-
assert "function_call" in types, "Function call items must be present"
1854+
assert "function_call" in types, "Function call items must be present without storage"
18491855
assert "function_call_output" in types, "Function call output must be present"
18501856

1851-
# Verify reasoning has id
1852-
reasoning_items = [item for item in result if item.get("type") == "reasoning"]
1853-
assert reasoning_items[0]["id"] == "rs_test123"
1854-
18551857
# Verify function_call has id
18561858
fc_items = [item for item in result if item.get("type") == "function_call"]
18571859
assert fc_items[0]["id"] == "fc_test456"
18581860

1859-
# Verify correct ordering: reasoning before function_call
1860-
reasoning_idx = types.index("reasoning")
1861-
fc_idx = types.index("function_call")
1862-
assert reasoning_idx < fc_idx, "Reasoning must come before function_call"
1863-
18641861

18651862
def test_prepare_message_for_openai_filters_error_content() -> None:
18661863
"""Test that error content in messages is handled properly."""
@@ -4983,7 +4980,10 @@ async def test_prepare_messages_for_openai_does_not_replay_fc_id_when_loaded_fro
49834980

49844981
next_turn_input = Message(role="user", contents=[Content.from_text(text="Book the cheapest one")])
49854982

4986-
live_result = client._prepare_messages_for_openai([*session.state[provider.source_id]["messages"], next_turn_input])
4983+
live_result = client._prepare_messages_for_openai(
4984+
[*session.state[provider.source_id]["messages"], next_turn_input],
4985+
request_uses_service_side_storage=False,
4986+
)
49874987
live_function_call = next(item for item in live_result if item.get("type") == "function_call")
49884988
assert live_function_call["id"] == "fc_provider123"
49894989

@@ -4996,7 +4996,8 @@ async def test_prepare_messages_for_openai_does_not_replay_fc_id_when_loaded_fro
49964996
) # type: ignore[arg-type]
49974997

49984998
loaded_result = client._prepare_messages_for_openai(
4999-
context.get_messages(sources={provider.source_id}, include_input=True)
4999+
context.get_messages(sources={provider.source_id}, include_input=True),
5000+
request_uses_service_side_storage=False,
50005001
)
50015002
loaded_function_call = next(item for item in loaded_result if item.get("type") == "function_call")
50025003
assert loaded_function_call["id"] == "fc_call_1"
@@ -5015,7 +5016,8 @@ async def test_prepare_messages_for_openai_does_not_replay_fc_id_when_loaded_fro
50155016
) # type: ignore[arg-type]
50165017

50175018
restored_result = client._prepare_messages_for_openai(
5018-
restored_context.get_messages(sources={provider.source_id}, include_input=True)
5019+
restored_context.get_messages(sources={provider.source_id}, include_input=True),
5020+
request_uses_service_side_storage=False,
50195021
)
50205022
restored_function_call = next(item for item in restored_result if item.get("type") == "function_call")
50215023
assert restored_function_call["id"] == "fc_call_1"
@@ -5049,7 +5051,9 @@ def test_prepare_messages_for_openai_keeps_live_fc_id_separate_from_replayed_his
50495051
],
50505052
)
50515053

5052-
result = client._prepare_messages_for_openai([history_message, live_message])
5054+
result = client._prepare_messages_for_openai(
5055+
[history_message, live_message], request_uses_service_side_storage=False
5056+
)
50535057

50545058
function_calls = [item for item in result if item.get("type") == "function_call"]
50555059
assert [item["id"] for item in function_calls] == ["fc_call_1", "fc_live123"]
@@ -5087,7 +5091,7 @@ def test_prepare_messages_for_openai_filters_empty_fc_id() -> None:
50875091
),
50885092
]
50895093

5090-
result = client._prepare_messages_for_openai(messages)
5094+
result = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=False)
50915095

50925096
# Find the function_call items in the result
50935097
fc_items = [item for item in result if item.get("type") == "function_call"]
@@ -5122,7 +5126,7 @@ def test_prepare_messages_for_openai_filters_none_fc_id() -> None:
51225126
),
51235127
]
51245128

5125-
result = client._prepare_messages_for_openai(messages)
5129+
result = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=False)
51265130

51275131
# Find the function_call item
51285132
fc_items = [item for item in result if item.get("type") == "function_call"]
@@ -5133,4 +5137,125 @@ def test_prepare_messages_for_openai_filters_none_fc_id() -> None:
51335137
assert fc_item["id"].startswith("fc_")
51345138

51355139

5140+
def _strip_rule_messages() -> list[Message]:
5141+
return [
5142+
Message(role="user", contents=[Content.from_text(text="search hotels in Paris")]),
5143+
Message(
5144+
role="assistant",
5145+
contents=[
5146+
Content.from_function_call(
5147+
call_id="call_1",
5148+
name="search_hotels",
5149+
arguments='{"city": "Paris"}',
5150+
additional_properties={"fc_id": "fc_server_issued"},
5151+
),
5152+
],
5153+
),
5154+
Message(
5155+
role="tool",
5156+
contents=[Content.from_function_result(call_id="call_1", result="Found 3 hotels in Paris")],
5157+
),
5158+
]
5159+
5160+
5161+
def test_prepare_messages_strips_function_call_under_storage() -> None:
5162+
"""Regression for #3295: when previous_response_id / conversation_id is in flight, the chat
5163+
client must not re-send server-issued function_call items inline. The server already has them
5164+
via the prior response and rejects duplicates with 'Duplicate item found with id fc_...'.
5165+
The function_result keeps its call_id so the server can pair result-to-call."""
5166+
client = OpenAIChatClient(model="test-model", api_key="test-key")
5167+
5168+
result = client._prepare_messages_for_openai(_strip_rule_messages(), request_uses_service_side_storage=True)
5169+
5170+
types = [item.get("type") for item in result]
5171+
assert "function_call" not in types
5172+
assert "function_call_output" in types
5173+
output_item = next(item for item in result if item.get("type") == "function_call_output")
5174+
assert output_item["call_id"] == "call_1"
5175+
5176+
5177+
def test_prepare_messages_keeps_function_call_without_storage() -> None:
5178+
"""Without storage there is no previous_response_id, so inline function_call items are the
5179+
only source of truth for the server. Behavior is byte-identical to pre-#3295."""
5180+
client = OpenAIChatClient(model="test-model", api_key="test-key")
5181+
5182+
result = client._prepare_messages_for_openai(_strip_rule_messages(), request_uses_service_side_storage=False)
5183+
5184+
types = [item.get("type") for item in result]
5185+
assert "function_call" in types
5186+
assert "function_call_output" in types
5187+
fc_item = next(item for item in result if item.get("type") == "function_call")
5188+
assert fc_item["call_id"] == "call_1"
5189+
assert fc_item["id"] == "fc_server_issued"
5190+
output_item = next(item for item in result if item.get("type") == "function_call_output")
5191+
assert output_item["call_id"] == "call_1"
5192+
5193+
5194+
def test_prepare_messages_strips_approval_items_under_storage() -> None:
5195+
"""Approval request/response items also carry server-issued IDs and must be stripped under
5196+
storage. Without storage they are kept (#3295)."""
5197+
client = OpenAIChatClient(model="test-model", api_key="test-key")
5198+
5199+
function_call = Content.from_function_call(
5200+
call_id="mcp_1",
5201+
name="sensitive_action",
5202+
arguments='{"action": "delete"}',
5203+
)
5204+
approval_request = Content.from_function_approval_request(
5205+
id="approval_req_1",
5206+
function_call=function_call,
5207+
)
5208+
approval_response = Content.from_function_approval_response(
5209+
approved=True,
5210+
id="approval_req_1",
5211+
function_call=function_call,
5212+
)
5213+
messages = [
5214+
Message(role="assistant", contents=[approval_request]),
5215+
Message(role="user", contents=[approval_response]),
5216+
]
5217+
5218+
storage_on = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=True)
5219+
storage_on_types = [item.get("type") for item in storage_on]
5220+
assert "mcp_approval_request" not in storage_on_types
5221+
assert "mcp_approval_response" not in storage_on_types
5222+
5223+
storage_off = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=False)
5224+
storage_off_types = [item.get("type") for item in storage_off]
5225+
assert "mcp_approval_request" in storage_off_types
5226+
assert "mcp_approval_response" in storage_off_types
5227+
5228+
5229+
def test_prepare_messages_strips_local_shell_call_under_storage() -> None:
5230+
"""Local-shell-call function_results carry a server-issued local_shell_call_item_id and must
5231+
be stripped under storage. Plain function_results (no shell ID) are kept either way (#3295)."""
5232+
from agent_framework_openai._chat_client import (
5233+
OPENAI_LOCAL_SHELL_CALL_ITEM_ID_KEY,
5234+
OPENAI_SHELL_OUTPUT_TYPE_KEY,
5235+
OPENAI_SHELL_OUTPUT_TYPE_LOCAL_SHELL_CALL,
5236+
)
5237+
5238+
client = OpenAIChatClient(model="test-model", api_key="test-key")
5239+
shell_result = Content.from_function_result(
5240+
call_id="shell_1",
5241+
result="ok",
5242+
additional_properties={
5243+
OPENAI_SHELL_OUTPUT_TYPE_KEY: OPENAI_SHELL_OUTPUT_TYPE_LOCAL_SHELL_CALL,
5244+
OPENAI_LOCAL_SHELL_CALL_ITEM_ID_KEY: "lsh_server_issued",
5245+
},
5246+
)
5247+
plain_result = Content.from_function_result(call_id="plain_1", result="plain")
5248+
message = Message(role="tool", contents=[shell_result, plain_result])
5249+
5250+
storage_on = client._prepare_message_for_openai(message, request_uses_service_side_storage=True)
5251+
types_on = [item.get("type") for item in storage_on]
5252+
assert OPENAI_SHELL_OUTPUT_TYPE_LOCAL_SHELL_CALL not in types_on
5253+
assert "function_call_output" in types_on
5254+
5255+
storage_off = client._prepare_message_for_openai(message, request_uses_service_side_storage=False)
5256+
types_off = [item.get("type") for item in storage_off]
5257+
assert OPENAI_SHELL_OUTPUT_TYPE_LOCAL_SHELL_CALL in types_off
5258+
assert "function_call_output" in types_off
5259+
5260+
51365261
# endregion

0 commit comments

Comments
 (0)