Skip to content

fix(memory): restore Memory and AsyncMemory context-manager protocol - #7354

Merged
blazingretinol merged 1 commit into
mem0ai:mainfrom
Tyagiquamar:fix/memory-context-manager-7272
Sep 24, 2026
Merged

blazingretinol merged 1 commit into
mem0ai:mainfrom
Tyagiquamar:fix/memory-context-manager-7272

Conversation

@Tyagiquamar

Copy link
Copy Markdown

Linked Issue

Closes #7272

Description

The V3 rewrite dropped the context-manager protocol methods that PR #4535 had added: \with Memory(...) as m\ raises \TypeError: 'Memory' object does not support the context manager protocol\ (and the async equivalent for \AsyncMemory), even though \close()\ still exists and the published skill documents the \with\ form. This restores the four methods (_enter_/_exit_\ on \Memory, _aenter_/_aexit_\ on \AsyncMemory) around the existing \close()\ methods. _exit_/_aexit_\ call \close()\ and return \False\ so body exceptions propagate.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactor (no functional changes)
  • Documentation update

AI Assistance

  • No AI assistance
  • AI-assisted (autocomplete, or I asked a model questions while writing this)
  • AI-generated (an agent wrote most or all of this diff)

Tool: Muse Spark (OpenCode) for repo triage and scaffolding; I read \mem0/memory/main.py\ (both \close()\ methods), the prior art in #4535, and \ ests/test_telemetry.py\ myself, ran the failing-first tests and the fix in Docker, and reviewed the final diff.

  • I can explain every line of this diff and how it interacts with the rest of the codebase, without asking an AI tool.

Breaking Changes

N/A

Test Coverage

  • I added/updated unit tests
  • I added/updated integration tests
  • I tested manually (describe below)
  • No tests needed (explain why)

Added 4 regression tests to the existing lifecycle classes in \ ests/test_telemetry.py\ (whose docstrings already claimed context-manager support): sync/async \with\ yields self and closes on exit, and closes before propagating a body exception. Written first: all 4 fail on main with \TypeError, all pass with the fix.

Docker (\python:3.11-bookworm, venv, \pip install -e '.[test]'):

  • \pytest tests/test_telemetry.py: 47 passed
  • \pytest tests/memory/test_main.py: 52 passed

  • uff check\ (repo config) on both touched files: clean.
    uff format --check\ flags pre-existing drift in \mem0/memory/main.py\ outside this diff (not CI-gated for the Python SDK; left untouched to keep the diff minimal).

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation if needed (no public API change; restores documented behavior)

@github-actions github-actions Bot added the sdk-python Python SDK specific label Sep 17, 2026
@github-actions

Copy link
Copy Markdown

Thanks for taking the time to open this.

We only review pull requests that fix an issue we have already agreed to take on, so this one is closed for now.
Closed does not mean rejected. It means it is not in the queue yet, and reopening takes about a minute.

To get it reviewed:

  1. Make sure an issue describes the problem, with the version you are on, a runnable reproduction, and the real output or traceback you saw.
  2. Link it from this pull request description with Closes #<number>.
  3. Ask a maintainer to label that issue accepted. This pull request reopens by itself when they do.

Issue already labeled accepted? Just add Closes #<number> to the description. That reopens this too.

Documentation-only changes skip this gate entirely.

See CONTRIBUTING.md for the full policy.

@github-actions github-actions Bot closed this Sep 17, 2026
@github-actions github-actions Bot added the rest-api REST API server label Sep 17, 2026
@github-actions

Copy link
Copy Markdown

Hi @Tyagiquamar, thanks for opening this pull request.

This is just a soft check: you are not yet in this repo's vouched contributor list (.github/VOUCHED.td). Nothing is blocked and there is nothing you need to do.

A maintainer can vouch for you by commenting !vouch @Tyagiquamar on any issue.

@github-actions github-actions Bot reopened this Sep 24, 2026
@github-actions

Copy link
Copy Markdown

An accepted issue is linked now, so this is open again and ready for review.

@CLAassistant

CLAassistant commented Sep 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@blazingretinol

Copy link
Copy Markdown
Contributor

hey @Tyagiquamar can you please sign the cla here so that i can review this pr here?

@Tyagiquamar

Copy link
Copy Markdown
Author

@kartik-mem0 signed

@blazingretinol
blazingretinol merged commit d675cf6 into mem0ai:main Sep 24, 2026
28 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rest-api REST API server sdk-python Python SDK specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(memory): V3 rewrite dropped Memory and AsyncMemory context-manager methods

3 participants