1+ from typing import Any , cast
2+
13from openai .types .responses import ResponseOutputMessage , ResponseOutputText
24from openai .types .responses .response_reasoning_item import ResponseReasoningItem
35
@@ -96,6 +98,14 @@ def _get_reasoning_output_run_item() -> ReasoningItem:
9698 )
9799
98100
101+ def _as_message (item : TResponseInputItem ) -> dict [str , Any ]:
102+ assert isinstance (item , dict )
103+ role = item .get ("role" )
104+ assert isinstance (role , str )
105+ assert role in {"assistant" , "user" , "system" , "developer" }
106+ return cast (dict [str , Any ], item )
107+
108+
99109def test_empty_data ():
100110 handoff_input_data = HandoffInputData (
101111 input_history = (),
@@ -242,13 +252,16 @@ def test_nest_handoff_history_wraps_transcript() -> None:
242252 nested = nest_handoff_history (data )
243253
244254 assert isinstance (nested .input_history , tuple )
245- assert nested .input_history [0 ]["role" ] == "developer"
246- developer_content = nested .input_history [0 ]["content" ]
255+ developer = _as_message (nested .input_history [0 ])
256+ assert developer ["role" ] == "developer"
257+ developer_content = developer ["content" ]
258+ assert isinstance (developer_content , str )
247259 assert "<CONVERSATION HISTORY>" in developer_content
248260 assert "</CONVERSATION HISTORY>" in developer_content
249261 assert "Assist reply" in developer_content
250- assert nested .input_history [1 ]["role" ] == "user"
251- assert nested .input_history [1 ]["content" ] == "Hello"
262+ latest_user = _as_message (nested .input_history [1 ])
263+ assert latest_user ["role" ] == "user"
264+ assert latest_user ["content" ] == "Hello"
252265 assert len (nested .pre_handoff_items ) == 0
253266 assert nested .new_items == data .new_items
254267
@@ -265,8 +278,11 @@ def test_nest_handoff_history_handles_missing_user() -> None:
265278
266279 assert isinstance (nested .input_history , tuple )
267280 assert len (nested .input_history ) == 1
268- assert nested .input_history [0 ]["role" ] == "developer"
269- assert "reasoning" in nested .input_history [0 ]["content" ].lower ()
281+ developer = _as_message (nested .input_history [0 ])
282+ assert developer ["role" ] == "developer"
283+ developer_content = developer ["content" ]
284+ assert isinstance (developer_content , str )
285+ assert "reasoning" in developer_content .lower ()
270286
271287
272288def test_nest_handoff_history_appends_existing_history () -> None :
@@ -278,9 +294,10 @@ def test_nest_handoff_history_appends_existing_history() -> None:
278294 )
279295
280296 first_nested = nest_handoff_history (first )
297+ assert isinstance (first_nested .input_history , tuple )
281298 developer_message = first_nested .input_history [0 ]
282299
283- follow_up_history = (
300+ follow_up_history : tuple [ TResponseInputItem , ...] = (
284301 developer_message ,
285302 _get_user_input_item ("Another question" ),
286303 )
@@ -295,9 +312,10 @@ def test_nest_handoff_history_appends_existing_history() -> None:
295312 second_nested = nest_handoff_history (second )
296313
297314 assert isinstance (second_nested .input_history , tuple )
298- developer = second_nested .input_history [0 ]
315+ developer = _as_message ( second_nested .input_history [0 ])
299316 assert developer ["role" ] == "developer"
300317 content = developer ["content" ]
318+ assert isinstance (content , str )
301319 assert content .count ("<CONVERSATION HISTORY>" ) == 1
302320 assert content .count ("</CONVERSATION HISTORY>" ) == 1
303321 assert "First reply" in content
0 commit comments