basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

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

Open egoes opened this issue 1 year ago • 9 comments

Summary

The basic-memory import memory-json command fails with a KeyError when attempting to import a memory.json file, indicating the import process expects a 'name' key that doesn't exist in the data structure.

Environment

  • Basic Memory version: 0.14.2
  • Installation method: Homebrew
  • Operating System: macOS
  • Python version: 3.13

Steps to Reproduce

  1. Attempt to import a memory.json file using the CLI command:
    basic-memory import memory-json path/to/memory.json
    

Expected Behavior

The memory.json file should be imported successfully without errors.

Actual Behavior

The import fails with the following error:

Importing from [PATH_REDACTED]/memory.json...writing to [PATH_REDACTED]
Failed to import memory.json
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/basic-memory/0.14.2/libexec/tools/basic-memory/lib/python3.13/site-packages/basic_memory/importers/memory_json_importer.py", line 44, in import_data
    entities[data["name"]] = data
             ~~~~^^^^^^^^
KeyError: 'name'
Error during import: 'NoneType' object has no attribute 'success'

Additional Context

  • The error occurs in memory_json_importer.py at line 44
  • The secondary error "NoneType object has no attribute 'success'" occurs after the initial KeyError
  • The memory.json file being imported was generated by the Knowledge Graph Memory Server: https://github.com/modelcontextprotocol/servers/tree/main/src/memory

This issue was written with assistance from Claude (Anthropic) to help structure the bug report and redact private information.

egoes avatar Jul 18 '25 07:07 egoes

@egoes this is probably the same issue as #234. We'll implement a fix, but you can see that issue for a workaround in the meantime.

phernandez avatar Jul 18 '25 21:07 phernandez

The workaround unfortunately didn’t work for me. Looking more closely, the errors don’t seem to be of the same nature. I will have to wait for your fix as I’m at the limit of what I feel able to understand. Thank you.

egoes avatar Jul 19 '25 07:07 egoes

The workaround unfortunately didn’t work for me. Looking more closely, the errors don’t seem to be of the same nature. I will have to wait for your fix as I’m at the limit of what I feel able to understand. Thank you.

@egoes Can you provide a sample (sanitized of any personal info) of your memory.json file that is failing? I've not seen a memory file without a name field for each entity. Thanks!

jope-bm avatar Jul 25 '25 17:07 jope-bm

@jope-bm Sorry, missed your comment. The file is entirely personal info, so I’m a bit puzzled about how I could get something out to you. However, let me try and also explain what I see.

If I look at what I can understand from such a file, I actually see name fields for every entity.

When I open the file, one thing that seems curious to me (again, my knowledge is limited) is that entities are not separated by commas. I have the plugin Error Lens installed in VSCode and it is throwing me an error at the end of the second line. Please forgive the very blurred screenshot I enclosed, it is just to show you.

Image

If it is of any interest, the json file has 332 lines and is 249K. The first entity/line holds for half of the file. Let me know how I can help further!

egoes avatar Jul 28 '25 13:07 egoes

@jope-bm Sorry, missed your comment. The file is entirely personal info, so I’m a bit puzzled about how I could get something out to you. However, let me try and also explain what I see.

If I look at what I can understand from such a file, I actually see name fields for every entity.

When I open the file, one thing that seems curious to me (again, my knowledge is limited) is that entities are not separated by commas. I have the plugin Error Lens installed in VSCode and it is throwing me an error at the end of the second line. Please forgive the very blurred screenshot I enclosed, it is just to show you.

@egoes Ok, to clarify the missing commas, the memory.json file is actually in a "JSON log" format. Which means each line in the file is its own separate JSON object. We parse out each line from the file as a separate JSON object.

The error that you're getting

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/basic-memory/0.14.2/libexec/tools/basic-memory/lib/python3.13/site-packages/basic_memory/importers/memory_json_importer.py", line 44, in import_data
    entities[data["name"]] = data
             ~~~~^^^^^^^^
KeyError: 'name'

Implies that you have entity objects in the file that are missing the name field. This is what is curious to me. Can you find any entity objects in your memory.json that are missing the name field? If so, what are the field names for that entity (ex., type, entityType, observations, etc)?

jope-bm avatar Jul 28 '25 14:07 jope-bm

@jope-bm I understand (and learned something!)

I found one line that has {"type":"entity","entityName »: »… Rename it and try another import? I don’t have a clue how this odd one got there.

Image

egoes avatar Jul 28 '25 15:07 egoes

@egoes Oh wow, yeah that is odd that only one entity exists list that. I'll update our code to check for it just in case there are others out there (maybe an old version of KGMS was writing it).

I'll also make a change so that entities not having an expected name field are just skipped and won't error-out.

jope-bm avatar Jul 28 '25 15:07 jope-bm

Ace. Thanks @jope-bm I’ll edit the file manually and try another import. Will report back

egoes avatar Jul 28 '25 16:07 egoes

Manually changed editedName to name and the import worked! Joy. Thank you for the insights and the fix.

egoes avatar Jul 28 '25 18:07 egoes