Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/agents/memory/openai_conversations_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
else:
async for item in self._openai_client.conversations.items.list(
conversation_id=session_id,
limit=session_limit,
order="desc",
):
Comment on lines 101 to 104

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bound the provider page size for small limits

When callers use a small positive history limit (for example, get_items(limit=1)) on conversations containing large item payloads, omitting limit makes the paginator fetch its full default page before this loop can break. Previously the request fetched no more than the requested number of items; retain a provider-safe capped page size so oversized limits paginate but the common small-window path does not download unnecessary conversation data and add avoidable latency.

Useful? React with 👍 / 👎.

# calling model_dump() to make this serializable
Expand Down
26 changes: 26 additions & 0 deletions tests/memory/test_openai_conversations_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,32 @@ async def test_get_items_zero_limit_returns_empty_without_api_call(self, mock_op
assert session.session_id == "test_conversation_id"
mock_openai_client.conversations.items.list.assert_not_called()

@pytest.mark.asyncio
async def test_get_items_limit_is_applied_after_provider_pagination(self, mock_openai_client):
"""A session limit must not be used as the Conversations API page size."""

async def conversation_items():
for index in range(4, 0, -1):
if index == 1:
raise AssertionError("get_items should stop after collecting the limit")
item = MagicMock()
item.model_dump.return_value = {"id": str(index), "role": "user"}
yield item

mock_openai_client.conversations.items.list = MagicMock(return_value=conversation_items())
session = OpenAIConversationsSession(
conversation_id="test_id", openai_client=mock_openai_client
)

assert await session.get_items(limit=3) == [
{"id": "2", "role": "user"},
{"id": "3", "role": "user"},
{"id": "4", "role": "user"},
]
mock_openai_client.conversations.items.list.assert_called_once_with(
conversation_id="test_id", order="desc"
)

@pytest.mark.asyncio
async def test_add_items_simple(self, mock_openai_client):
"""Test adding items to the conversation."""
Expand Down