Repository navigation
fix(storages): handle serialized role strings in Mem0Storage message preparation - #4396
Wenyan0315 wants to merge 1 commit into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
CI triage for head
The package workflow uses the repository OpenAI secret on This is log-based triage, not a fresh test rerun or a claim that the full suite passes. The existing focused local test result remains recorded in the PR description. |
chrikrah
left a comment
There was a problem hiding this comment.
@Wenyan0315, approving at 8ce1e09. This fixes #4389 on the path I reported it from: ChatHistoryMemory over Mem0Storage now reaches MemoryClient.add.
$ pytest -q test/storages/key_value_storages/test_mem0_cloud.py
# 8ce1e09 (head), mem0ai 2.0.11 as in uv.lock
15 passed, 1 skipped in 2.49s
# head tests, mem0_cloud.py restored from 24fde60 (the PR's base)
FAILED ...TestMem0Storage::test_save_with_serialized_records
E AssertionError: Expected 'add' to have been called once. Called 0 times.
1 failed, 14 passed, 1 skipped in 2.20s
$ python camel_probe.py # ChatHistoryMemory(storage=Mem0Storage), patched MemoryClient, one write_record()
# 8ce1e09
MemoryClient.add calls: 1 [{'role': 'user', 'content': 'remember this'}]
# 24fde60
ERROR - Error adding memory: 'str' object has no attribute 'value'
MemoryClient.add calls: 0
# not run: a live Mem0 account
@fengju0213, could you merge this one when you get to it?
Related Issue
Closes #4389
Description
Mem0Storage._prepare_messagesreads.valueoffrecord["role_at_backend"], butMemoryRecord.to_dict()— the only in-tree producer of these dicts — serializes that field to a plain string. Everysave()therefore raisesAttributeError, which is swallowed by the blanketexcept Exception, soMemoryClient.addis never called and nothing is persisted.The fix makes
_prepare_messagesaccept both forms: it unwraps.valuewhen the field is anOpenAIBackendRoleand passes strings through as-is.The existing
test_savenever caught this because it injects the enum object directly, bypassing theto_dict()\) serialization path that real callers (e.g.ChatHistoryMemory) go through. This PR addstest_save_with_serialized_records, which builds records throughMemoryRecord.to_dict()`; it fails on the unfixed code and passes with the fix.What is the purpose of this pull request?
Checklist
pyproject.tomland runuv lockTest output
The new
test_save_with_serialized_recordswas also verified to fail against the unfixed code (stashed-fix A/B check), confirming it is a true regression test.