Repository navigation
Conversation
…SS client after_run unconditionally passed both user_id and agent_id as direct kwargs to the OSS AsyncMemory client, even though _validate_filters permits configuring only one of them. When a user configured only user_id (agent_id=None), add() received agent_id=None and crashed the run after the model had already produced a reply. Match the Platform path (_build_filters), which only includes non-empty identity fields.
There was a problem hiding this comment.
🟡 Changes recommended
The regression tests lack coverage for valid agent-only configurations, so restoring an unconditional user_id assignment could go undetected.
1 open issue
What changed in this PR
This PR updates the Python Mem0 integration to omit unset identity fields when storing memories through the OSS client, matching the Platform path’s filtering behavior.
Changes:
- Forward
user_idandagent_idonly when non-empty. - Add a user-only regression test verifying that
agent_idandfiltersare absent from OSS storage arguments.
| File | Description |
|---|---|
python/packages/mem0/tests/test_mem0_context_provider.py |
Adds regression coverage for user-only OSS storage. |
python/packages/mem0/agent_framework_mem0/_context_provider.py |
Conditionally forwards identity fields to the OSS client. |
Review effort: Balanced
| async def test_oss_storage_omits_unset_identity_fields(self, mock_oss_mem0_client: AsyncMock) -> None: | ||
| """OSS add must not forward unset identity fields as None kwargs. | ||
|
|
||
| _validate_filters permits configuring only one of user_id/agent_id, but | ||
| the OSS branch previously forwarded both unconditionally, passing | ||
| ``agent_id=None`` (or ``user_id=None``) to AsyncMemory.add and crashing | ||
| the run after the model had already produced a reply. | ||
| """ | ||
| provider = Mem0ContextProvider(source_id="mem0", mem0_client=mock_oss_mem0_client, user_id="u1") | ||
| session = AgentSession(session_id="test-session") | ||
| ctx = SessionContext(input_messages=[Message(role="user", contents=["hi"])], session_id="s1") | ||
| ctx._response = AgentResponse(messages=[]) | ||
|
|
||
| await provider.after_run( | ||
| agent=cast(Any, None), | ||
| session=session, | ||
| context=ctx, | ||
| state=session.state.setdefault(provider.source_id, {}), | ||
| ) # type: ignore[arg-type] | ||
|
|
||
| call_kwargs = mock_oss_mem0_client.add.call_args.kwargs | ||
| assert call_kwargs["user_id"] == "u1" | ||
| assert "agent_id" not in call_kwargs |
Parameterize the omit-unset-identity test to exercise both user-only and agent-only configurations, asserting the configured identity is forwarded and the other key is absent.
|
Thanks for the contribution, lisry-1. The code review is clean, but the PR body still needs the required Related Issue answer before approval. Please link an agreed issue with a closing keyword, or document why this is an undisputed trivial change or repository automation. Missing AI Assistance alone is not a blocker. |
|
Thanks Eduard van Valkenburg (@eavanvalkenburg). I've opened #9976 to track this bug and linked it in the PR's Related Issue section ( |

Summary
Mem0ContextProvider.after_runforwardsuser_idandagent_idto the OSSAsyncMemoryclient as unconditional direct kwargs, even though_validate_filterspermits configuring only one of them. When a user configures onlyuser_id(soagent_id is None),add()receivesagent_id=Noneand the run crashes after the model has already produced a reply.Problem
The two client paths are asymmetric for the same identity configuration:
Platform path (
_build_filters) only includes non-empty fields:OSS path writes both unconditionally:
_validate_filtersexplicitly allows a single identity field ("At least one of the filters: agent_id, user_id, or application_id"), so auser_id-only configuration is valid — yet it passesagent_id=Noneto the OSS SDK's requiredstrparameter and fails after the reply was generated (after_runis awaited with no try/except upstream).Changes
Make the OSS branch conditional, matching
_build_filters: only includeuser_id/agent_idwhen non-empty.Tests
Added
test_oss_storage_omits_unset_identity_fields: with onlyuser_id="u1"configured,add()receivesuser_idbut noagent_idkey and nofiltersdict.