Skip to content

Commit bc3b95c

Browse files
fix: make RelationResponse.from_id optional to handle null permalinks
When entities are imported from environments where permalinks were not enabled, the from_entity.permalink can be None. This causes Pydantic validation to fail because from_id was typed as non-optional Permalink. This fix: - Changes from_id to Optional[Permalink] with default=None - Matches the existing pattern for to_id field - Adds test case for null permalink validation - Maintains backwards compatibility Fixes #483 Co-authored-by: Paul Hernandez <[email protected]>
1 parent 48e6e84 commit bc3b95c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/basic_memory/schemas/response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ class RelationResponse(Relation, SQLAlchemyModel):
6464

6565
permalink: Permalink
6666

67-
from_id: Permalink = Field(
67+
from_id: Optional[Permalink] = Field(
6868
# use the permalink from the associated Entity
6969
# or the from_id value
7070
validation_alias=AliasChoices(
7171
AliasPath("from_entity", "permalink"),
7272
"from_id",
73-
)
73+
),
74+
default=None,
7475
)
7576
to_id: Optional[Permalink] = Field( # pyright: ignore
7677
# use the permalink from the associated Entity

tests/schemas/test_schemas.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ def test_relation_response():
9191
assert relation.context is None
9292

9393

94+
def test_relation_response_with_null_permalink():
95+
"""Test RelationResponse handles null permalinks (fixes issue #483).
96+
97+
When entities are imported from environments without permalinks enabled,
98+
the from_entity.permalink and to_entity.permalink can be None.
99+
This should not cause validation errors.
100+
"""
101+
data = {
102+
"permalink": "test/relation/123",
103+
"from_id": None, # Can be None when from_entity.permalink is None
104+
"to_id": None, # Can be None when to_entity.permalink is None
105+
"relation_type": "relates_to",
106+
"from_entity": {"permalink": None}, # Null permalink
107+
"to_entity": {"permalink": None}, # Null permalink
108+
}
109+
relation = RelationResponse.model_validate(data)
110+
assert relation.from_id is None
111+
assert relation.to_id is None
112+
assert relation.relation_type == "relates_to"
113+
114+
94115
def test_entity_out_from_attributes():
95116
"""Test EntityOut creation from database model attributes."""
96117
# Simulate database model attributes

0 commit comments

Comments
 (0)