Repository navigation
fix(memory): restore Memory and AsyncMemory context-manager protocol - #7354
Conversation
|
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. To get it reviewed:
Issue already labeled Documentation-only changes skip this gate entirely. See CONTRIBUTING.md for the full policy. |
|
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 ( A maintainer can vouch for you by commenting |
|
An |
|
hey @Tyagiquamar can you please sign the cla here so that i can review this pr here? |
|
@kartik-mem0 signed |
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
AI Assistance
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.
Breaking Changes
N/A
Test Coverage
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]'):
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