Skip to content

RelationResponse validation fails when from_entity.permalink is null #483

@phernandez

Description

@phernandez

Problem

When viewing entities that have relations to entities with null permalinks, the API returns a 500 error:

500: Internal proxy error: 1 validation error for EntityResponseV2
relations.0.from_entity.permalink
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

Root Cause

In src/basic_memory/schemas/response.py, the RelationResponse schema has:

from_id: Permalink = Field(
    validation_alias=AliasChoices(
        AliasPath("from_entity", "permalink"),  # Tries this FIRST
        "from_id",
    )
)

When from_entity.permalink is None (which is valid for imported notes without permalinks enabled), Pydantic fails validation because from_id is typed as Permalink (non-nullable Annotated[str, MinLen(1)]).

Reproduction

  1. Import notes from a local environment where permalinks were not enabled
  2. Create a relation between an entity with a permalink and one without
  3. Try to view the entity with the relation via /v2/projects/{id}/knowledge/entities/{id}
  4. 500 error occurs

Evidence

From production Logfire trace for user with imported notes:

  • 212 out of 345 entities (61%) have null permalinks
  • Viewing entity 346 fails because relation id 205 references entity 42 which has permalink: null

Proposed Fix

Either:

  1. Make from_id optional: from_id: Optional[Permalink] = Field(...)
  2. Or add proper fallback handling when from_entity.permalink is None (use entity title or file_path as fallback)

Option 2 is preferred since it maintains backwards compatibility and provides a meaningful value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions