11"""Schemas for memory context."""
22
33from datetime import datetime
4- from typing import List , Optional , Annotated , Sequence
4+ from typing import List , Optional , Annotated , Sequence , Literal , Union
55
66from annotated_types import MinLen , MaxLen
77from pydantic import BaseModel , Field , BeforeValidator , TypeAdapter
@@ -118,7 +118,7 @@ def memory_url_path(url: memory_url) -> str: # pyright: ignore
118118class EntitySummary (BaseModel ):
119119 """Simplified entity representation."""
120120
121- type : str = "entity"
121+ type : Literal [ "entity" ] = "entity"
122122 permalink : Optional [str ]
123123 title : str
124124 content : Optional [str ] = None
@@ -129,7 +129,7 @@ class EntitySummary(BaseModel):
129129class RelationSummary (BaseModel ):
130130 """Simplified relation representation."""
131131
132- type : str = "relation"
132+ type : Literal [ "relation" ] = "relation"
133133 title : str
134134 file_path : str
135135 permalink : str
@@ -142,7 +142,7 @@ class RelationSummary(BaseModel):
142142class ObservationSummary (BaseModel ):
143143 """Simplified observation representation."""
144144
145- type : str = "observation"
145+ type : Literal [ "observation" ] = "observation"
146146 title : str
147147 file_path : str
148148 permalink : str
@@ -169,17 +169,21 @@ class MemoryMetadata(BaseModel):
169169class ContextResult (BaseModel ):
170170 """Context result containing a primary item with its observations and related items."""
171171
172- primary_result : EntitySummary | RelationSummary | ObservationSummary = Field (
173- description = "Primary item"
174- )
172+ primary_result : Annotated [
173+ Union [EntitySummary , RelationSummary , ObservationSummary ],
174+ Field (discriminator = "type" , description = "Primary item" )
175+ ]
175176
176177 observations : Sequence [ObservationSummary ] = Field (
177178 description = "Observations belonging to this entity" , default_factory = list
178179 )
179180
180- related_results : Sequence [EntitySummary | RelationSummary | ObservationSummary ] = Field (
181- description = "Related items" , default_factory = list
182- )
181+ related_results : Sequence [
182+ Annotated [
183+ Union [EntitySummary , RelationSummary , ObservationSummary ],
184+ Field (discriminator = "type" )
185+ ]
186+ ] = Field (description = "Related items" , default_factory = list )
183187
184188
185189class GraphContext (BaseModel ):
0 commit comments