Skip to content

fix(storages): handle serialized role strings in Mem0Storage message preparation - #4396

Open
Wenyan0315 wants to merge 1 commit into
camel-ai:masterfrom
Wenyan0315:fix/mem0-storage-role-type
Open

Wenyan0315 wants to merge 1 commit into
camel-ai:masterfrom
Wenyan0315:fix/mem0-storage-role-type

Conversation

@Wenyan0315

Copy link
Copy Markdown

Related Issue

Closes #4389

Description

Mem0Storage._prepare_messages reads .value off record["role_at_backend"], but MemoryRecord.to_dict() — the only in-tree producer of these dicts — serializes that field to a plain string. Every save() therefore raises AttributeError, which is swallowed by the blanket except Exception, so MemoryClient.add is never called and nothing is persisted.

The fix makes _prepare_messages accept both forms: it unwraps .value when the field is an OpenAIBackendRole and passes strings through as-is.

The existing test_save never caught this because it injects the enum object directly, bypassing the to_dict()\) serialization path that real callers (e.g. ChatHistoryMemory) go through. This PR adds test_save_with_serialized_records, which builds records through MemoryRecord.to_dict()`; it fails on the unfixed code and passes with the fix.

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Checklist

  • I have read and agree to the AI-Generated Code Policy (required)
  • I have linked this PR to an issue (required)
  • I have checked if any dependencies need to be added or updated in pyproject.toml and run uv lock
  • I have updated the tests accordingly (required for a bug fix or a new feature)
  • I have updated the documentation if needed
  • I have added examples if this is a new feature

Test output

$ python -m pytest test/storages/key_value_storages/test_mem0_cloud.py -v

test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_clear PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_error_handling_clear PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_error_handling_load PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_error_handling_save PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_initialization PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_initialization_required_agent_id PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_integration SKIPPED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_load PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_load_with_both_agent_and_user_id PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_load_with_missing_metadata_key PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_load_with_no_filters PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_load_with_none_metadata PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_load_with_only_agent_id PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_prepare_messages PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_save PASSED
test/storages/key_value_storages/test_mem0_cloud.py::TestMem0Storage::test_save_with_serialized_records PASSED

======================== 15 passed, 1 skipped in 0.47s =========================

The new test_save_with_serialized_records was also verified to fail against the unfixed code (stashed-fix A/B check), confirming it is a true regression test.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7884d3b5-7cf5-4bdf-8bca-f04dbb676e42

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Wenyan0315

Copy link
Copy Markdown
Author

CI triage for head 8ce1e091b3f6ed3f6cb5a8cb6b3b4d3439b627bc (AI-assisted; checked against the failed-job logs and workflow definitions):

  • Package tests: all three jobs (fast, llm, very_slow) stop during collection with the same five credential errors: test_chat_agent, test_knowledge_agent, test_role_playing, test_agent_mcp_server, and test_function_tool_decorator. Logs report missing OPENAI_API_KEY / missing OpenAI credentials; these runs do not establish whether the Mem0 regression test passes in CI.
  • Examples and apps: examples stop during collection of test_role_description_example for the missing key. Apps finish with 7 passed, 1 skipped, and 1 failed (test_session), also for the missing key.
  • Label automation: the issues.addLabels request returns HTTP 403, Resource not accessible by integration.

The package workflow uses the repository OpenAI secret on pull_request, and label automation uses GITHUB_TOKEN. The observed failures are consistent with fork-PR secret/token restrictions. No code/test changes are proposed to work around them; a maintainer will need to use the project's approved CI path or assess these environmental checks. Pre-commit, minimal dependencies, dependency review, and CodeQL passed.

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 chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mem0Storage.save never persists: OpenAIBackendRole is read as a string

2 participants