Skip to content

fix: handle missing 'name' key in memory JSON import#241

Merged
jope-bm merged 2 commits intomainfrom
fix/232-key-error-when-importing-memory-json
Jul 28, 2025
Merged

fix: handle missing 'name' key in memory JSON import#241
jope-bm merged 2 commits intomainfrom
fix/232-key-error-when-importing-memory-json

Conversation

@jope-bm
Copy link
Copy Markdown
Contributor

@jope-bm jope-bm commented Jul 28, 2025

Description:

Summary

  • Fixed KeyError when importing memory JSON files that don't have a 'name' key in entry metadata
  • Added fallback to use 'id' or 'entityName' when 'name' is missing, maintaining backward compatibility
  • Improved error handling for malformed JSON entries

Changes

  • Modified import_memory_json() in src/basic_memory/cli/import_command.py to handle missing 'name' key gracefully
  • Added fallback logic: uses 'id' or 'entityName' field when 'name' is not present in entry metadata

Test plan

  • Verify import works with JSON files containing 'name' key (existing functionality)
  • Verify import works with JSON files missing 'name' key (new functionality)
  • Confirm error handling for completely malformed entries

Fixes #232

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

jope-bm and others added 2 commits July 28, 2025 11:14
Fixes KeyError when importing memory.json files that use different
identifier keys (id, entityName) instead of 'name'. This commonly
occurs with files from Knowledge Graph Memory Server.

Changes:
- Add robust key handling to try name/entityName/id as fallback options
- Add fallback handling for entityType and observations fields
- Track and display count of skipped entities with missing identifiers
- Add comprehensive test coverage for missing name key scenarios

The importer now gracefully handles various JSON structures while
maintaining backward compatibility with existing formats.

Fixes #232

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: jope-bm <jope-bm@users.noreply.github.com>
Signed-off-by: Joe P <joe@basicmemory.com>
Apply automatic code formatting and linting fixes from ruff formatter.
These changes ensure consistent code style across the codebase without
functional modifications.

Changes:
- Fix whitespace and line ending consistency
- Apply consistent string quote formatting
- Remove trailing whitespace
- Format multi-line expressions for better readability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: jope-bm <jope-bm@users.noreply.github.com>
Signed-off-by: Joe P <joe@basicmemory.com>
@jope-bm jope-bm requested a review from phernandez July 28, 2025 17:52
if data["type"] == "entity":
entities[data["name"]] = data
# Handle different possible name keys
entity_name = data.get("name") or data.get("entityName") or data.get("id")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though I haven't encountered a memory.json with "id" in entity, Claude seems to think it's possible.

@jope-bm jope-bm merged commit a4a3b1b into main Jul 28, 2025
11 checks passed
@jope-bm jope-bm deleted the fix/232-key-error-when-importing-memory-json branch July 28, 2025 20:52
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.

[BUG] KeyError 'name' when importing memory.json file

2 participants