Skip to content

Commit

Permalink
validate model before returning
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahwooders committed Nov 26, 2024
1 parent 28c361d commit a729209
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions letta/services/block_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ def update_block(self, block_id: str, block_update: BlockUpdate, actor: Pydantic
with self.session_maker() as session:
block = BlockModel.read(db_session=session, identifier=block_id, actor=actor)
update_data = block_update.model_dump(exclude_unset=True, exclude_none=True)
validate_block_model = block.to_pydantic() # use this to ensure we end up with a valid pydantic object
# try:
# validate_block_model = Block(**update_data.items())
# except Exception as e:
# # invalid pydantic model
# raise ValueError(f"Failed to create pydantic model: {e}")
for key, value in update_data.items():
setattr(block, key, value)
try:
validate_block_model.__setattr__(key, value)
except Exception as e:
# invalid pydantic model
raise ValueError(f"Failed to set {key} to {value} on block {block_id}: {e}")
try:
block.to_pydantic()
except Exception as e:
# invalid pydantic model
raise ValueError(f"Failed to create pydantic model: {e}")
block.update(db_session=session, actor=actor)
return block.to_pydantic()

Expand Down

0 comments on commit a729209

Please sign in to comment.