Skip to content

Fix handle malformed entity dicts and None LLM response in memgraph_memory#4238

Merged
deshraj merged 3 commits intomem0ai:mainfrom
liviaellen:fix/memgraph-malformed-entity-handling
Mar 7, 2026
Merged

Fix handle malformed entity dicts and None LLM response in memgraph_memory#4238
deshraj merged 3 commits intomem0ai:mainfrom
liviaellen:fix/memgraph-malformed-entity-handling

Conversation

@liviaellen
Copy link
Copy Markdown
Contributor

@liviaellen liviaellen commented Mar 7, 2026

Two crashes in memgraph_memory.py when calling memory.add() with graph memory enabled.

Fixes #4055 | Related #2054

Bugs Fixed

1. KeyError: 'entity_type'
When the LLM returns a malformed entity like {"entity": "task"} with no entity_type, the loop
crashes and drops all remaining entities silently. Fixed by skipping invalid items instead of
stopping the loop.

2. TypeError: 'NoneType' object is not subscriptable
When using openai_structured, message.content is None when a tool call is made. The code did
extracted_entities["tool_calls"] directly on that None and crashed. Fixed by adding a null
check, same pattern already used in graph_memory.py.

Bug 2 only surfaces when switching to openai_structured as a workaround for Bug 1, leaving
users with no working option.

Type of change

  • Bug fix

How Has This Been Tested?

  • Unit Test

Added tests/memory/test_memgraph_memory.py. All 46 existing tests still pass.

Bug 2 reproduced with real OpenAI API (gpt-4o-mini):

Input message.content Error
"Hello world" "Hello! How can I assist you today?" TypeError: string indices must be integers
"The sky is blue." None TypeError: 'NoneType' object is not subscriptable

Both crash at extracted_entities["tool_calls"] , exact error from issue #4055.

…es_from_data instead of crashing the entire loop (KeyError: entity_type), fix in _establish_nodes_relations_from_data when LLM returns None (TypeError: NoneType object is not subscriptable) — matches pattern in graph_memory.py, issues mem0ai#4055 , related mem0ai#2054
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 7, 2026

CLA assistant check
All committers have signed the CLA.

deshraj
deshraj previously approved these changes Mar 7, 2026
@liviaellen
Copy link
Copy Markdown
Contributor Author

fix linting issues ^ @deshraj

@deshraj deshraj merged commit 72e2c5c into mem0ai:main Mar 7, 2026
6 of 7 checks passed
iamvaleriofantozzi pushed a commit to iamvaleriofantozzi/mem0-1 that referenced this pull request Mar 19, 2026
jamebobob pushed a commit to jamebobob/mem0-vigil-recall that referenced this pull request Mar 29, 2026
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.

Error in _retrieve_nodes_from_data during mem.add, and its workaround

3 participants