3434 CallModelData ,
3535 CallModelInputFilter ,
3636 ModelInputData ,
37+ ReasoningItemIdPolicy ,
3738 RunConfig ,
3839 RunOptions ,
3940 ToolErrorFormatter ,
124125 "ModelInputData" ,
125126 "CallModelData" ,
126127 "CallModelInputFilter" ,
128+ "ReasoningItemIdPolicy" ,
127129 "ToolErrorFormatter" ,
128130 "ToolErrorFormatterArgs" ,
129131 "DEFAULT_MAX_TURNS" ,
@@ -490,6 +492,14 @@ async def run(
490492 )
491493 original_input_for_state = prepared_input
492494
495+ resolved_reasoning_item_id_policy : ReasoningItemIdPolicy | None = (
496+ run_config .reasoning_item_id_policy
497+ if run_config .reasoning_item_id_policy is not None
498+ else (run_state ._reasoning_item_id_policy if run_state is not None else None )
499+ )
500+ if run_state is not None :
501+ run_state ._reasoning_item_id_policy = resolved_reasoning_item_id_policy
502+
493503 # Check whether to enable OpenAI server-managed conversation
494504 if (
495505 conversation_id is not None
@@ -500,6 +510,7 @@ async def run(
500510 conversation_id = conversation_id ,
501511 previous_response_id = previous_response_id ,
502512 auto_previous_response_id = auto_previous_response_id ,
513+ reasoning_item_id_policy = resolved_reasoning_item_id_policy ,
503514 )
504515 else :
505516 server_conversation_tracker = None
@@ -566,8 +577,15 @@ async def run(
566577 previous_response_id = previous_response_id ,
567578 auto_previous_response_id = auto_previous_response_id ,
568579 )
580+ run_state ._reasoning_item_id_policy = resolved_reasoning_item_id_policy
569581 run_state .set_trace (get_current_trace ())
570582
583+ def _with_reasoning_item_id_policy (result : RunResult ) -> RunResult :
584+ result ._reasoning_item_id_policy = resolved_reasoning_item_id_policy
585+ if run_state is not None :
586+ run_state ._reasoning_item_id_policy = resolved_reasoning_item_id_policy
587+ return result
588+
571589 pending_server_items : list [RunItem ] | None = None
572590 input_guardrail_results : list [InputGuardrailResult ] = (
573591 list (run_state ._input_guardrail_results ) if run_state is not None else []
@@ -675,6 +693,9 @@ async def run(
675693 run_state ._current_turn_persisted_item_count
676694 ),
677695 response_id = turn_result .model_response .response_id ,
696+ reasoning_item_id_policy = (
697+ run_state ._reasoning_item_id_policy
698+ ),
678699 store = store_setting ,
679700 )
680701 )
@@ -727,7 +748,7 @@ async def run(
727748 original_input = original_input ,
728749 )
729750 return finalize_conversation_tracking (
730- result ,
751+ _with_reasoning_item_id_policy ( result ) ,
731752 server_conversation_tracker = server_conversation_tracker ,
732753 run_state = run_state ,
733754 )
@@ -792,7 +813,7 @@ async def run(
792813 )
793814 result ._original_input = copy_input_items (original_input )
794815 return finalize_conversation_tracking (
795- result ,
816+ _with_reasoning_item_id_policy ( result ) ,
796817 server_conversation_tracker = server_conversation_tracker ,
797818 run_state = run_state ,
798819 )
@@ -852,6 +873,7 @@ async def run(
852873 new_items = session_items ,
853874 raw_responses = model_responses ,
854875 last_agent = current_agent ,
876+ reasoning_item_id_policy = resolved_reasoning_item_id_policy ,
855877 )
856878 handler_result = await resolve_run_error_handler_result (
857879 error_handlers = error_handlers ,
@@ -921,7 +943,7 @@ async def run(
921943 )
922944 result ._original_input = copy_input_items (original_input )
923945 return finalize_conversation_tracking (
924- result ,
946+ _with_reasoning_item_id_policy ( result ) ,
925947 server_conversation_tracker = server_conversation_tracker ,
926948 run_state = run_state ,
927949 )
@@ -995,6 +1017,7 @@ async def run(
9951017 if not is_resumed_state and session_persistence_enabled
9961018 else None
9971019 ),
1020+ reasoning_item_id_policy = resolved_reasoning_item_id_policy ,
9981021 )
9991022 )
10001023
@@ -1048,6 +1071,7 @@ async def run(
10481071 if not is_resumed_state and session_persistence_enabled
10491072 else None
10501073 ),
1074+ reasoning_item_id_policy = resolved_reasoning_item_id_policy ,
10511075 )
10521076
10531077 # Start hooks should only run on the first turn unless reset by a handoff.
@@ -1116,6 +1140,9 @@ async def run(
11161140 items_to_save_turn ,
11171141 None ,
11181142 response_id = turn_result .model_response .response_id ,
1143+ reasoning_item_id_policy = (
1144+ run_state ._reasoning_item_id_policy
1145+ ),
11191146 store = store_setting ,
11201147 )
11211148 run_state ._current_turn_persisted_item_count += saved_count
@@ -1181,7 +1208,7 @@ async def run(
11811208 )
11821209 result ._original_input = copy_input_items (original_input )
11831210 return finalize_conversation_tracking (
1184- result ,
1211+ _with_reasoning_item_id_policy ( result ) ,
11851212 server_conversation_tracker = server_conversation_tracker ,
11861213 run_state = run_state ,
11871214 )
@@ -1242,7 +1269,7 @@ async def run(
12421269 original_input = original_input ,
12431270 )
12441271 return finalize_conversation_tracking (
1245- result ,
1272+ _with_reasoning_item_id_policy ( result ) ,
12461273 server_conversation_tracker = server_conversation_tracker ,
12471274 run_state = run_state ,
12481275 )
@@ -1473,6 +1500,14 @@ def run_streamed(
14731500 auto_previous_response_id = auto_previous_response_id ,
14741501 )
14751502
1503+ resolved_reasoning_item_id_policy : ReasoningItemIdPolicy | None = (
1504+ run_config .reasoning_item_id_policy
1505+ if run_config .reasoning_item_id_policy is not None
1506+ else (run_state ._reasoning_item_id_policy if run_state is not None else None )
1507+ )
1508+ if run_state is not None :
1509+ run_state ._reasoning_item_id_policy = resolved_reasoning_item_id_policy
1510+
14761511 (
14771512 trace_workflow_name ,
14781513 trace_id ,
@@ -1551,6 +1586,7 @@ def run_streamed(
15511586 streamed_result ._model_input_items = (
15521587 list (run_state ._generated_items ) if run_state is not None else []
15531588 )
1589+ streamed_result ._reasoning_item_id_policy = resolved_reasoning_item_id_policy
15541590 if run_state is not None :
15551591 streamed_result ._trace_state = run_state ._trace_state
15561592 # Store run_state in streamed_result._state so it's accessible throughout streaming
0 commit comments