Skip to content

fix(sessions): skip corrupt docs in MongoDBSession.pop_item - #3247

Merged
seratch merged 1 commit into
openai:mainfrom
adityasingh2400:fix/mongo-pop-corrupt-skip
May 9, 2026
Merged

fix(sessions): skip corrupt docs in MongoDBSession.pop_item#3247
seratch merged 1 commit into
openai:mainfrom
adityasingh2400:fix/mongo-pop-corrupt-skip

Conversation

@adityasingh2400

Copy link
Copy Markdown
Contributor

Summary

MongoDBSession.pop_item returns None from two distinct conditions:

  1. The session is empty.
  2. The most-recent document's message_data fails to deserialize (invalid JSON, missing field, or non-string type — the same (JSONDecodeError, KeyError, TypeError) triple that get_items already swallows).

In the second case the corrupt document is removed by find_one_and_delete and the caller sees None, even though valid older messages still exist. A loop that pops items until empty therefore stops one item early and leaves history behind.

This mirrors the behavior get_items already provides — it silently skips corrupt rows and keeps reading valid ones — so pop_item should do the same to stay consistent.

Fix

Wrap the pop in a while True loop that discards corrupt documents and re-pops the next-most-recent until a valid item is decoded or the session is genuinely empty. The fix is local to pop_item and adds five lines of behavior plus a docstring note.

Test plan

  • tests/extensions/memory/test_mongodb_session.py::test_pop_item_skips_corrupt_most_recent — corrupt latest + valid older returns the older item and leaves the collection empty.
  • tests/extensions/memory/test_mongodb_session.py::test_pop_item_returns_none_when_only_corrupt_docs_remain — all-corrupt session drains every bad row before returning None.
  • All 31 existing MongoDBSession tests still pass (33 total now).
  • ruff check / ruff format --check clean.

When pop_item retrieves a document whose message_data fails to deserialize
(invalid JSON, missing/non-string field), it currently returns None — the
same value used to signal an empty session. A non-empty session with a
corrupt latest doc therefore looks empty to callers, causing them to stop
draining items even though valid messages remain.

Wrap the pop in a loop that discards corrupt rows and tries the next-most
recent until a valid item is decoded or the session is genuinely empty.
This brings pop_item in line with get_items, which already skips corrupt
documents.

Adds two regression tests covering the mixed and all-corrupt cases.
@github-actions github-actions Bot added bug Something isn't working feature:extensions labels May 8, 2026
@seratch

seratch commented May 8, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@seratch seratch added this to the 0.17.x milestone May 9, 2026
@seratch
seratch merged commit 7aeb39e into openai:main May 9, 2026
10 checks passed
RudrenduPaul pushed a commit to RudrenduPaul/openai-agents-python that referenced this pull request Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:extensions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants