Skip to content

fix: skip malformed entities in _remove_spaces_from_entities#4184

Open
Br1an67 wants to merge 1 commit intomem0ai:mainfrom
Br1an67:fix/issue-3907-empty-entity-filter
Open

fix: skip malformed entities in _remove_spaces_from_entities#4184
Br1an67 wants to merge 1 commit intomem0ai:mainfrom
Br1an67:fix/issue-3907-empty-entity-filter

Conversation

@Br1an67
Copy link
Copy Markdown
Contributor

@Br1an67 Br1an67 commented Mar 1, 2026

Description

When the LLM returns empty dicts ({}) or dicts missing required keys (source, relationship, destination) in the entity deletion list, _remove_spaces_from_entities crashes with a KeyError. This happens when the LLM determines no entities need to be deleted but still returns [{}] instead of [].

This PR filters out invalid entries before processing — items that are empty or missing any of the three required keys are silently skipped.

Fixes #3907

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit tests
pytest tests/memory/test_neo4j_cypher_syntax.py::TestRemoveSpacesFromEntities -v

4 new tests covering: empty dicts, incomplete dicts, all-empty list, and normal entities.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes
  • My changes generate no new warnings

When the LLM returns empty dicts ({}) or dicts missing required keys
(source, relationship, destination) in the entity list, the method
crashes with a KeyError. Filter out invalid entries before processing.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 1, 2026

CLA assistant check
All committers have signed the CLA.

@Br1an67
Copy link
Copy Markdown
Contributor Author

Br1an67 commented Mar 9, 2026

Hi — just a gentle bump on this. Happy to make any changes if needed!

@ionicus
Copy link
Copy Markdown

ionicus commented Mar 9, 2026

We're hitting the same bug independently with Ollama + Memgraph (as opposed to Kuzu in #3907). When the LLM returns relation dicts missing source/relationship/destination keys, _remove_spaces_from_entities crashes with KeyError. We've been running a local monkey-patch that filters out malformed entities — essentially the same approach as this PR. Can confirm the fix works well in production.

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

Labels

None yet

Projects

None yet

3 participants