@@ -127,14 +127,18 @@ def __hash__(self) -> int:
127
127
return hash ((self .name , self .text ))
128
128
129
129
130
+ # Sentinel to autopopulate a field within model_validator
131
+ AUTOPOPULATE_VALUE = "" # NOTE: this is falsy by design
132
+
133
+
130
134
class Context (BaseModel ):
131
135
"""A class to hold the context of a question."""
132
136
133
137
model_config = ConfigDict (extra = "allow" )
134
138
135
139
id : str = Field (
140
+ default = AUTOPOPULATE_VALUE ,
136
141
description = "Unique identifier for the context. Auto-generated if not provided." ,
137
- init = False ,
138
142
)
139
143
140
144
context : str = Field (description = "Summary of the text with respect to a question." )
@@ -159,15 +163,17 @@ def __str__(self) -> str:
159
163
160
164
@model_validator (mode = "before" )
161
165
@classmethod
162
- def populate_id (cls , data : Any ) -> Any :
163
- if not data .get ("id" ):
166
+ def populate_id (cls , data : dict [ str , Any ] ) -> dict [ str , Any ] :
167
+ if not data .get ("id" ): # NOTE: this includes missing or empty strings
164
168
content = (
165
169
data .get ("question" , "" )
166
170
+ data .get ("context" , "" )[: cls .CONTEXT_ENCODING_LENGTH ]
167
171
)
168
- data ["id" ] = cls .REFERENCE_TEMPLATE .format (
169
- id = encode_id (content or str (uuid4 ()), maxsize = cls .ID_HASH_LENGTH )
170
- )
172
+ return data | { # Avoid mutating input data
173
+ "id" : cls .REFERENCE_TEMPLATE .format (
174
+ id = encode_id (content or str (uuid4 ()), maxsize = cls .ID_HASH_LENGTH )
175
+ )
176
+ }
171
177
return data
172
178
173
179
@@ -485,9 +491,6 @@ def update_other(self, other: dict[str, Any] | None = None) -> dict[str, Any]:
485
491
class DocDetails (Doc ):
486
492
model_config = ConfigDict (validate_assignment = True , extra = "ignore" )
487
493
488
- # Sentinel to auto-populate a field within model_validator
489
- AUTOPOPULATE_VALUE : ClassVar [str ] = ""
490
-
491
494
docname : str = AUTOPOPULATE_VALUE
492
495
dockey : DocKey = AUTOPOPULATE_VALUE
493
496
citation : str = AUTOPOPULATE_VALUE
0 commit comments