Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: Remove extraneous relationships on ORM objects (#1755)
Matthew Zhou748e8d683764
diff --git a/letta/helpers/converters.py b/letta/helpers/converters.pyindex 853ad7272..88dacef9e 100644--- a/letta/helpers/converters.py+++ b/letta/helpers/converters.py@@ -154,7 +154,7 @@ def deserialize_tool_calls(data: Optional[List[Dict]]) -> List[OpenAIToolCall]:calls = []for item in data:func_data = item.pop("function", None)- tool_call_function = OpenAIFunction(**func_data) if func_data else None+ tool_call_function = OpenAIFunction(**func_data)calls.append(OpenAIToolCall(function=tool_call_function, **item))return callsdiff --git a/letta/orm/agent.py b/letta/orm/agent.pyindex f8da449af..fe5d7a74e 100644--- a/letta/orm/agent.py+++ b/letta/orm/agent.py@@ -7,7 +7,6 @@ from sqlalchemy.orm import Mapped, mapped_column, relationshipfrom letta.orm.block import Blockfrom letta.orm.custom_columns import EmbeddingConfigColumn, LLMConfigColumn, ToolRulesColumnfrom letta.orm.identity import Identity-from letta.orm.message import Messagefrom letta.orm.mixins import OrganizationMixinfrom letta.orm.organization import Organizationfrom letta.orm.sqlalchemy_base import SqlalchemyBase@@ -91,13 +90,6 @@ class Agent(SqlalchemyBase, OrganizationMixin):back_populates="agents",doc="Blocks forming the core memory of the agent.",)- messages: Mapped[List["Message"]] = relationship(- "Message",- back_populates="agent",- lazy="selectin",- cascade="all, delete-orphan", # Ensure messages are deleted when the agent is deleted- passive_deletes=True,- )tags: Mapped[List["AgentsTags"]] = relationship("AgentsTags",back_populates="agent",@@ -105,25 +97,6 @@ class Agent(SqlalchemyBase, OrganizationMixin):lazy="selectin",doc="Tags associated with the agent.",)- source_passages: Mapped[List["SourcePassage"]] = relationship(- "SourcePassage",- secondary="sources_agents", # The join table for Agent -> Source- primaryjoin="Agent.id == sources_agents.c.agent_id",- secondaryjoin="and_(SourcePassage.source_id == sources_agents.c.source_id)",- lazy="selectin",- order_by="SourcePassage.created_at.desc()",- viewonly=True, # Ensures SQLAlchemy doesn't attempt to manage this relationship- doc="All passages derived from sources associated with this agent.",- )- agent_passages: Mapped[List["AgentPassage"]] = relationship(- "AgentPassage",- back_populates="agent",- lazy="selectin",- order_by="AgentPassage.created_at.desc()",- cascade="all, delete-orphan",- viewonly=True, # Ensures SQLAlchemy doesn't attempt to manage this relationship- doc="All passages derived created by this agent.",- )identities: Mapped[List["Identity"]] = relationship("Identity",secondary="identities_agents",diff --git a/letta/orm/message.py b/letta/orm/message.pyindex 6e08f2d7b..a9febdb8e 100644--- a/letta/orm/message.py+++ b/letta/orm/message.py@@ -55,7 +55,6 @@ class Message(SqlalchemyBase, OrganizationMixin, AgentMixin):)# Relationships- agent: Mapped["Agent"] = relationship("Agent", back_populates="messages", lazy="selectin")organization: Mapped["Organization"] = relationship("Organization", back_populates="messages", lazy="selectin")step: Mapped["Step"] = relationship("Step", back_populates="messages", lazy="selectin")diff --git a/letta/orm/passage.py b/letta/orm/passage.pyindex 617edaeff..4bec17177 100644--- a/letta/orm/passage.py+++ b/letta/orm/passage.py@@ -14,7 +14,6 @@ from letta.settings import settingsconfig = LettaConfig()if TYPE_CHECKING:- from letta.orm.agent import Agentfrom letta.orm.organization import Organization@@ -81,8 +80,3 @@ class AgentPassage(BasePassage, AgentMixin):@declared_attrdef organization(cls) -> Mapped["Organization"]:return relationship("Organization", back_populates="agent_passages", lazy="selectin")-- @declared_attr- def agent(cls) -> Mapped["Agent"]:- """Relationship to agent"""- return relationship("Agent", back_populates="agent_passages", lazy="selectin", passive_deletes=True)diff --git a/letta/serialize_schemas/marshmallow_agent.py b/letta/serialize_schemas/marshmallow_agent.pyindex a6fb330fa..8938586c8 100644--- a/letta/serialize_schemas/marshmallow_agent.py+++ b/letta/serialize_schemas/marshmallow_agent.py@@ -4,6 +4,7 @@ from marshmallow import fields, post_dump, pre_loadimport lettafrom letta.orm import Agent+from letta.orm import Message as MessageModelfrom letta.schemas.agent import AgentState as PydanticAgentStatefrom letta.schemas.user import Userfrom letta.serialize_schemas.marshmallow_agent_environment_variable import SerializedAgentEnvironmentVariableSchema@@ -35,7 +36,6 @@ class MarshmallowAgentSchema(BaseSchema):tool_rules = ToolRulesField()- messages = fields.List(fields.Nested(SerializedMessageSchema))core_memory = fields.List(fields.Nested(SerializedBlockSchema))tools = fields.List(fields.Nested(SerializedToolSchema))tool_exec_environment_variables = fields.List(fields.Nested(SerializedAgentEnvironmentVariableSchema))@@ -54,6 +54,30 @@ class MarshmallowAgentSchema(BaseSchema):field.schema.session = sessionfield.schema.actor = actor+ @post_dump+ def attach_messages(self, data: Dict, **kwargs):+ """+ After dumping the agent, load all its Message rows and serialize them here.+ """+ # TODO: This is hacky, but want to move fast, please refactor moving forward+ from letta.server.db import db_context as session_maker++ with session_maker() as session:+ agent_id = data.get("id")+ msgs = (+ session.query(MessageModel)+ .filter(+ MessageModel.agent_id == agent_id,+ MessageModel.organization_id == self.actor.organization_id,+ )+ .order_by(MessageModel.sequence_id.asc())+ .all()+ )+ # overwrite the “messages” key with a fully serialized list+ data[self.FIELD_MESSAGES] = [SerializedMessageSchema(session=self.session, actor=self.actor).dump(m) for m in msgs]++ return data+@post_dumpdef sanitize_ids(self, data: Dict, **kwargs):"""@@ -101,25 +125,6 @@ class MarshmallowAgentSchema(BaseSchema):del data[self.FIELD_VERSION]return data- @pre_load- def remap_in_context_messages(self, data, **kwargs):- """- Restores `message_ids` by collecting message IDs where `in_context` is True,- generates new IDs for all messages, and removes `in_context` from all messages.- """- messages = data.get(self.FIELD_MESSAGES, [])- for msg in messages:- msg[self.FIELD_ID] = SerializedMessageSchema.generate_id() # Generate new ID-- message_ids = []- in_context_message_indices = data.pop(self.FIELD_IN_CONTEXT_INDICES)- for idx in in_context_message_indices:- message_ids.append(messages[idx][self.FIELD_ID])-- data[self.FIELD_MESSAGE_IDS] = message_ids-- return data-class Meta(BaseSchema.Meta):model = Agentexclude = BaseSchema.Meta.exclude + (@@ -127,8 +132,6 @@ class MarshmallowAgentSchema(BaseSchema):"template_id","base_template_id","sources",- "source_passages",- "agent_passages","identities","is_deleted","groups",diff --git a/letta/serialize_schemas/marshmallow_message.py b/letta/serialize_schemas/marshmallow_message.pyindex 664947f84..1a8a8c22f 100644--- a/letta/serialize_schemas/marshmallow_message.py+++ b/letta/serialize_schemas/marshmallow_message.py@@ -39,4 +39,4 @@ class SerializedMessageSchema(BaseSchema):class Meta(BaseSchema.Meta):model = Message- exclude = BaseSchema.Meta.exclude + ("step", "job_message", "agent", "otid", "is_deleted")+ exclude = BaseSchema.Meta.exclude + ("step", "job_message", "otid", "is_deleted")diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex c561e2f6f..7295841e1 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -175,7 +175,7 @@ async def import_agent_serialized(raise HTTPException(status_code=400, detail="Corrupted agent file format.")except ValidationError as e:- raise HTTPException(status_code=422, detail=f"Invalid agent schema: {e.errors()}")+ raise HTTPException(status_code=422, detail=f"Invalid agent schema: {str(e)}")except IntegrityError as e:raise HTTPException(status_code=409, detail=f"Database integrity error: {str(e)}")diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex a13af948c..1eacb90cf 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -48,6 +48,7 @@ from letta.schemas.tool_rule import TerminalToolRule as PydanticTerminalToolRulefrom letta.schemas.tool_rule import ToolRule as PydanticToolRulefrom letta.schemas.user import User as PydanticUserfrom letta.serialize_schemas import MarshmallowAgentSchema+from letta.serialize_schemas.marshmallow_message import SerializedMessageSchemafrom letta.serialize_schemas.marshmallow_tool import SerializedToolSchemafrom letta.serialize_schemas.pydantic_agent_schema import AgentSchemafrom letta.services.block_manager import BlockManager@@ -528,7 +529,6 @@ class AgentManager:@enforce_typesdef serialize(self, agent_id: str, actor: PydanticUser) -> AgentSchema:with self.session_maker() as session:- # Retrieve the agentagent = AgentModel.read(db_session=session, identifier=agent_id, actor=actor)schema = MarshmallowAgentSchema(session=session, actor=actor)data = schema.dump(agent)@@ -544,12 +544,24 @@ class AgentManager:project_id: Optional[str] = None,strip_messages: Optional[bool] = False,) -> PydanticAgentState:- serialized_agent = serialized_agent.model_dump()- tool_data_list = serialized_agent.pop("tools", [])+ serialized_agent_dict = serialized_agent.model_dump()+ tool_data_list = serialized_agent_dict.pop("tools", [])+ messages = serialized_agent_dict.pop(MarshmallowAgentSchema.FIELD_MESSAGES, [])++ for msg in messages:+ msg[MarshmallowAgentSchema.FIELD_ID] = SerializedMessageSchema.generate_id() # Generate new ID++ message_ids = []+ in_context_message_indices = serialized_agent_dict.pop(MarshmallowAgentSchema.FIELD_IN_CONTEXT_INDICES)+ for idx in in_context_message_indices:+ message_ids.append(messages[idx][MarshmallowAgentSchema.FIELD_ID])++ serialized_agent_dict[MarshmallowAgentSchema.FIELD_MESSAGE_IDS] = message_idswith self.session_maker() as session:schema = MarshmallowAgentSchema(session=session, actor=actor)- agent = schema.load(serialized_agent, session=session)+ agent = schema.load(serialized_agent_dict, session=session)+if append_copy_suffix:agent.name += "_copy"if project_id:@@ -559,17 +571,23 @@ class AgentManager:# we want to strip all but the first (system) messageagent.message_ids = [agent.message_ids[0]]agent = agent.create(session, actor=actor)+pydantic_agent = agent.to_pydantic()+ pyd_msgs = []+ message_schema = SerializedMessageSchema(session=session, actor=actor)++ for serialized_message in messages:+ pydantic_message = message_schema.load(serialized_message, session=session).to_pydantic()+ pydantic_message.agent_id = agent.id+ pyd_msgs.append(pydantic_message)+ self.message_manager.create_many_messages(pyd_msgs, actor=actor)+# Need to do this separately as there's some fancy upsert logic that SqlAlchemy cannot handlefor tool_data in tool_data_list:pydantic_tool = SerializedToolSchema(actor=actor).load(tool_data, transient=True).to_pydantic()existing_pydantic_tool = self.tool_manager.get_tool_by_name(pydantic_tool.name, actor=actor)- # If the tool exists- # AND EITHER:- # 1) override_existing_tools is set to False- # 2) existing_pydantic_tool is NOT any type of Letta core toolif existing_pydantic_tool and (existing_pydantic_tool.tool_type in {ToolType.LETTA_CORE, ToolType.LETTA_MULTI_AGENT_CORE, ToolType.LETTA_MEMORY_CORE}or not override_existing_tools@@ -787,10 +805,6 @@ class AgentManager:# Retrieve the existing agent (will raise NoResultFound if invalid)agent = AgentModel.read(db_session=session, identifier=agent_id, actor=actor)- # Because of cascade="all, delete-orphan" on agent.messages, setting- # this relationship to an empty list will physically remove them from the DB.- agent.messages = []-# Also clear out the message_ids field to keep in-context memory consistentagent.message_ids = []@@ -799,6 +813,8 @@ class AgentManager:agent_state = agent.to_pydantic()+ self.message_manager.delete_all_messages_for_agent(agent_id=agent_id, actor=actor)+if add_default_initial_messages:return self.append_initial_message_sequence_to_in_context_messages(actor, agent_state)else:diff --git a/letta/services/message_manager.py b/letta/services/message_manager.pyindex c9a5d5364..fb39b9550 100644--- a/letta/services/message_manager.py+++ b/letta/services/message_manager.py@@ -1,7 +1,7 @@import jsonfrom typing import List, Optional, Sequence-from sqlalchemy import exists, func, select, text+from sqlalchemy import delete, exists, func, select, textfrom letta.log import get_loggerfrom letta.orm.agent import Agent as AgentModel@@ -348,3 +348,25 @@ class MessageManager:# Execute and convert each Message to its Pydantic representation.results = query.all()return [msg.to_pydantic() for msg in results]++ @enforce_types+ def delete_all_messages_for_agent(self, agent_id: str, actor: PydanticUser) -> int:+ """+ Efficiently deletes all messages associated with a given agent_id,+ while enforcing permission checks and avoiding any ORM‑level loads.+ """+ with self.session_maker() as session:+ # 1) verify the agent exists and the actor has access+ AgentModel.read(db_session=session, identifier=agent_id, actor=actor)++ # 2) issue a CORE DELETE against the mapped class+ stmt = (+ delete(MessageModel).where(MessageModel.agent_id == agent_id).where(MessageModel.organization_id == actor.organization_id)+ )+ result = session.execute(stmt)++ # 3) commit once+ session.commit()++ # 4) return the number of rows deleted+ return result.rowcountdiff --git a/tests/test_agent_serialization.py b/tests/test_agent_serialization.pyindex 2bd228f9e..529b7a489 100644--- a/tests/test_agent_serialization.py+++ b/tests/test_agent_serialization.py@@ -401,9 +401,9 @@ def test_sanity_datetime_mismatch():# Agent serialize/deserialize tests[email protected]("append_copy_suffix", [True, False])-def test_append_copy_suffix_simple(local_client, server, serialize_test_agent, default_user, other_user, append_copy_suffix):+def test_deserialize_simple(local_client, server, serialize_test_agent, default_user, other_user):"""Test deserializing JSON into an Agent instance."""+ append_copy_suffix = Falseresult = server.agent_manager.serialize(agent_id=serialize_test_agent.id, actor=default_user)# Deserialize the agent