Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: remove organization from blocks pydantic schema (#3428)

cthomasb0e8fa6d4d6e
diff --git a/letta/orm/block.py b/letta/orm/block.py
index a5a1fcfb8..bbbb6dfc1 100644
--- a/letta/orm/block.py
+++ b/letta/orm/block.py
@@ -55,7 +55,7 @@ class Block(OrganizationMixin, SqlalchemyBase):
__mapper_args__ = {"version_id_col": version}
# relationships
- organization: Mapped[Optional["Organization"]] = relationship("Organization")
+ organization: Mapped[Optional["Organization"]] = relationship("Organization", lazy="raise")
agents: Mapped[List["Agent"]] = relationship(
"Agent",
secondary="blocks_agents",
diff --git a/letta/schemas/block.py b/letta/schemas/block.py
index 8b00d5c12..ea9f12d41 100644
--- a/letta/schemas/block.py
+++ b/letta/schemas/block.py
@@ -74,9 +74,6 @@ class Block(BaseBlock):
id: str = BaseBlock.generate_id_field()
- # associated user/agent
- organization_id: Optional[str] = Field(None, description="The unique identifier of the organization associated with the block.")
-
# default orm fields
created_by_id: Optional[str] = Field(None, description="The id of the user that made this Block.")
last_updated_by_id: Optional[str] = Field(None, description="The id of the user that last updated this Block.")
diff --git a/letta/serialize_schemas/marshmallow_block.py b/letta/serialize_schemas/marshmallow_block.py
index 15ff703be..082cd3289 100644
--- a/letta/serialize_schemas/marshmallow_block.py
+++ b/letta/serialize_schemas/marshmallow_block.py
@@ -1,3 +1,7 @@
+from typing import Dict
+
+from marshmallow import post_dump, pre_load
+
from letta.orm.block import Block
from letta.schemas.block import Block as PydanticBlock
from letta.serialize_schemas.marshmallow_base import BaseSchema
@@ -10,6 +14,24 @@ class SerializedBlockSchema(BaseSchema):
__pydantic_model__ = PydanticBlock
+ @post_dump
+ def sanitize_ids(self, data: Dict, **kwargs) -> Dict:
+ # delete id
+ del data["id"]
+ del data["_created_by_id"]
+ del data["_last_updated_by_id"]
+
+ return data
+
+ @pre_load
+ def regenerate_ids(self, data: Dict, **kwargs) -> Dict:
+ if self.Meta.model:
+ data["id"] = self.generate_id()
+ data["_created_by_id"] = self.actor.id
+ data["_last_updated_by_id"] = self.actor.id
+
+ return data
+
class Meta(BaseSchema.Meta):
model = Block
- exclude = BaseSchema.Meta.exclude + ("agents", "identities", "is_deleted", "groups")
+ exclude = BaseSchema.Meta.exclude + ("agents", "identities", "is_deleted", "groups", "organization")
diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.py
index cf3408d28..a9c7e0086 100644
--- a/letta/services/agent_manager.py
+++ b/letta/services/agent_manager.py
@@ -1320,6 +1320,8 @@ class AgentManager:
agent = schema.load(serialized_agent_dict, session=session)
agent.organization_id = actor.organization_id
+ for block in agent.core_memory:
+ block.organization_id = actor.organization_id
if append_copy_suffix:
agent.name += "_copy"
if project_id:
diff --git a/tests/test_managers.py b/tests/test_managers.py
index a34338f8a..8874403ca 100644
--- a/tests/test_managers.py
+++ b/tests/test_managers.py
@@ -3668,7 +3668,6 @@ def test_create_block(server: SyncServer, default_user):
assert block.description == block_create.description
assert block.limit == block_create.limit
assert block.metadata == block_create.metadata
- assert block.organization_id == default_user.organization_id
@pytest.mark.asyncio
@@ -3878,7 +3877,6 @@ async def test_batch_create_multiple_blocks(server: SyncServer, default_user, ev
assert label in created_by_label, f"Missing label: {label}"
blk = created_by_label[label]
assert blk.value == value
- assert blk.organization_id == default_user.organization_id
assert blk.id is not None
# Confirm all created blocks exist in the full list from get_blocks