Skip to content

[BUG] Validation error moving a file in basic memory #155

Closed

Description

@phernandez

User is encountering validation errors when trying to move a file in basic-memory. The error log shows:

"Error moving entity: 1 validation error for EntityResponse permalink - Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]"

The move operation fails when attempting to move 'Finanza/Financial Analysis/Mastercard (MA) Financial Analysis - May 2025.md' to 'Finanza/Settori/Financial/Mastercard (MA) Financial Analysis - May 2025.md'.

I noticed in my config.json file that "update_permalinks_on_move" is currently set to false. Would changing this setting to true resolve this validation error? I'd appreciate your guidance on the correct configuration to fix this issue.

Image

basic-memory-mcp (4).log

Activity

  1. phernandez commented on Jun 20, 2025

    @phernandez
    MemberAuthor

    @claude can you look at this issue and see if you can find what is causing the error?

  2. claude commented on Jun 20, 2025

    @claude
    Contributor

    Claude finished @phernandez's task —— View job


    Analysis Complete - Root Cause Identified ✅

    I've found the exact cause of the validation error when moving files in basic-memory.

    Root Cause

    This is a schema mismatch bug between the database and API response validation:

    1. Database Migration: A recent migration (502b60eaa905_remove_required_from_entity_permalink.py) made the permalink column nullable in the database
    2. Response Schema: The EntityResponse.permalink field still requires a non-null string (Permalink = Annotated[str, MinLen(1)])
    3. Move Operation: When update_permalinks_on_move is false, no new permalink is generated, leaving the database value as NULL
    4. Validation Failure: Pydantic validation fails when trying to create an EntityResponse from an entity with permalink=None

    Your Config Question

    Yes, setting "update_permalinks_on_move": true would resolve this issue because:

    • It forces generation of a new permalink during the move operation
    • The entity would have a valid string permalink instead of NULL
    • The validation would pass

    The Real Fix Needed

    However, this is a bug that should be fixed in the code. The system should handle entities with NULL permalinks gracefully. Here are the recommended solutions:

    Option 1: Make EntityResponse.permalink Optional

    # In src/basic_memory/schemas/response.py
    permalink: Optional[Permalink] = None

    Option 2: Generate Default Permalinks

    # In the move_entity method, always ensure permalink exists
    if not updated_entity.permalink:
        # Generate permalink from file_path
        updates["permalink"] = await self.resolve_permalink(destination_path)

    Option 3: Handle NULL in Model Validation
    Add a validator to generate permalinks when missing.

    Immediate Workaround

    For now, you can resolve this by setting "update_permalinks_on_move": true in your config, but this shouldn't be necessary.

    Files Involved

    • src/basic_memory/schemas/response.py:134 - EntityResponse schema validation
    • src/basic_memory/services/entity_service.py:626-720 - move_entity method
    • src/basic_memory/alembic/versions/502b60eaa905_* - Migration that made permalinks nullable

    The migration made the database field nullable but the API schema wasn't updated to match, creating this validation mismatch.

  3. phernandez commented on Jun 20, 2025

    @phernandez
    MemberAuthor

    @claude Nice find. Can you make a PR that has a test and also a fix?

  4. claude commented on Jun 20, 2025

    @claude
    Contributor

    Claude finished @phernandez's task —— View job • claude/issue-155-20250620_134617 • Create PR ➔


    Creating PR with Test and Fix ✅

    Todo List:

    • Read CLAUDE.md for repo guidelines and setup
    • Examine current code structure (entity_service.py, schemas/response.py)
    • Create test that reproduces the validation error
    • Implement fix to ensure permalinks are generated during move operations
    • Run test suite to verify fix works
    • Run linting and type checking
    • Commit changes to branch
    • Provide PR link

    Summary

    Successfully implemented a fix for issue #155 with comprehensive test coverage.

    Root Cause: Entities with null permalinks from database migration would fail validation when creating EntityResponse objects during move operations.

    Solution: Modified move_entity method to automatically generate permalinks for entities with null permalinks, regardless of the update_permalinks_on_move configuration setting.

    Changes Made:

    • Fix: src/basic_memory/services/entity_service.py - Added null permalink detection and generation
    • Test: tests/services/test_entity_service.py - Added test_move_entity_with_null_permalink_generates_permalink

    Key Benefits:

    • ✅ Prevents validation errors for migrated entities
    • ✅ Maintains backward compatibility with existing settings
    • ✅ Ensures all entities have valid permalinks after move operations
    • ✅ Comprehensive test coverage for the fix

  5. added this to the v0.14.0 milestone on Jun 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions