@@ -232,6 +232,41 @@ async def test_get_items_zero_limit_returns_empty_without_api_call(self, mock_op
232232 assert session .session_id == "test_conversation_id"
233233 mock_openai_client .conversations .items .list .assert_not_called ()
234234
235+ @pytest .mark .asyncio
236+ async def test_get_items_applies_large_limit_after_provider_pagination (
237+ self , mock_openai_client
238+ ):
239+ """A session limit must not be forwarded as the Conversations API page size."""
240+
241+ class ConversationItem :
242+ def __init__ (self , item_id : int ) -> None :
243+ self .item_id = item_id
244+
245+ def model_dump (self , * , exclude_unset : bool ) -> dict [str , int ]:
246+ assert exclude_unset is True
247+ return {"item_id" : self .item_id }
248+
249+ yielded_item_ids : list [int ] = []
250+
251+ async def descending_items ():
252+ # Yield one extra newest-first item so the local cutoff can prove it stops at N.
253+ for item_id in range (101 , - 1 , - 1 ):
254+ yielded_item_ids .append (item_id )
255+ yield ConversationItem (item_id )
256+
257+ mock_openai_client .conversations .items .list = MagicMock (return_value = descending_items ())
258+ session = OpenAIConversationsSession (
259+ conversation_id = "test_id" , openai_client = mock_openai_client
260+ )
261+
262+ items = await session .get_items (limit = 101 )
263+
264+ assert [cast (dict [str , int ], item )["item_id" ] for item in items ] == list (range (1 , 102 ))
265+ assert yielded_item_ids == list (range (101 , 0 , - 1 ))
266+ mock_openai_client .conversations .items .list .assert_called_once_with (
267+ conversation_id = "test_id" , order = "desc"
268+ )
269+
235270 @pytest .mark .asyncio
236271 async def test_add_items_simple (self , mock_openai_client ):
237272 """Test adding items to the conversation."""
0 commit comments