Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,10 @@ def get_role_value(role: Any) -> str:
}

if isinstance(self.mem0_client, AsyncMemory):
add_kwargs["user_id"] = self.user_id
add_kwargs["agent_id"] = self.agent_id
if self.user_id:
add_kwargs["user_id"] = self.user_id
if self.agent_id:
add_kwargs["agent_id"] = self.agent_id
else:
add_kwargs["filters"] = self._build_filters()

Expand Down
39 changes: 39 additions & 0 deletions python/packages/mem0/tests/test_mem0_context_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,45 @@ async def test_oss_stores_identity_fields_as_direct_kwargs(self, mock_oss_mem0_c
assert call_kwargs["agent_id"] == "a1"
assert "filters" not in call_kwargs

@pytest.mark.parametrize(
("configured", "expected_present", "expected_absent"),
[
({"user_id": "u1"}, "user_id", "agent_id"),
({"agent_id": "a1"}, "agent_id", "user_id"),
],
ids=["user-only", "agent-only"],
)
async def test_oss_storage_omits_unset_identity_fields(
self,
mock_oss_mem0_client: AsyncMock,
configured: dict[str, str],
expected_present: str,
expected_absent: str,
) -> 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, **configured)
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[expected_present] == configured[expected_present]
assert expected_absent not in call_kwargs
assert "filters" not in call_kwargs

async def test_oss_storage_rejects_application_id(self, mock_oss_mem0_client: AsyncMock) -> None:
"""OSS storage rejects Platform-only application_id because AsyncMemory.add has no app_id parameter."""
provider = Mem0ContextProvider(
Expand Down
Loading