@@ -30,6 +30,15 @@ def validate(self, typename: str, payload: dict[str, Any]) -> dict[str, Any]:
3030 except ValidationError as e :
3131 raise ValidationFailed (str (e ))
3232
33+ def get_type_by_model (self , model_class : type [BaseModel ]) -> str | None :
34+ """
35+ Reverse lookup: finds the registered type name for a given Pydantic class.
36+ """
37+ for type_name , cls in self ._schemas .items ():
38+ if cls == model_class :
39+ return type_name
40+ return None
41+
3342
3443class Constraint :
3544 def __init__ (self , singleton_key : str | None = None , immutable : bool = False ) -> None :
@@ -121,6 +130,30 @@ def commit(
121130
122131 raise e
123132
133+ def commit_model (
134+ self ,
135+ model : BaseModel ,
136+ session_id : str | None = None ,
137+ ephemeral : bool = False ,
138+ actor : str | None = None ,
139+ reason : str | None = None ,
140+ ) -> str :
141+ """
142+ Commit a Pydantic model instance directly.
143+ Auto-detects the schema type from the registry.
144+ """
145+ schema_type = self ._schema_registry .get_type_by_model (model .__class__ )
146+
147+ if not schema_type :
148+ raise MemoryStoreError (
149+ f"Model class '{ model .__class__ .__name__ } ' is not registered. "
150+ f"Please call memory.register_schema('your_type_name', { model .__class__ .__name__ } ) first."
151+ )
152+
153+ fact = Fact (type = schema_type , payload = model .model_dump ())
154+
155+ return self .commit (fact , session_id = session_id , ephemeral = ephemeral , actor = actor , reason = reason )
156+
124157 def update (self , fact_id : str , patch : dict [str , Any ], actor : str | None = None , reason : str | None = None ) -> str :
125158 with self ._lock :
126159 existing = self .storage .load (fact_id )
0 commit comments