55from datetime import datetime
66from typing import Any
77
8- from pydantic import BaseModel , Field , root_validator
8+ from pydantic import BaseModel , Field , model_validator
99
1010
1111class UMAPParameters (BaseModel ):
@@ -142,11 +142,13 @@ class ClusterCharacteristic(BaseModel):
142142 top_tfidf_scores : list [float ]
143143 sample_comments : list [str ]
144144
145- @root_validator (pre = True )
146- def create_cluster_key (self , values : dict [str , Any ]) -> dict [str , Any ]:
145+ @model_validator (mode = "before" )
146+ @classmethod
147+ def create_cluster_key (cls , values : Any ) -> Any :
147148 """Create the cluster_key if not provided."""
148- if "cluster_key" not in values and "layer_id" in values and "cluster_id" in values :
149- values ["cluster_key" ] = f"layer{ values ['layer_id' ]} _{ values ['cluster_id' ]} "
149+ if isinstance (values , dict ):
150+ if "cluster_key" not in values and "layer_id" in values and "cluster_id" in values :
151+ values ["cluster_key" ] = f"layer{ values ['layer_id' ]} _{ values ['cluster_id' ]} "
150152 return values
151153
152154
@@ -159,11 +161,13 @@ class EnhancedTopicName(BaseModel):
159161 cluster_id : int
160162 topic_name : str # Format: "Keywords: word1, word2, word3, ..."
161163
162- @root_validator (pre = True )
163- def create_topic_key (self , values : dict [str , Any ]) -> dict [str , Any ]:
164+ @model_validator (mode = "before" )
165+ @classmethod
166+ def create_topic_key (cls , values : Any ) -> Any :
164167 """Create the topic_key if not provided."""
165- if "topic_key" not in values and "layer_id" in values and "cluster_id" in values :
166- values ["topic_key" ] = f"layer{ values ['layer_id' ]} _{ values ['cluster_id' ]} "
168+ if isinstance (values , dict ):
169+ if "topic_key" not in values and "layer_id" in values and "cluster_id" in values :
170+ values ["topic_key" ] = f"layer{ values ['layer_id' ]} _{ values ['cluster_id' ]} "
167171 return values
168172
169173
@@ -179,11 +183,13 @@ class LLMTopicName(BaseModel):
179183 job_id : str | None = None # ID of the job that generated this topic name
180184 created_at : str = Field (default_factory = lambda : datetime .now ().isoformat ())
181185
182- @root_validator (pre = True )
183- def create_topic_key (self , values : dict [str , Any ]) -> dict [str , Any ]:
186+ @model_validator (mode = "before" )
187+ @classmethod
188+ def create_topic_key (cls , values : Any ) -> Any :
184189 """Create the topic_key if not provided."""
185- if "topic_key" not in values and "layer_id" in values and "cluster_id" in values :
186- values ["topic_key" ] = f"layer{ values ['layer_id' ]} _{ values ['cluster_id' ]} "
190+ if isinstance (values , dict ):
191+ if "topic_key" not in values and "layer_id" in values and "cluster_id" in values :
192+ values ["topic_key" ] = f"layer{ values ['layer_id' ]} _{ values ['cluster_id' ]} "
187193 return values
188194
189195
0 commit comments