Skip to content

[BUG]Mem0 contextual memory fails #2591

Closed
@dperssongrt

Description

@dperssongrt

Description

2 methods incorrectly attempt to access memories that are returned by Mem0

The issue is in the file crewai.memory.contextual.contextual_memory.py

The search on line 56:
stm_results = self.stm.search(query)

Results look like this when returned
[
{
'id': 'an id',
'metadata': 'some data',
'context': 'some context',
'score': 0.37634707735140455
}
]

This is then executed
formatted_results = "\n".join(
[
f"- {result['memory'] if self.memory_provider == 'mem0' else result['context']}"
for result in stm_results
]
)

Which then leads to this error:
File "/Users/dpersson/projects/ted-crew/venv/lib/python3.12/site-packages/crewai/memory/contextual/contextual_memory.py", line 95, in _fetch_entity_context
f"- {result['memory'] if self.memory_provider == 'mem0' else result['context']}"
~~~~~~^^^^^^^^^^
KeyError: 'memory'

Maybe you meant to do context or metadata?
I also just modified the file like so:
f"- {result if self.memory_provider == 'mem0' else result['context']}"

That seemed to work but I am unsure if specifying a key is what you intended.

Steps to Reproduce

Using Mem0 retrieve any memories

Expected behavior

Memories should be retrieved

Screenshots/Code snippets

File "/Users/dpersson/projects/ted-crew/venv/lib/python3.12/site-packages/crewai/memory/contextual/contextual_memory.py", line 95, in _fetch_entity_context
f"- {result['memory'] if self.memory_provider == 'mem0' else result['context']}"
~~~~~~^^^^^^

Operating System

macOS Sonoma

Python Version

3.12

crewAI Version

0.114.0

crewAI Tools Version

0.40.1

Virtual Environment

Venv

Evidence

File "/Users/dpersson/projects/ted-crew/venv/lib/python3.12/site-packages/crewai/memory/contextual/contextual_memory.py", line 95, in _fetch_entity_context
f"- {result['memory'] if self.memory_provider == 'mem0' else result['context']}"
~~~~~~^^^^^^

Possible Solution

Either choose a valid key to return or return the entire result

Additional context

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions