@@ -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
18011805def 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
18651862def 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