Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
chore: bump v0.7.16 (#2636)
cthomas23e778c8eeda
diff --git a/alembic/versions/220856bbf43b_add_read_only_column.py b/alembic/versions/220856bbf43b_add_read_only_column.pynew file mode 100644index 000000000..a8a962def--- /dev/null+++ b/alembic/versions/220856bbf43b_add_read_only_column.py@@ -0,0 +1,35 @@+"""add read-only column++Revision ID: 220856bbf43b+Revises: 1dc0fee72dea+Create Date: 2025-05-13 14:42:17.353614++"""++from typing import Sequence, Union++import sqlalchemy as sa++from alembic import op++# revision identifiers, used by Alembic.+revision: str = "220856bbf43b"+down_revision: Union[str, None] = "1dc0fee72dea"+branch_labels: Union[str, Sequence[str], None] = None+depends_on: Union[str, Sequence[str], None] = None+++def upgrade() -> None:+ # add default value of `False`+ op.add_column("block", sa.Column("read_only", sa.Boolean(), nullable=True))+ op.execute(+ f"""+ UPDATE block+ SET read_only = False+ """+ )+ op.alter_column("block", "read_only", nullable=False)+++def downgrade() -> None:+ op.drop_column("block", "read_only")diff --git a/letta/__init__.py b/letta/__init__.pyindex 19cdb6f6b..876aac38d 100644--- a/letta/__init__.py+++ b/letta/__init__.py@@ -1,4 +1,4 @@-__version__ = "0.7.15"+__version__ = "0.7.16"# import clientsfrom letta.client.client import LocalClient, RESTClient, create_clientdiff --git a/letta/agent.py b/letta/agent.pyindex 101627c53..8ca22f319 100644--- a/letta/agent.py+++ b/letta/agent.py@@ -179,6 +179,15 @@ class Agent(BaseAgent):raise ValueError(f"Invalid JSON format in message: {text_content}")return None+ def ensure_read_only_block_not_modified(self, new_memory: Memory) -> None:+ """+ Throw an error if a read-only block has been modified+ """+ for label in self.agent_state.memory.list_block_labels():+ if self.agent_state.memory.get_block(label).read_only:+ if new_memory.get_block(label).value != self.agent_state.memory.get_block(label).value:+ raise ValueError(READ_ONLY_BLOCK_EDIT_ERROR)+def update_memory_if_changed(self, new_memory: Memory) -> bool:"""Update internal memory object and system prompt if there have been modifications.@@ -1277,6 +1286,9 @@ class Agent(BaseAgent):agent_state_copy = self.agent_state.__deepcopy__()function_args["agent_state"] = agent_state_copy # need to attach self to arg since it's dynamically linkedfunction_response = callable_func(**function_args)+ self.ensure_read_only_block_not_modified(+ new_memory=agent_state_copy.memory+ ) # memory editing tools cannot edit read-only blocksself.update_memory_if_changed(agent_state_copy.memory)elif target_letta_tool.tool_type == ToolType.EXTERNAL_COMPOSIO:action_name = generate_composio_action_from_func_name(target_letta_tool.name)diff --git a/letta/agents/helpers.py b/letta/agents/helpers.pyindex ce07bafcc..5578d1fb6 100644--- a/letta/agents/helpers.py+++ b/letta/agents/helpers.py@@ -10,14 +10,18 @@ from letta.server.rest_api.utils import create_input_messagesfrom letta.services.message_manager import MessageManager-def _create_letta_response(new_in_context_messages: list[Message], use_assistant_message: bool) -> LettaResponse:+def _create_letta_response(+ new_in_context_messages: list[Message], use_assistant_message: bool, usage: LettaUsageStatistics+) -> LettaResponse:"""Converts the newly created/persisted messages into a LettaResponse."""- response_messages = []- for msg in new_in_context_messages:- response_messages.extend(msg.to_letta_messages(use_assistant_message=use_assistant_message))- return LettaResponse(messages=response_messages, usage=LettaUsageStatistics())+ # NOTE: hacky solution to avoid returning heartbeat messages and the original user message+ filter_user_messages = [m for m in new_in_context_messages if m.role != "user"]+ response_messages = Message.to_letta_messages_from_list(+ messages=filter_user_messages, use_assistant_message=use_assistant_message, reverse=False+ )+ return LettaResponse(messages=response_messages, usage=usage)def _prepare_in_context_messages(@@ -56,6 +60,45 @@ def _prepare_in_context_messages(return current_in_context_messages, new_in_context_messages+async def _prepare_in_context_messages_async(+ input_messages: List[MessageCreate],+ agent_state: AgentState,+ message_manager: MessageManager,+ actor: User,+) -> Tuple[List[Message], List[Message]]:+ """+ Prepares in-context messages for an agent, based on the current state and a new user input.+ Async version of _prepare_in_context_messages.++ Args:+ input_messages (List[MessageCreate]): The new user input messages to process.+ agent_state (AgentState): The current state of the agent, including message buffer config.+ message_manager (MessageManager): The manager used to retrieve and create messages.+ actor (User): The user performing the action, used for access control and attribution.++ Returns:+ Tuple[List[Message], List[Message]]: A tuple containing:+ - The current in-context messages (existing context for the agent).+ - The new in-context messages (messages created from the new input).+ """++ if agent_state.message_buffer_autoclear:+ # If autoclear is enabled, only include the most recent system message (usually at index 0)+ current_in_context_messages = [+ (await message_manager.get_messages_by_ids_async(message_ids=agent_state.message_ids, actor=actor))[0]+ ]+ else:+ # Otherwise, include the full list of messages by ID for context+ current_in_context_messages = await message_manager.get_messages_by_ids_async(message_ids=agent_state.message_ids, actor=actor)++ # Create a new user message from the input and store it+ new_in_context_messages = await message_manager.create_many_messages_async(+ create_input_messages(input_messages=input_messages, agent_id=agent_state.id, actor=actor), actor=actor+ )++ return current_in_context_messages, new_in_context_messages++def serialize_message_history(messages: List[str], context: str) -> str:"""Produce an XML document like:diff --git a/letta/agents/letta_agent.py b/letta/agents/letta_agent.pyindex 2b20cfaf9..bc754de51 100644--- a/letta/agents/letta_agent.py+++ b/letta/agents/letta_agent.py@@ -4,6 +4,7 @@ import uuidfrom typing import Any, AsyncGenerator, Dict, List, Optional, Tuple, Unionfrom openai import AsyncStream+from openai.types import CompletionUsagefrom openai.types.chat import ChatCompletion, ChatCompletionChunkfrom letta.agents.base_agent import BaseAgent@@ -23,6 +24,7 @@ from letta.schemas.letta_message_content import OmittedReasoningContent, Reasonifrom letta.schemas.letta_response import LettaResponsefrom letta.schemas.message import Message, MessageCreatefrom letta.schemas.openai.chat_completion_response import ToolCall+from letta.schemas.usage import LettaUsageStatisticsfrom letta.schemas.user import Userfrom letta.server.rest_api.utils import create_letta_messages_from_llm_responsefrom letta.services.agent_manager import AgentManager@@ -47,7 +49,6 @@ class LettaAgent(BaseAgent):block_manager: BlockManager,passage_manager: PassageManager,actor: User,- use_assistant_message: bool = True,):super().__init__(agent_id=agent_id, openai_client=None, message_manager=message_manager, agent_manager=agent_manager, actor=actor)@@ -55,26 +56,31 @@ class LettaAgent(BaseAgent):# Summarizer settingsself.block_manager = block_managerself.passage_manager = passage_manager- self.use_assistant_message = use_assistant_messageself.response_messages: List[Message] = []- self.last_function_response = self._load_last_function_response()+ self.last_function_response = None++ # Cached archival memory/message size+ self.num_messages = self.message_manager.size(actor=self.actor, agent_id=agent_id)+ self.num_archival_memories = self.passage_manager.size(actor=self.actor, agent_id=agent_id)# Cached archival memory/message sizeself.num_messages = self.message_manager.size(actor=self.actor, agent_id=agent_id)self.num_archival_memories = self.passage_manager.size(actor=self.actor, agent_id=agent_id)@trace_method- async def step(self, input_messages: List[MessageCreate], max_steps: int = 10) -> LettaResponse:- agent_state = self.agent_manager.get_agent_by_id(self.agent_id, actor=self.actor)- current_in_context_messages, new_in_context_messages = await self._step(+ async def step(self, input_messages: List[MessageCreate], max_steps: int = 10, use_assistant_message: bool = True) -> LettaResponse:+ agent_state = await self.agent_manager.get_agent_by_id_async(self.agent_id, actor=self.actor)+ current_in_context_messages, new_in_context_messages, usage = await self._step(agent_state=agent_state, input_messages=input_messages, max_steps=max_steps)- return _create_letta_response(new_in_context_messages=new_in_context_messages, use_assistant_message=self.use_assistant_message)+ return _create_letta_response(+ new_in_context_messages=new_in_context_messages, use_assistant_message=use_assistant_message, usage=usage+ )async def _step(self, agent_state: AgentState, input_messages: List[MessageCreate], max_steps: int = 10- ) -> Tuple[List[Message], List[Message]]:+ ) -> Tuple[List[Message], List[Message], CompletionUsage]:current_in_context_messages, new_in_context_messages = _prepare_in_context_messages(input_messages, agent_state, self.message_manager, self.actor)@@ -84,6 +90,7 @@ class LettaAgent(BaseAgent):put_inner_thoughts_first=True,actor=self.actor,)+ usage = LettaUsageStatistics()for _ in range(max_steps):response = await self._get_ai_reply(llm_client=llm_client,@@ -95,11 +102,21 @@ class LettaAgent(BaseAgent):)tool_call = response.choices[0].message.tool_calls[0]+ reasoning = [TextContent(text=response.choices[0].message.content)] # reasoning placed into content for legacy reasons- persisted_messages, should_continue = await self._handle_ai_response(tool_call, agent_state, tool_rules_solver)+ persisted_messages, should_continue = await self._handle_ai_response(+ tool_call, agent_state, tool_rules_solver, reasoning_content=reasoning+ )self.response_messages.extend(persisted_messages)new_in_context_messages.extend(persisted_messages)+ # update usage+ # TODO: add run_id+ usage.step_count += 1+ usage.completion_tokens += response.usage.completion_tokens+ usage.prompt_tokens += response.usage.prompt_tokens+ usage.total_tokens += response.usage.total_tokens+if not should_continue:break@@ -108,17 +125,17 @@ class LettaAgent(BaseAgent):message_ids = [m.id for m in (current_in_context_messages + new_in_context_messages)]self.agent_manager.set_in_context_messages(agent_id=self.agent_id, message_ids=message_ids, actor=self.actor)- return current_in_context_messages, new_in_context_messages+ return current_in_context_messages, new_in_context_messages, usage@trace_methodasync def step_stream(- self, input_messages: List[MessageCreate], max_steps: int = 10, use_assistant_message: bool = False+ self, input_messages: List[MessageCreate], max_steps: int = 10, use_assistant_message: bool = True) -> AsyncGenerator[str, None]:"""Main streaming loop that yields partial tokens.Whenever we detect a tool call, we yield from _handle_ai_response as well."""- agent_state = self.agent_manager.get_agent_by_id(self.agent_id, actor=self.actor)+ agent_state = await self.agent_manager.get_agent_by_id_async(self.agent_id, actor=self.actor)current_in_context_messages, new_in_context_messages = _prepare_in_context_messages(input_messages, agent_state, self.message_manager, self.actor)@@ -128,6 +145,7 @@ class LettaAgent(BaseAgent):put_inner_thoughts_first=True,actor=self.actor,)+ usage = LettaUsageStatistics()for _ in range(max_steps):stream = await self._get_ai_reply(@@ -137,7 +155,6 @@ class LettaAgent(BaseAgent):tool_rules_solver=tool_rules_solver,stream=True,)-# TODO: THIS IS INCREDIBLY UGLY# TODO: THERE ARE MULTIPLE COPIES OF THE LLM_CONFIG EVERYWHERE THAT ARE GETTING MANIPULATEDinterface = AnthropicStreamingInterface(@@ -146,6 +163,12 @@ class LettaAgent(BaseAgent):async for chunk in interface.process(stream):yield f"data: {chunk.model_dump_json()}\n\n"+ # update usage+ usage.step_count += 1+ usage.completion_tokens += interface.output_tokens+ usage.prompt_tokens += interface.input_tokens+ usage.total_tokens += interface.input_tokens + interface.output_tokens+# Process resulting stream contenttool_call = interface.get_tool_call_object()reasoning_content = interface.get_reasoning_content()@@ -160,6 +183,10 @@ class LettaAgent(BaseAgent):self.response_messages.extend(persisted_messages)new_in_context_messages.extend(persisted_messages)+ if not use_assistant_message or should_continue:+ tool_return = [msg for msg in persisted_messages if msg.role == "tool"][-1].to_letta_messages()[0]+ yield f"data: {tool_return.model_dump_json()}\n\n"+if not should_continue:break@@ -174,7 +201,7 @@ class LettaAgent(BaseAgent):self.num_archival_memories = self.passage_manager.size(actor=self.actor, agent_id=agent_state.id)# TODO: Also yield out a letta usage stats SSE-+ yield f"data: {usage.model_dump_json()}\n\n"yield f"data: {MessageStreamStatus.done.model_dump_json()}\n\n"@trace_method@@ -214,6 +241,8 @@ class LettaAgent(BaseAgent):]# Mirror the sync agent loop: get allowed tools or allow all if none are allowed+ if self.last_function_response is None:+ self.last_function_response = await self._load_last_function_response_async()valid_tool_names = tool_rules_solver.get_allowed_tool_names(available_tools=set([t.name for t in tools]),last_function_response=self.last_function_response,@@ -307,7 +336,7 @@ class LettaAgent(BaseAgent):pre_computed_assistant_message_id=pre_computed_assistant_message_id,pre_computed_tool_message_id=pre_computed_tool_message_id,)- persisted_messages = self.message_manager.create_many_messages(tool_call_messages, actor=self.actor)+ persisted_messages = await self.message_manager.create_many_messages_async(tool_call_messages, actor=self.actor)self.last_function_response = function_responsereturn persisted_messages, continue_stepping@@ -359,7 +388,6 @@ class LettaAgent(BaseAgent):block_manager=self.block_manager,passage_manager=self.passage_manager,actor=self.actor,- use_assistant_message=True,)augmented_message = (@@ -394,9 +422,9 @@ class LettaAgent(BaseAgent):results = await asyncio.gather(*tasks)return results- def _load_last_function_response(self):+ async def _load_last_function_response_async(self):"""Load the last function response from message history"""- in_context_messages = self.agent_manager.get_in_context_messages(agent_id=self.agent_id, actor=self.actor)+ in_context_messages = await self.agent_manager.get_in_context_messages_async(agent_id=self.agent_id, actor=self.actor)for msg in reversed(in_context_messages):if msg.role == MessageRole.tool and msg.content and len(msg.content) == 1 and isinstance(msg.content[0], TextContent):text_content = msg.content[0].textdiff --git a/letta/agents/letta_agent_batch.py b/letta/agents/letta_agent_batch.pyindex cbe17e1ae..46800bccf 100644--- a/letta/agents/letta_agent_batch.py+++ b/letta/agents/letta_agent_batch.py@@ -7,7 +7,7 @@ from aiomultiprocess import Poolfrom anthropic.types.beta.messages import BetaMessageBatchCanceledResult, BetaMessageBatchErroredResult, BetaMessageBatchSucceededResultfrom letta.agents.base_agent import BaseAgent-from letta.agents.helpers import _prepare_in_context_messages+from letta.agents.helpers import _prepare_in_context_messages_asyncfrom letta.helpers import ToolRulesSolverfrom letta.helpers.datetime_helpers import get_utc_timefrom letta.helpers.tool_execution_helper import enable_strict_mode@@ -107,7 +107,6 @@ class LettaAgentBatch(BaseAgent):sandbox_config_manager: SandboxConfigManager,job_manager: JobManager,actor: User,- use_assistant_message: bool = True,max_steps: int = 10,):self.message_manager = message_manager@@ -117,7 +116,6 @@ class LettaAgentBatch(BaseAgent):self.batch_manager = batch_managerself.sandbox_config_manager = sandbox_config_managerself.job_manager = job_manager- self.use_assistant_message = use_assistant_messageself.actor = actorself.max_steps = max_steps@@ -128,6 +126,7 @@ class LettaAgentBatch(BaseAgent):letta_batch_job_id: str,agent_step_state_mapping: Optional[Dict[str, AgentStepState]] = None,) -> LettaBatchResponse:+ """Carry out agent steps until the LLM request is sent."""log_event(name="validate_inputs")if not batch_requests:raise ValueError("Empty list of batch_requests passed in!")@@ -135,15 +134,26 @@ class LettaAgentBatch(BaseAgent):agent_step_state_mapping = {}log_event(name="load_and_prepare_agents")- agent_messages_mapping: Dict[str, List[Message]] = {}- agent_tools_mapping: Dict[str, List[dict]] = {}+ # prepares (1) agent states, (2) step states, (3) LLMBatchItems (4) message batch_item_ids (5) messages per agent (6) tools per agent++ agent_messages_mapping: dict[str, list[Message]] = {}+ agent_tools_mapping: dict[str, list[dict]] = {}# TODO: This isn't optimal, moving fast - prone to bugs because we pass around this half formed pydantic object- agent_batch_item_mapping: Dict[str, LLMBatchItem] = {}+ agent_batch_item_mapping: dict[str, LLMBatchItem] = {}++ # fetch agent states in batch+ agent_mapping = {+ agent_state.id: agent_state+ for agent_state in await self.agent_manager.get_agents_by_ids_async(+ agent_ids=[request.agent_id for request in batch_requests], actor=self.actor+ )+ }+agent_states = []for batch_request in batch_requests:agent_id = batch_request.agent_id- agent_state = self.agent_manager.get_agent_by_id(agent_id, actor=self.actor)- agent_states.append(agent_state)+ agent_state = agent_mapping[agent_id]+ agent_states.append(agent_state) # keeping this to maintain ordering, but may not be necessaryif agent_id not in agent_step_state_mapping:agent_step_state_mapping[agent_id] = AgentStepState(@@ -164,7 +174,7 @@ class LettaAgentBatch(BaseAgent):for msg in batch_request.messages:msg.batch_item_id = llm_batch_item.id- agent_messages_mapping[agent_id] = self._prepare_in_context_messages_per_agent(+ agent_messages_mapping[agent_id] = await self._prepare_in_context_messages_per_agent_async(agent_state=agent_state, input_messages=batch_request.messages)@@ -186,7 +196,7 @@ class LettaAgentBatch(BaseAgent):)log_event(name="persist_llm_batch_job")- llm_batch_job = self.batch_manager.create_llm_batch_job(+ llm_batch_job = await self.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic, # TODO: Expand to more providerscreate_batch_response=batch_response,actor=self.actor,@@ -204,7 +214,7 @@ class LettaAgentBatch(BaseAgent):if batch_items:log_event(name="bulk_create_batch_items")- batch_items_persisted = self.batch_manager.create_llm_batch_items_bulk(batch_items, actor=self.actor)+ batch_items_persisted = await self.batch_manager.create_llm_batch_items_bulk_async(batch_items, actor=self.actor)log_event(name="return_batch_response")return LettaBatchResponse(@@ -219,7 +229,7 @@ class LettaAgentBatch(BaseAgent):@trace_methodasync def resume_step_after_request(self, letta_batch_id: str, llm_batch_id: str) -> LettaBatchResponse:log_event(name="load_context")- llm_batch_job = self.batch_manager.get_llm_batch_job_by_id(llm_batch_id=llm_batch_id, actor=self.actor)+ llm_batch_job = await self.batch_manager.get_llm_batch_job_by_id_async(llm_batch_id=llm_batch_id, actor=self.actor)ctx = await self._collect_resume_context(llm_batch_id)log_event(name="update_statuses")@@ -229,7 +239,7 @@ class LettaAgentBatch(BaseAgent):exec_results = await self._execute_tools(ctx)log_event(name="persist_messages")- msg_map = self._persist_tool_messages(exec_results, ctx)+ msg_map = await self._persist_tool_messages(exec_results, ctx)log_event(name="mark_steps_done")self._mark_steps_complete(llm_batch_id, ctx.agent_ids)@@ -237,7 +247,9 @@ class LettaAgentBatch(BaseAgent):log_event(name="prepare_next")next_reqs, next_step_state = self._prepare_next_iteration(exec_results, ctx, msg_map)if len(next_reqs) == 0:- self.job_manager.update_job_by_id(job_id=letta_batch_id, job_update=JobUpdate(status=JobStatus.completed), actor=self.actor)+ await self.job_manager.update_job_by_id_async(+ job_id=letta_batch_id, job_update=JobUpdate(status=JobStatus.completed), actor=self.actor+ )return LettaBatchResponse(letta_batch_id=llm_batch_job.letta_batch_job_id,last_llm_batch_id=llm_batch_job.id,@@ -256,18 +268,22 @@ class LettaAgentBatch(BaseAgent):@trace_methodasync def _collect_resume_context(self, llm_batch_id: str) -> _ResumeContext:# NOTE: We only continue for items with successful results- batch_items = self.batch_manager.list_llm_batch_items(llm_batch_id=llm_batch_id, request_status=JobStatus.completed)+ batch_items = await self.batch_manager.list_llm_batch_items_async(llm_batch_id=llm_batch_id, request_status=JobStatus.completed)- agent_ids, agent_state_map = [], {}- provider_results, name_map, args_map, cont_map = {}, {}, {}, {}+ agent_ids = []+ provider_results = {}request_status_updates: List[RequestStatusUpdateInfo] = []for item in batch_items:aid = item.agent_idagent_ids.append(aid)- agent_state_map[aid] = self.agent_manager.get_agent_by_id(aid, actor=self.actor)provider_results[aid] = item.batch_request_result.result+ agent_states = await self.agent_manager.get_agents_by_ids_async(agent_ids, actor=self.actor)+ agent_state_map = {agent.id: agent for agent in agent_states}++ name_map, args_map, cont_map = {}, {}, {}+ for aid in agent_ids:# status bookkeepingpr = provider_results[aid]status = (@@ -344,14 +360,14 @@ class LettaAgentBatch(BaseAgent):tool_params.append(param)if rethink_memory_params:- return self._bulk_rethink_memory(rethink_memory_params)+ return await self._bulk_rethink_memory_async(rethink_memory_params)if tool_params:async with Pool() as pool:return await pool.map(execute_tool_wrapper, tool_params)@trace_method- def _bulk_rethink_memory(self, params: List[ToolExecutionParams]) -> Sequence[Tuple[str, Tuple[str, bool]]]:+ async def _bulk_rethink_memory_async(self, params: List[ToolExecutionParams]) -> Sequence[Tuple[str, Tuple[str, bool]]]:updates = {}result = []for param in params:@@ -372,11 +388,11 @@ class LettaAgentBatch(BaseAgent):# TODO: This is quite ugly and confusing - this is mostly to align with the returns of other toolsresult.append((param.agent_id, ("", True)))- self.block_manager.bulk_update_block_values(updates=updates, actor=self.actor)+ await self.block_manager.bulk_update_block_values_async(updates=updates, actor=self.actor)return result- def _persist_tool_messages(+ async def _persist_tool_messages(self,exec_results: Sequence[Tuple[str, Tuple[str, bool]]],ctx: _ResumeContext,@@ -398,7 +414,7 @@ class LettaAgentBatch(BaseAgent):)msg_map[aid] = msgs# flatten & persist- self.message_manager.create_many_messages([m for msgs in msg_map.values() for m in msgs], actor=self.actor)+ await self.message_manager.create_many_messages_async([m for msgs in msg_map.values() for m in msgs], actor=self.actor)return msg_mapdef _mark_steps_complete(self, llm_batch_id: str, agent_ids: List[str]) -> None:@@ -530,12 +546,14 @@ class LettaAgentBatch(BaseAgent):valid_tool_names = tool_rules_solver.get_allowed_tool_names(available_tools=set([t.name for t in tools]))return [enable_strict_mode(t.json_schema) for t in tools if t.name in set(valid_tool_names)]- def _prepare_in_context_messages_per_agent(self, agent_state: AgentState, input_messages: List[MessageCreate]) -> List[Message]:- current_in_context_messages, new_in_context_messages = _prepare_in_context_messages(+ async def _prepare_in_context_messages_per_agent_async(+ self, agent_state: AgentState, input_messages: List[MessageCreate]+ ) -> List[Message]:+ current_in_context_messages, new_in_context_messages = await _prepare_in_context_messages_async(input_messages, agent_state, self.message_manager, self.actor)- in_context_messages = self._rebuild_memory(current_in_context_messages + new_in_context_messages, agent_state)+ in_context_messages = await self._rebuild_memory_async(current_in_context_messages + new_in_context_messages, agent_state)return in_context_messages# TODO: Make this a bullk functiondiff --git a/letta/agents/voice_sleeptime_agent.py b/letta/agents/voice_sleeptime_agent.pyindex d3e2b70cc..86922571a 100644--- a/letta/agents/voice_sleeptime_agent.py+++ b/letta/agents/voice_sleeptime_agent.py@@ -58,7 +58,7 @@ class VoiceSleeptimeAgent(LettaAgent):def update_message_transcript(self, message_transcripts: List[str]):self.message_transcripts = message_transcripts- async def step(self, input_messages: List[MessageCreate], max_steps: int = 20) -> LettaResponse:+ async def step(self, input_messages: List[MessageCreate], max_steps: int = 20, use_assistant_message: bool = True) -> LettaResponse:"""Process the user's input message, allowing the model to call memory-related toolsuntil it decides to stop and provide a final response.@@ -74,7 +74,7 @@ class VoiceSleeptimeAgent(LettaAgent):]# Summarize- current_in_context_messages, new_in_context_messages = await super()._step(+ current_in_context_messages, new_in_context_messages, usage = await super()._step(agent_state=agent_state, input_messages=input_messages, max_steps=max_steps)new_in_context_messages, updated = self.summarizer.summarize(@@ -84,7 +84,9 @@ class VoiceSleeptimeAgent(LettaAgent):agent_id=self.agent_id, message_ids=[m.id for m in new_in_context_messages], actor=self.actor)- return _create_letta_response(new_in_context_messages=new_in_context_messages, use_assistant_message=self.use_assistant_message)+ return _create_letta_response(+ new_in_context_messages=new_in_context_messages, use_assistant_message=use_assistant_message, usage=usage+ )@trace_methodasync def _execute_tool(self, tool_name: str, tool_args: dict, agent_state: AgentState) -> Tuple[str, bool]:@@ -146,7 +148,7 @@ class VoiceSleeptimeAgent(LettaAgent):return f"Failed to store memory given start_index {start_index} and end_index {end_index}: {e}", Falseasync def step_stream(- self, input_messages: List[MessageCreate], max_steps: int = 10, use_assistant_message: bool = False+ self, input_messages: List[MessageCreate], max_steps: int = 10, use_assistant_message: bool = True) -> AsyncGenerator[Union[LettaMessage, LegacyLettaMessage, MessageStreamStatus], None]:"""This agent is synchronous-only. If called in an async context, raise an error.diff --git a/letta/client/client.py b/letta/client/client.pyindex 14fdc0091..802ca451e 100644--- a/letta/client/client.py+++ b/letta/client/client.py@@ -1,3 +1,4 @@+import asyncioimport loggingimport sysimport time@@ -3055,7 +3056,21 @@ class LocalClient(AbstractClient):Returns:tools (List[Tool]): List of tools"""- return self.server.tool_manager.list_tools(after=after, limit=limit, actor=self.user)+ # Get the current event loop or create a new one if there isn't one+ try:+ loop = asyncio.get_event_loop()+ if loop.is_running():+ # We're in an async context but can't await - use a new loop via run_coroutine_threadsafe+ concurrent_future = asyncio.run_coroutine_threadsafe(+ self.server.tool_manager.list_tools_async(actor=self.user, after=after, limit=limit), loop+ )+ return concurrent_future.result()+ else:+ # We have a loop but it's not running - we can just run the coroutine+ return loop.run_until_complete(self.server.tool_manager.list_tools_async(actor=self.user, after=after, limit=limit))+ except RuntimeError:+ # No running event loop - create a new one with asyncio.run+ return asyncio.run(self.server.tool_manager.list_tools_async(actor=self.user, after=after, limit=limit))def get_tool(self, id: str) -> Optional[Tool]:"""diff --git a/letta/constants.py b/letta/constants.pyindex 448277f84..1068c6141 100644--- a/letta/constants.py+++ b/letta/constants.py@@ -195,6 +195,9 @@ DATA_SOURCE_ATTACH_ALERT = ("[ALERT] New data was just uploaded to archival memory. You can view this data by calling the archival_memory_search tool.")+# Throw an error message when a read-only block is edited+READ_ONLY_BLOCK_EDIT_ERROR = f"{ERROR_MESSAGE_PREFIX} This block is read-only and cannot be edited."+# The ackknowledgement message used in the summarize sequenceMESSAGE_SUMMARY_REQUEST_ACK = "Understood, I will respond with a summary of the message (and only the summary, nothing else) once I receive the conversation history. I'm ready."diff --git a/letta/functions/async_composio_toolset.py b/letta/functions/async_composio_toolset.pyindex f240721e0..bcea60d61 100644--- a/letta/functions/async_composio_toolset.py+++ b/letta/functions/async_composio_toolset.py@@ -12,7 +12,7 @@ from composio.exceptions import ()-class AsyncComposioToolSet(BaseComposioToolSet, runtime="letta"):+class AsyncComposioToolSet(BaseComposioToolSet, runtime="letta", description_char_limit=1024):"""Async version of ComposioToolSet client for interacting with Composio APIUsed to asynchronously hit the execute action endpointdiff --git a/letta/interfaces/anthropic_streaming_interface.py b/letta/interfaces/anthropic_streaming_interface.pyindex 179ff11d0..d86435388 100644--- a/letta/interfaces/anthropic_streaming_interface.py+++ b/letta/interfaces/anthropic_streaming_interface.py@@ -108,6 +108,8 @@ class AnthropicStreamingInterface:raiseasync def process(self, stream: AsyncStream[BetaRawMessageStreamEvent]) -> AsyncGenerator[LettaMessage, None]:+ prev_message_type = None+ message_index = 0try:async with stream:async for event in stream:@@ -137,14 +139,17 @@ class AnthropicStreamingInterface:# TODO: Can capture signature, etc.elif isinstance(content, BetaRedactedThinkingBlock):self.anthropic_mode = EventMode.REDACTED_THINKING-+ if prev_message_type and prev_message_type != "hidden_reasoning_message":+ message_index += 1hidden_reasoning_message = HiddenReasoningMessage(id=self.letta_assistant_message_id,state="redacted",hidden_reasoning=content.data,date=datetime.now(timezone.utc).isoformat(),+ otid=Message.generate_otid_from_id(self.letta_assistant_message_id, message_index),)self.reasoning_messages.append(hidden_reasoning_message)+ prev_message_type = hidden_reasoning_message.message_typeyield hidden_reasoning_messageelif isinstance(event, BetaRawContentBlockDeltaEvent):@@ -175,12 +180,16 @@ class AnthropicStreamingInterface:self.partial_tag_buffer = combined_text[-10:] if len(combined_text) > 10 else combined_textself.accumulated_inner_thoughts.append(delta.text)+ if prev_message_type and prev_message_type != "reasoning_message":+ message_index += 1reasoning_message = ReasoningMessage(id=self.letta_assistant_message_id,reasoning=self.accumulated_inner_thoughts[-1],date=datetime.now(timezone.utc).isoformat(),+ otid=Message.generate_otid_from_id(self.letta_assistant_message_id, message_index),)self.reasoning_messages.append(reasoning_message)+ prev_message_type = reasoning_message.message_typeyield reasoning_messageelif isinstance(delta, BetaInputJSONDelta):@@ -198,21 +207,30 @@ class AnthropicStreamingInterface:inner_thoughts_diff = current_inner_thoughts[len(previous_inner_thoughts) :]if inner_thoughts_diff:+ if prev_message_type and prev_message_type != "reasoning_message":+ message_index += 1reasoning_message = ReasoningMessage(id=self.letta_assistant_message_id,reasoning=inner_thoughts_diff,date=datetime.now(timezone.utc).isoformat(),+ otid=Message.generate_otid_from_id(self.letta_assistant_message_id, message_index),)self.reasoning_messages.append(reasoning_message)+ prev_message_type = reasoning_message.message_typeyield reasoning_message# Check if inner thoughts are complete - if so, flush the bufferif not self.inner_thoughts_complete and self._check_inner_thoughts_complete(self.accumulated_tool_call_args):self.inner_thoughts_complete = True# Flush all buffered tool call messages- for buffered_msg in self.tool_call_buffer:- yield buffered_msg- self.tool_call_buffer = []+ if len(self.tool_call_buffer) > 0:+ if prev_message_type and prev_message_type != "tool_call_message":+ message_index += 1+ for buffered_msg in self.tool_call_buffer:+ buffered_msg.otid = Message.generate_otid_from_id(self.letta_tool_message_id, message_index)+ prev_message_type = buffered_msg.message_type+ yield buffered_msg+ self.tool_call_buffer = []# Start detecting special case of "send_message"if self.tool_call_name == DEFAULT_MESSAGE_TOOL and self.use_assistant_message:@@ -222,11 +240,16 @@ class AnthropicStreamingInterface:# Only stream out if it's not an empty stringif send_message_diff:- yield AssistantMessage(+ if prev_message_type and prev_message_type != "assistant_message":+ message_index += 1+ assistant_msg = AssistantMessage(id=self.letta_assistant_message_id,content=[TextContent(text=send_message_diff)],date=datetime.now(timezone.utc).isoformat(),+ otid=Message.generate_otid_from_id(self.letta_assistant_message_id, message_index),)+ prev_message_type = assistant_msg.message_type+ yield assistant_msgelse:# Otherwise, it is a normal tool call - buffer or yield based on inner thoughts statustool_call_msg = ToolCallMessage(@@ -234,8 +257,11 @@ class AnthropicStreamingInterface:tool_call=ToolCallDelta(arguments=delta.partial_json),date=datetime.now(timezone.utc).isoformat(),)-if self.inner_thoughts_complete:+ if prev_message_type and prev_message_type != "tool_call_message":+ message_index += 1+ tool_call_msg.otid = Message.generate_otid_from_id(self.letta_tool_message_id, message_index)+ prev_message_type = tool_call_msg.message_typeyield tool_call_msgelse:self.tool_call_buffer.append(tool_call_msg)@@ -249,13 +275,17 @@ class AnthropicStreamingInterface:f"Streaming integrity failed - received BetaThinkingBlock object while not in THINKING EventMode: {delta}")+ if prev_message_type and prev_message_type != "reasoning_message":+ message_index += 1reasoning_message = ReasoningMessage(id=self.letta_assistant_message_id,source="reasoner_model",reasoning=delta.thinking,date=datetime.now(timezone.utc).isoformat(),+ otid=Message.generate_otid_from_id(self.letta_assistant_message_id, message_index),)self.reasoning_messages.append(reasoning_message)+ prev_message_type = reasoning_message.message_typeyield reasoning_messageelif isinstance(delta, BetaSignatureDelta):# Safety check@@ -264,14 +294,18 @@ class AnthropicStreamingInterface:f"Streaming integrity failed - received BetaSignatureDelta object while not in THINKING EventMode: {delta}")+ if prev_message_type and prev_message_type != "reasoning_message":+ message_index += 1reasoning_message = ReasoningMessage(id=self.letta_assistant_message_id,source="reasoner_model",reasoning="",date=datetime.now(timezone.utc).isoformat(),signature=delta.signature,+ otid=Message.generate_otid_from_id(self.letta_assistant_message_id, message_index),)self.reasoning_messages.append(reasoning_message)+ prev_message_type = reasoning_message.message_typeyield reasoning_messageelif isinstance(event, BetaRawMessageStartEvent):self.message_id = event.message.iddiff --git a/letta/jobs/llm_batch_job_polling.py b/letta/jobs/llm_batch_job_polling.pyindex a12274758..e0f51dd54 100644--- a/letta/jobs/llm_batch_job_polling.py+++ b/letta/jobs/llm_batch_job_polling.py@@ -180,7 +180,7 @@ async def poll_running_llm_batches(server: "SyncServer") -> List[LettaBatchRespotry:# 1. Retrieve running batch jobs- batches = server.batch_manager.list_running_llm_batches()+ batches = await server.batch_manager.list_running_llm_batches_async()metrics.total_batches = len(batches)# TODO: Expand to more providers@@ -220,7 +220,11 @@ async def poll_running_llm_batches(server: "SyncServer") -> List[LettaBatchRespo)# launch them all at once- tasks = [_resume(server.batch_manager.get_llm_batch_job_by_id(bid)) for bid, *_ in completed]+ async def get_and_resume(batch_id):+ batch = await server.batch_manager.get_llm_batch_job_by_id_async(batch_id)+ return await _resume(batch)++ tasks = [get_and_resume(bid) for bid, *_ in completed]new_batch_responses = await asyncio.gather(*tasks, return_exceptions=True)return new_batch_responsesdiff --git a/letta/orm/agent.py b/letta/orm/agent.pyindex ed5deb5a0..e37a5ba20 100644--- a/letta/orm/agent.py+++ b/letta/orm/agent.py@@ -2,6 +2,7 @@ import uuidfrom typing import TYPE_CHECKING, List, Optional, Setfrom sqlalchemy import JSON, Boolean, Index, String+from sqlalchemy.ext.asyncio import AsyncAttrsfrom sqlalchemy.orm import Mapped, mapped_column, relationshipfrom letta.orm.block import Block@@ -26,7 +27,7 @@ if TYPE_CHECKING:from letta.orm.tool import Tool-class Agent(SqlalchemyBase, OrganizationMixin):+class Agent(SqlalchemyBase, OrganizationMixin, AsyncAttrs):__tablename__ = "agents"__pydantic_model__ = PydanticAgentState__table_args__ = (Index("ix_agents_created_at", "created_at", "id"),)@@ -200,3 +201,103 @@ class Agent(SqlalchemyBase, OrganizationMixin):state[field_name] = resolver()return self.__pydantic_model__(**state)++ async def to_pydantic_async(self, include_relationships: Optional[Set[str]] = None) -> PydanticAgentState:+ """+ Converts the SQLAlchemy Agent model into its Pydantic counterpart.++ The following base fields are always included:+ - id, agent_type, name, description, system, message_ids, metadata_,+ llm_config, embedding_config, project_id, template_id, base_template_id,+ tool_rules, message_buffer_autoclear, tags++ Everything else (e.g., tools, sources, memory, etc.) is optional and only+ included if specified in `include_fields`.++ Args:+ include_relationships (Optional[Set[str]]):+ A set of additional field names to include in the output. If None or empty,+ no extra fields are loaded beyond the base fields.++ Returns:+ PydanticAgentState: The Pydantic representation of the agent.+ """+ # Base fields: always included+ state = {+ "id": self.id,+ "agent_type": self.agent_type,+ "name": self.name,+ "description": self.description,+ "system": self.system,+ "message_ids": self.message_ids,+ "metadata": self.metadata_, # Exposed as 'metadata' to Pydantic+ "llm_config": self.llm_config,+ "embedding_config": self.embedding_config,+ "project_id": self.project_id,+ "template_id": self.template_id,+ "base_template_id": self.base_template_id,+ "tool_rules": self.tool_rules,+ "message_buffer_autoclear": self.message_buffer_autoclear,+ "created_by_id": self.created_by_id,+ "last_updated_by_id": self.last_updated_by_id,+ "created_at": self.created_at,+ "updated_at": self.updated_at,+ # optional field defaults+ "tags": [],+ "tools": [],+ "sources": [],+ "memory": Memory(blocks=[]),+ "identity_ids": [],+ "multi_agent_group": None,+ "tool_exec_environment_variables": [],+ "enable_sleeptime": None,+ "response_format": self.response_format,+ }+ optional_fields = {+ "tags": [],+ "tools": [],+ "sources": [],+ "memory": Memory(blocks=[]),+ "identity_ids": [],+ "multi_agent_group": None,+ "tool_exec_environment_variables": [],+ "enable_sleeptime": None,+ "response_format": self.response_format,+ }++ # Initialize include_relationships to an empty set if it's None+ include_relationships = set(optional_fields.keys() if include_relationships is None else include_relationships)++ # Only load requested relationships+ if "tags" in include_relationships:+ tags = await self.awaitable_attrs.tags+ state["tags"] = [t.tag for t in tags]++ if "tools" in include_relationships:+ state["tools"] = await self.awaitable_attrs.tools++ if "sources" in include_relationships:+ sources = await self.awaitable_attrs.sources+ state["sources"] = [s.to_pydantic() for s in sources]++ if "memory" in include_relationships:+ memory_blocks = await self.awaitable_attrs.core_memory+ state["memory"] = Memory(+ blocks=[b.to_pydantic() for b in memory_blocks],+ prompt_template=get_prompt_template_for_agent_type(self.agent_type),+ )++ if "identity_ids" in include_relationships:+ identities = await self.awaitable_attrs.identities+ state["identity_ids"] = [i.id for i in identities]++ if "multi_agent_group" in include_relationships:+ state["multi_agent_group"] = await self.awaitable_attrs.multi_agent_group++ if "tool_exec_environment_variables" in include_relationships:+ state["tool_exec_environment_variables"] = await self.awaitable_attrs.tool_exec_environment_variables++ if "enable_sleeptime" in include_relationships:+ state["enable_sleeptime"] = await self.awaitable_attrs.enable_sleeptime++ return self.__pydantic_model__(**state)diff --git a/letta/orm/block.py b/letta/orm/block.pyindex 30b2f1abb..271a9baa3 100644--- a/letta/orm/block.py+++ b/letta/orm/block.py@@ -39,6 +39,9 @@ class Block(OrganizationMixin, SqlalchemyBase):limit: Mapped[BigInteger] = mapped_column(Integer, default=CORE_MEMORY_BLOCK_CHAR_LIMIT, doc="Character limit of the block.")metadata_: Mapped[Optional[dict]] = mapped_column(JSON, default={}, doc="arbitrary information related to the block.")+ # permissions of the agent+ read_only: Mapped[bool] = mapped_column(doc="whether the agent has read-only access to the block", default=False)+# history pointers / locking mechanismscurrent_history_entry_id: Mapped[Optional[str]] = mapped_column(String, ForeignKey("block_history.id", name="fk_block_current_history_entry", use_alter=True), nullable=True, index=Truediff --git a/letta/orm/sqlalchemy_base.py b/letta/orm/sqlalchemy_base.pyindex dcb4cebf9..dda47c6cf 100644--- a/letta/orm/sqlalchemy_base.py+++ b/letta/orm/sqlalchemy_base.py@@ -114,154 +114,323 @@ class SqlalchemyBase(CommonSqlalchemyMetaMixins, Base):if before_obj and after_obj and before_obj.created_at < after_obj.created_at:raise ValueError("'before' reference must be later than 'after' reference")- query = select(cls)+ query = cls._list_preprocess(+ before_obj=before_obj,+ after_obj=after_obj,+ start_date=start_date,+ end_date=end_date,+ limit=limit,+ query_text=query_text,+ query_embedding=query_embedding,+ ascending=ascending,+ tags=tags,+ match_all_tags=match_all_tags,+ actor=actor,+ access=access,+ access_type=access_type,+ join_model=join_model,+ join_conditions=join_conditions,+ identifier_keys=identifier_keys,+ identity_id=identity_id,+ **kwargs,+ )++ # Execute the query+ results = session.execute(query)++ results = list(results.scalars())+ results = cls._list_postprocess(+ before=before,+ after=after,+ limit=limit,+ results=results,+ )- if join_model and join_conditions:- query = query.join(join_model, and_(*join_conditions))+ return results- # Apply access predicate if actor is provided- if actor:- query = cls.apply_access_predicate(query, actor, access, access_type)+ @classmethod+ @handle_db_timeout+ async def list_async(+ cls,+ *,+ db_session: "AsyncSession",+ before: Optional[str] = None,+ after: Optional[str] = None,+ start_date: Optional[datetime] = None,+ end_date: Optional[datetime] = None,+ limit: Optional[int] = 50,+ query_text: Optional[str] = None,+ query_embedding: Optional[List[float]] = None,+ ascending: bool = True,+ tags: Optional[List[str]] = None,+ match_all_tags: bool = False,+ actor: Optional["User"] = None,+ access: Optional[List[Literal["read", "write", "admin"]]] = ["read"],+ access_type: AccessType = AccessType.ORGANIZATION,+ join_model: Optional[Base] = None,+ join_conditions: Optional[Union[Tuple, List]] = None,+ identifier_keys: Optional[List[str]] = None,+ identity_id: Optional[str] = None,+ **kwargs,+ ) -> List["SqlalchemyBase"]:+ """+ Async version of list method above.+ NOTE: Keep in sync.+ List records with before/after pagination, ordering by created_at.+ Can use both before and after to fetch a window of records.- # Handle tag filtering if the model has tags- if tags and hasattr(cls, "tags"):- query = select(cls)-- if match_all_tags:- # Match ALL tags - use subqueries- subquery = (- select(cls.tags.property.mapper.class_.agent_id)- .where(cls.tags.property.mapper.class_.tag.in_(tags))- .group_by(cls.tags.property.mapper.class_.agent_id)- .having(func.count() == len(tags))- )- query = query.filter(cls.id.in_(subquery))- else:- # Match ANY tag - use join and filter- query = (- query.join(cls.tags).filter(cls.tags.property.mapper.class_.tag.in_(tags)).distinct(cls.id).order_by(cls.id)- ) # Deduplicate results+ Args:+ db_session: SQLAlchemy session+ before: ID of item to paginate before (upper bound)+ after: ID of item to paginate after (lower bound)+ start_date: Filter items after this date+ end_date: Filter items before this date+ limit: Maximum number of items to return+ query_text: Text to search for+ query_embedding: Vector to search for similar embeddings+ ascending: Sort direction+ tags: List of tags to filter by+ match_all_tags: If True, return items matching all tags. If False, match any tag.+ **kwargs: Additional filters to apply+ """+ if start_date and end_date and start_date > end_date:+ raise ValueError("start_date must be earlier than or equal to end_date")++ logger.debug(f"Listing {cls.__name__} with kwarg filters {kwargs}")++ async with db_session as session:+ # Get the reference objects for pagination+ before_obj = None+ after_obj = None- # select distinct primary key- query = query.distinct(cls.id).order_by(cls.id)+ if before:+ before_obj = await session.get(cls, before)+ if not before_obj:+ raise NoResultFound(f"No {cls.__name__} found with id {before}")- if identifier_keys and hasattr(cls, "identities"):- query = query.join(cls.identities).filter(cls.identities.property.mapper.class_.identifier_key.in_(identifier_keys))+ if after:+ after_obj = await session.get(cls, after)+ if not after_obj:+ raise NoResultFound(f"No {cls.__name__} found with id {after}")- # given the identity_id, we can find within the agents table any agents that have the identity_id in their identity_ids- if identity_id and hasattr(cls, "identities"):- query = query.join(cls.identities).filter(cls.identities.property.mapper.class_.id == identity_id)+ # Validate that before comes after the after object if both are provided+ if before_obj and after_obj and before_obj.created_at < after_obj.created_at:+ raise ValueError("'before' reference must be later than 'after' reference")- # Apply filtering logic from kwargs- for key, value in kwargs.items():- if "." in key:- # Handle joined table columns- table_name, column_name = key.split(".")- joined_table = locals().get(table_name) or globals().get(table_name)- column = getattr(joined_table, column_name)- else:- # Handle columns from main table- column = getattr(cls, key)-- if isinstance(value, (list, tuple, set)):- query = query.where(column.in_(value))- else:- query = query.where(column == value)-- # Date range filtering- if start_date:- query = query.filter(cls.created_at > start_date)- if end_date:- query = query.filter(cls.created_at < end_date)-- # Handle pagination based on before/after- if before or after:- conditions = []-- if before and after:- # Window-based query - get records between before and after- conditions = [- or_(cls.created_at < before_obj.created_at, and_(cls.created_at == before_obj.created_at, cls.id < before_obj.id)),- or_(cls.created_at > after_obj.created_at, and_(cls.created_at == after_obj.created_at, cls.id > after_obj.id)),- ]- else:- # Pure pagination query- if before:- conditions.append(- or_(- cls.created_at < before_obj.created_at,- and_(cls.created_at == before_obj.created_at, cls.id < before_obj.id),- )+ query = cls._list_preprocess(+ before_obj=before_obj,+ after_obj=after_obj,+ start_date=start_date,+ end_date=end_date,+ limit=limit,+ query_text=query_text,+ query_embedding=query_embedding,+ ascending=ascending,+ tags=tags,+ match_all_tags=match_all_tags,+ actor=actor,+ access=access,+ access_type=access_type,+ join_model=join_model,+ join_conditions=join_conditions,+ identifier_keys=identifier_keys,+ identity_id=identity_id,+ **kwargs,+ )++ # Execute the query+ results = await session.execute(query)++ results = list(results.scalars())+ results = cls._list_postprocess(+ before=before,+ after=after,+ limit=limit,+ results=results,+ )++ return results++ @classmethod+ def _list_preprocess(+ cls,+ *,+ before_obj,+ after_obj,+ start_date: Optional[datetime] = None,+ end_date: Optional[datetime] = None,+ limit: Optional[int] = 50,+ query_text: Optional[str] = None,+ query_embedding: Optional[List[float]] = None,+ ascending: bool = True,+ tags: Optional[List[str]] = None,+ match_all_tags: bool = False,+ actor: Optional["User"] = None,+ access: Optional[List[Literal["read", "write", "admin"]]] = ["read"],+ access_type: AccessType = AccessType.ORGANIZATION,+ join_model: Optional[Base] = None,+ join_conditions: Optional[Union[Tuple, List]] = None,+ identifier_keys: Optional[List[str]] = None,+ identity_id: Optional[str] = None,+ **kwargs,+ ):+ """+ Constructs the query for listing records.+ """+ query = select(cls)++ if join_model and join_conditions:+ query = query.join(join_model, and_(*join_conditions))++ # Apply access predicate if actor is provided+ if actor:+ query = cls.apply_access_predicate(query, actor, access, access_type)++ # Handle tag filtering if the model has tags+ if tags and hasattr(cls, "tags"):+ query = select(cls)++ if match_all_tags:+ # Match ALL tags - use subqueries+ subquery = (+ select(cls.tags.property.mapper.class_.agent_id)+ .where(cls.tags.property.mapper.class_.tag.in_(tags))+ .group_by(cls.tags.property.mapper.class_.agent_id)+ .having(func.count() == len(tags))+ )+ query = query.filter(cls.id.in_(subquery))+ else:+ # Match ANY tag - use join and filter+ query = (+ query.join(cls.tags).filter(cls.tags.property.mapper.class_.tag.in_(tags)).distinct(cls.id).order_by(cls.id)+ ) # Deduplicate results++ # select distinct primary key+ query = query.distinct(cls.id).order_by(cls.id)++ if identifier_keys and hasattr(cls, "identities"):+ query = query.join(cls.identities).filter(cls.identities.property.mapper.class_.identifier_key.in_(identifier_keys))++ # given the identity_id, we can find within the agents table any agents that have the identity_id in their identity_ids+ if identity_id and hasattr(cls, "identities"):+ query = query.join(cls.identities).filter(cls.identities.property.mapper.class_.id == identity_id)++ # Apply filtering logic from kwargs+ for key, value in kwargs.items():+ if "." in key:+ # Handle joined table columns+ table_name, column_name = key.split(".")+ joined_table = locals().get(table_name) or globals().get(table_name)+ column = getattr(joined_table, column_name)+ else:+ # Handle columns from main table+ column = getattr(cls, key)++ if isinstance(value, (list, tuple, set)):+ query = query.where(column.in_(value))+ else:+ query = query.where(column == value)++ # Date range filtering+ if start_date:+ query = query.filter(cls.created_at > start_date)+ if end_date:+ query = query.filter(cls.created_at < end_date)++ # Handle pagination based on before/after+ if before_obj or after_obj:+ conditions = []++ if before_obj and after_obj:+ # Window-based query - get records between before and after+ conditions = [+ or_(cls.created_at < before_obj.created_at, and_(cls.created_at == before_obj.created_at, cls.id < before_obj.id)),+ or_(cls.created_at > after_obj.created_at, and_(cls.created_at == after_obj.created_at, cls.id > after_obj.id)),+ ]+ else:+ # Pure pagination query+ if before_obj:+ conditions.append(+ or_(+ cls.created_at < before_obj.created_at,+ and_(cls.created_at == before_obj.created_at, cls.id < before_obj.id),)- if after:- conditions.append(- or_(- cls.created_at > after_obj.created_at,- and_(cls.created_at == after_obj.created_at, cls.id > after_obj.id),- )+ )+ if after_obj:+ conditions.append(+ or_(+ cls.created_at > after_obj.created_at,+ and_(cls.created_at == after_obj.created_at, cls.id > after_obj.id),)-- if conditions:- query = query.where(and_(*conditions))-- # Text search- if query_text:- if hasattr(cls, "text"):- query = query.filter(func.lower(cls.text).contains(func.lower(query_text)))- elif hasattr(cls, "name"):- # Special case for Agent model - search across name- query = query.filter(func.lower(cls.name).contains(func.lower(query_text)))-- # Embedding search (for Passages)- is_ordered = False- if query_embedding:- if not hasattr(cls, "embedding"):- raise ValueError(f"Class {cls.__name__} does not have an embedding column")-- from letta.settings import settings-- if settings.letta_pg_uri_no_default:- # PostgreSQL with pgvector- query = query.order_by(cls.embedding.cosine_distance(query_embedding).asc())- else:- # SQLite with custom vector type- query_embedding_binary = adapt_array(query_embedding)- query = query.order_by(- func.cosine_distance(cls.embedding, query_embedding_binary).asc(),- cls.created_at.asc() if ascending else cls.created_at.desc(),- cls.id.asc(),)- is_ordered = True- # Handle soft deletes- if hasattr(cls, "is_deleted"):- query = query.where(cls.is_deleted == False)+ if conditions:+ query = query.where(and_(*conditions))++ # Text search+ if query_text:+ if hasattr(cls, "text"):+ query = query.filter(func.lower(cls.text).contains(func.lower(query_text)))+ elif hasattr(cls, "name"):+ # Special case for Agent model - search across name+ query = query.filter(func.lower(cls.name).contains(func.lower(query_text)))- # Apply ordering- if not is_ordered:- if ascending:- query = query.order_by(cls.created_at.asc(), cls.id.asc())- else:- query = query.order_by(cls.created_at.desc(), cls.id.desc())-- # Apply limit, adjusting for both bounds if necessary- if before and after:- # When both bounds are provided, we need to fetch enough records to satisfy- # the limit while respecting both bounds. We'll fetch more and then trim.- query = query.limit(limit * 2)+ # Embedding search (for Passages)+ is_ordered = False+ if query_embedding:+ if not hasattr(cls, "embedding"):+ raise ValueError(f"Class {cls.__name__} does not have an embedding column")++ from letta.settings import settings++ if settings.letta_pg_uri_no_default:+ # PostgreSQL with pgvector+ query = query.order_by(cls.embedding.cosine_distance(query_embedding).asc())else:- query = query.limit(limit)+ # SQLite with custom vector type+ query_embedding_binary = adapt_array(query_embedding)+ query = query.order_by(+ func.cosine_distance(cls.embedding, query_embedding_binary).asc(),+ cls.created_at.asc() if ascending else cls.created_at.desc(),+ cls.id.asc(),+ )+ is_ordered = True++ # Handle soft deletes+ if hasattr(cls, "is_deleted"):+ query = query.where(cls.is_deleted == False)- results = list(session.execute(query).scalars())+ # Apply ordering+ if not is_ordered:+ if ascending:+ query = query.order_by(cls.created_at.asc(), cls.id.asc())+ else:+ query = query.order_by(cls.created_at.desc(), cls.id.desc())- # If we have both bounds, take the middle portion- if before and after and len(results) > limit:- middle = len(results) // 2- start = max(0, middle - limit // 2)- end = min(len(results), start + limit)- results = results[start:end]+ # Apply limit, adjusting for both bounds if necessary+ if before_obj and after_obj:+ # When both bounds are provided, we need to fetch enough records to satisfy+ # the limit while respecting both bounds. We'll fetch more and then trim.+ query = query.limit(limit * 2)+ else:+ query = query.limit(limit)+ return query- return results+ @classmethod+ def _list_postprocess(+ cls,+ before: str | None,+ after: str | None,+ limit: int | None,+ results: list,+ ):+ # If we have both bounds, take the middle portion+ if before and after and len(results) > limit:+ middle = len(results) // 2+ start = max(0, middle - limit // 2)+ end = min(len(results), start + limit)+ results = results[start:end]+ return results@classmethod@handle_db_timeout@@ -305,7 +474,7 @@ class SqlalchemyBase(CommonSqlalchemyMetaMixins, Base):@handle_db_timeoutasync def read_async(cls,- db_session: "Session",+ db_session: "AsyncSession",identifier: Optional[str] = None,actor: Optional["User"] = None,access: Optional[List[Literal["read", "write", "admin"]]] = ["read"],@@ -462,6 +631,24 @@ class SqlalchemyBase(CommonSqlalchemyMetaMixins, Base):except (DBAPIError, IntegrityError) as e:self._handle_dbapi_error(e)+ @handle_db_timeout+ async def create_async(self, db_session: "AsyncSession", actor: Optional["User"] = None, no_commit: bool = False) -> "SqlalchemyBase":+ """Async version of create function"""+ logger.debug(f"Creating {self.__class__.__name__} with ID: {self.id} with actor={actor}")++ if actor:+ self._set_created_and_updated_by_fields(actor.id)+ try:+ db_session.add(self)+ if no_commit:+ await db_session.flush() # no commit, just flush to get PK+ else:+ await db_session.commit()+ await db_session.refresh(self)+ return self+ except (DBAPIError, IntegrityError) as e:+ self._handle_dbapi_error(e)+@classmethod@handle_db_timeoutdef batch_create(cls, items: List["SqlalchemyBase"], db_session: "Session", actor: Optional["User"] = None) -> List["SqlalchemyBase"]:@@ -503,6 +690,51 @@ class SqlalchemyBase(CommonSqlalchemyMetaMixins, Base):except (DBAPIError, IntegrityError) as e:cls._handle_dbapi_error(e)+ @classmethod+ @handle_db_timeout+ async def batch_create_async(+ cls, items: List["SqlalchemyBase"], db_session: "AsyncSession", actor: Optional["User"] = None+ ) -> List["SqlalchemyBase"]:+ """+ Async version of batch_create method.+ Create multiple records in a single transaction for better performance.+ Args:+ items: List of model instances to create+ db_session: AsyncSession session+ actor: Optional user performing the action+ Returns:+ List of created model instances+ """+ logger.debug(f"Async batch creating {len(items)} {cls.__name__} items with actor={actor}")+ if not items:+ return []++ # Set created/updated by fields if actor is provided+ if actor:+ for item in items:+ item._set_created_and_updated_by_fields(actor.id)++ try:+ async with db_session as session:+ session.add_all(items)+ await session.flush() # Flush to generate IDs but don't commit yet++ # Collect IDs to fetch the complete objects after commit+ item_ids = [item.id for item in items]++ await session.commit()++ # Re-query the objects to get them with relationships loaded+ query = select(cls).where(cls.id.in_(item_ids))+ if hasattr(cls, "created_at"):+ query = query.order_by(cls.created_at)++ result = await session.execute(query)+ return list(result.scalars())++ except (DBAPIError, IntegrityError) as e:+ cls._handle_dbapi_error(e)+@handle_db_timeoutdef delete(self, db_session: "Session", actor: Optional["User"] = None) -> "SqlalchemyBase":logger.debug(f"Soft deleting {self.__class__.__name__} with ID: {self.id} with actor={actor}")diff --git a/letta/schemas/agent.py b/letta/schemas/agent.pyindex 13f74d826..cccd048fc 100644--- a/letta/schemas/agent.py+++ b/letta/schemas/agent.py@@ -312,9 +312,17 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):)return ("{% for block in blocks %}"- '<{{ block.label }} characters="{{ block.value|length }}/{{ block.limit }}">\n'+ "<{{ block.label }}>\n"+ "<description>\n"+ "{{ block.description }}\n"+ "</description>\n"+ "<metadata>\n"+ '{% if block.read_only %}read_only="true" {% endif %}chars_current="{{ block.value|length }}" chars_limit="{{ block.limit }}"\n'+ "</metadata>\n"+ "<value>\n""{{ block.value }}\n"- "</{{ block.label }}>"+ "</value>\n"+ "</{{ block.label }}>\n""{% if not loop.last %}\n{% endif %}""{% endfor %}")diff --git a/letta/schemas/block.py b/letta/schemas/block.pyindex 3e2fbb7e1..babcd8032 100644--- a/letta/schemas/block.py+++ b/letta/schemas/block.py@@ -25,6 +25,9 @@ class BaseBlock(LettaBase, validate_assignment=True):# context window labellabel: Optional[str] = Field(None, description="Label of the block (e.g. 'human', 'persona') in the context window.")+ # permissions of the agent+ read_only: bool = Field(False, description="Whether the agent has read-only access to the block.")+# metadatadescription: Optional[str] = Field(None, description="Description of the block.")metadata: Optional[dict] = Field({}, description="Metadata of the block.")diff --git a/letta/schemas/memory.py b/letta/schemas/memory.pyindex 1f60a09a1..e64533be6 100644--- a/letta/schemas/memory.py+++ b/letta/schemas/memory.py@@ -69,9 +69,14 @@ class Memory(BaseModel, validate_assignment=True):# Memory.template is a Jinja2 template for compiling memory module into a prompt string.prompt_template: str = Field(default="{% for block in blocks %}"- '<{{ block.label }} characters="{{ block.value|length }}/{{ block.limit }}">\n'+ "<{{ block.label }}>\n"+ "<metadata>"+ 'read_only="{{ block.read_only}}" chars_current="{{ block.value|length }}" chars_limit="{{ block.limit }}"'+ "</metadata>"+ "<value>""{{ block.value }}\n"- "</{{ block.label }}>"+ "</value>"+ "</{{ block.label }}>\n""{% if not loop.last %}\n{% endif %}""{% endfor %}",description="Jinja2 template for compiling memory blocks into a prompt string",diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex 302eed5ea..4f56be887 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -44,7 +44,7 @@ logger = get_logger(__name__)@router.get("/", response_model=List[AgentState], operation_id="list_agents")-def list_agents(+async def list_agents(name: Optional[str] = Query(None, description="Name of the agent"),tags: Optional[List[str]] = Query(None, description="List of tags to filter agents by"),match_all_tags: bool = Query(@@ -86,7 +86,7 @@ def list_agents(actor = server.user_manager.get_user_or_default(user_id=actor_id)# Call list_agents directly without unnecessary dict handling- return server.agent_manager.list_agents(+ return await server.agent_manager.list_agents_async(actor=actor,name=name,before=before,@@ -223,7 +223,7 @@ class CreateAgentRequest(CreateAgent):@router.post("/", response_model=AgentState, operation_id="create_agent")-def create_agent(+async def create_agent(agent: CreateAgentRequest = Body(...),server: "SyncServer" = Depends(get_letta_server),actor_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present@@ -234,14 +234,14 @@ def create_agent("""try:actor = server.user_manager.get_user_or_default(user_id=actor_id)- return server.create_agent(agent, actor=actor)+ return await server.create_agent_async(agent, actor=actor)except Exception as e:traceback.print_exc()raise HTTPException(status_code=500, detail=str(e))@router.patch("/{agent_id}", response_model=AgentState, operation_id="modify_agent")-def modify_agent(+async def modify_agent(agent_id: str,update_agent: UpdateAgent = Body(...),server: "SyncServer" = Depends(get_letta_server),@@ -249,7 +249,7 @@ def modify_agent():"""Update an existing agent"""actor = server.user_manager.get_user_or_default(user_id=actor_id)- return server.update_agent(agent_id=agent_id, request=update_agent, actor=actor)+ return await server.update_agent_async(agent_id=agent_id, request=update_agent, actor=actor)@router.get("/{agent_id}/tools", response_model=List[Tool], operation_id="list_agent_tools")@@ -632,8 +632,8 @@ async def send_message(# TODO: This is redundant, remove soonagent = server.agent_manager.get_agent_by_id(agent_id, actor)agent_eligible = not agent.enable_sleeptime and not agent.multi_agent_group and agent.agent_type != AgentType.sleeptime_agent- experimental_header = request_obj.headers.get("x-experimental")- feature_enabled = settings.use_experimental or experimental_header+ experimental_header = request_obj.headers.get("X-EXPERIMENTAL") or "false"+ feature_enabled = settings.use_experimental or experimental_header.lower() == "true"model_compatible = agent.llm_config.model_endpoint_type in ["anthropic", "openai", "google_vertex", "google_ai"]if agent_eligible and feature_enabled and model_compatible:@@ -646,7 +646,7 @@ async def send_message(actor=actor,)- result = await experimental_agent.step(request.messages, max_steps=10)+ result = await experimental_agent.step(request.messages, max_steps=10, use_assistant_message=request.use_assistant_message)else:result = await server.send_message_to_agent(agent_id=agent_id,@@ -690,11 +690,11 @@ async def send_message_streaming(# TODO: This is redundant, remove soonagent = server.agent_manager.get_agent_by_id(agent_id, actor)agent_eligible = not agent.enable_sleeptime and not agent.multi_agent_group and agent.agent_type != AgentType.sleeptime_agent- experimental_header = request_obj.headers.get("x-experimental")- feature_enabled = settings.use_experimental or experimental_header- model_compatible = agent.llm_config.model_endpoint_type in ["anthropic", "openai"]+ experimental_header = request_obj.headers.get("X-EXPERIMENTAL") or "false"+ feature_enabled = settings.use_experimental or experimental_header.lower() == "true"+ model_compatible = agent.llm_config.model_endpoint_type == "anthropic"- if agent_eligible and feature_enabled and model_compatible:+ if agent_eligible and feature_enabled and model_compatible and request.stream_tokens:experimental_agent = LettaAgent(agent_id=agent_id,message_manager=server.message_manager,diff --git a/letta/server/rest_api/routers/v1/messages.py b/letta/server/rest_api/routers/v1/messages.pyindex 95b3748f7..fe5e0f917 100644--- a/letta/server/rest_api/routers/v1/messages.py+++ b/letta/server/rest_api/routers/v1/messages.py@@ -63,7 +63,7 @@ async def create_messages_batch()try:- batch_job = server.job_manager.create_job(pydantic_job=batch_job, actor=actor)+ batch_job = await server.job_manager.create_job_async(pydantic_job=batch_job, actor=actor)# create the batch runnerbatch_runner = LettaAgentBatch(@@ -86,7 +86,7 @@ async def create_messages_batch(traceback.print_exc()# mark job as failed- server.job_manager.update_job_by_id(job_id=batch_job.id, job=BatchJob(status=JobStatus.failed), actor=actor)+ await server.job_manager.update_job_by_id_async(job_id=batch_job.id, job_update=JobUpdate(status=JobStatus.failed), actor=actor)raisereturn batch_job@@ -103,7 +103,7 @@ async def retrieve_batch_run(actor = server.user_manager.get_user_or_default(user_id=actor_id)try:- job = server.job_manager.get_job_by_id(job_id=batch_id, actor=actor)+ job = await server.job_manager.get_job_by_id_async(job_id=batch_id, actor=actor)return BatchJob.from_job(job)except NoResultFound:raise HTTPException(status_code=404, detail="Batch not found")@@ -154,7 +154,7 @@ async def list_batch_messages(# First, verify the batch job exists and the user has access to ittry:- job = server.job_manager.get_job_by_id(job_id=batch_id, actor=actor)+ job = await server.job_manager.get_job_by_id_async(job_id=batch_id, actor=actor)BatchJob.from_job(job)except NoResultFound:raise HTTPException(status_code=404, detail="Batch not found")@@ -180,8 +180,8 @@ async def cancel_batch_run(actor = server.user_manager.get_user_or_default(user_id=actor_id)try:- job = server.job_manager.get_job_by_id(job_id=batch_id, actor=actor)- job = server.job_manager.update_job_by_id(job_id=job.id, job_update=JobUpdate(status=JobStatus.cancelled), actor=actor)+ job = await server.job_manager.get_job_by_id_async(job_id=batch_id, actor=actor)+ job = await server.job_manager.update_job_by_id_async(job_id=job.id, job_update=JobUpdate(status=JobStatus.cancelled), actor=actor)# Get related llm batch jobsllm_batch_jobs = server.batch_manager.list_llm_batch_jobs(letta_batch_id=job.id, actor=actor)diff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.pyindex 1191ecdde..bd5dd80e1 100644--- a/letta/server/rest_api/routers/v1/tools.py+++ b/letta/server/rest_api/routers/v1/tools.py@@ -76,7 +76,7 @@ def retrieve_tool(@router.get("/", response_model=List[Tool], operation_id="list_tools")-def list_tools(+async def list_tools(after: Optional[str] = None,limit: Optional[int] = 50,name: Optional[str] = None,@@ -89,9 +89,9 @@ def list_tools(try:actor = server.user_manager.get_user_or_default(user_id=actor_id)if name is not None:- tool = server.tool_manager.get_tool_by_name(tool_name=name, actor=actor)+ tool = await server.tool_manager.get_tool_by_name_async(tool_name=name, actor=actor)return [tool] if tool else []- return server.tool_manager.list_tools(actor=actor, after=after, limit=limit)+ return await server.tool_manager.list_tools_async(actor=actor, after=after, limit=limit)except Exception as e:# Log or print the full exception here for debuggingprint(f"Error occurred: {e}")diff --git a/letta/server/server.py b/letta/server/server.pyindex 6b28e9889..80eed05d1 100644--- a/letta/server/server.py+++ b/letta/server/server.py@@ -794,6 +794,54 @@ class SyncServer(Server):return main_agent+ @trace_method+ async def create_agent_async(+ self,+ request: CreateAgent,+ actor: User,+ # interface+ interface: Union[AgentInterface, None] = None,+ ) -> AgentState:+ if request.llm_config is None:+ if request.model is None:+ raise ValueError("Must specify either model or llm_config in request")+ config_params = {+ "handle": request.model,+ "context_window_limit": request.context_window_limit,+ "max_tokens": request.max_tokens,+ "max_reasoning_tokens": request.max_reasoning_tokens,+ "enable_reasoner": request.enable_reasoner,+ }+ log_event(name="start get_cached_llm_config", attributes=config_params)+ request.llm_config = self.get_cached_llm_config(actor=actor, **config_params)+ log_event(name="end get_cached_llm_config", attributes=config_params)++ if request.embedding_config is None:+ if request.embedding is None:+ raise ValueError("Must specify either embedding or embedding_config in request")+ embedding_config_params = {+ "handle": request.embedding,+ "embedding_chunk_size": request.embedding_chunk_size or constants.DEFAULT_EMBEDDING_CHUNK_SIZE,+ }+ log_event(name="start get_cached_embedding_config", attributes=embedding_config_params)+ request.embedding_config = self.get_cached_embedding_config(actor=actor, **embedding_config_params)+ log_event(name="end get_cached_embedding_config", attributes=embedding_config_params)++ log_event(name="start create_agent db")+ main_agent = await self.agent_manager.create_agent_async(+ agent_create=request,+ actor=actor,+ )+ log_event(name="end create_agent db")++ if request.enable_sleeptime:+ if request.agent_type == AgentType.voice_convo_agent:+ main_agent = self.create_voice_sleeptime_agent(main_agent=main_agent, actor=actor)+ else:+ main_agent = self.create_sleeptime_agent(main_agent=main_agent, actor=actor)++ return main_agent+def update_agent(self,agent_id: str,@@ -820,6 +868,32 @@ class SyncServer(Server):actor=actor,)+ async def update_agent_async(+ self,+ agent_id: str,+ request: UpdateAgent,+ actor: User,+ ) -> AgentState:+ if request.model is not None:+ request.llm_config = self.get_llm_config_from_handle(handle=request.model, actor=actor)++ if request.embedding is not None:+ request.embedding_config = self.get_embedding_config_from_handle(handle=request.embedding, actor=actor)++ if request.enable_sleeptime:+ agent = self.agent_manager.get_agent_by_id(agent_id=agent_id, actor=actor)+ if agent.multi_agent_group is None:+ if agent.agent_type == AgentType.voice_convo_agent:+ self.create_voice_sleeptime_agent(main_agent=agent, actor=actor)+ else:+ self.create_sleeptime_agent(main_agent=agent, actor=actor)++ return await self.agent_manager.update_agent_async(+ agent_id=agent_id,+ agent_update=request,+ actor=actor,+ )+def create_sleeptime_agent(self, main_agent: AgentState, actor: User) -> AgentState:request = CreateAgent(name=main_agent.name + "-sleeptime",diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex 0ff701f40..b861cd493 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -62,6 +62,7 @@ from letta.services.helpers.agent_manager_helper import (_apply_filters,_apply_identity_filters,_apply_pagination,+ _apply_pagination_async,_apply_tag_filter,_process_relationship,check_supports_structured_output,@@ -122,7 +123,35 @@ class AgentManager:return name_to_id, id_to_name@staticmethod- @trace_method+ async def _resolve_tools_async(session, names: Set[str], ids: Set[str], org_id: str) -> Tuple[Dict[str, str], Dict[str, str]]:+ """+ Bulk‑fetch all ToolModel rows matching either name ∈ names or id ∈ ids+ (and scoped to this organization), and return two maps:+ name_to_id, id_to_name.+ Raises if any requested name or id was not found.+ """+ stmt = select(ToolModel.id, ToolModel.name).where(+ ToolModel.organization_id == org_id,+ or_(+ ToolModel.name.in_(names),+ ToolModel.id.in_(ids),+ ),+ )+ result = await session.execute(stmt)+ rows = result.fetchall() # Use fetchall()+ name_to_id = {row[1]: row[0] for row in rows} # row[1] is name, row[0] is id+ id_to_name = {row[0]: row[1] for row in rows} # row[0] is id, row[1] is name++ missing_names = names - set(name_to_id.keys())+ missing_ids = ids - set(id_to_name.keys())+ if missing_names:+ raise ValueError(f"Tools not found by name: {missing_names}")+ if missing_ids:+ raise ValueError(f"Tools not found by id: {missing_ids}")++ return name_to_id, id_to_name++ @staticmethoddef _bulk_insert_pivot(session, table, rows: list[dict]):if not rows:return@@ -146,7 +175,29 @@ class AgentManager:session.execute(stmt)@staticmethod- @trace_method+ async def _bulk_insert_pivot_async(session, table, rows: list[dict]):+ if not rows:+ return++ dialect = session.bind.dialect.name+ if dialect == "postgresql":+ stmt = pg_insert(table).values(rows).on_conflict_do_nothing()+ elif dialect == "sqlite":+ stmt = sa.insert(table).values(rows).prefix_with("OR IGNORE")+ else:+ # fallback: filter out exact-duplicate dicts in Python+ seen = set()+ filtered = []+ for row in rows:+ key = tuple(sorted(row.items()))+ if key not in seen:+ seen.add(key)+ filtered.append(row)+ stmt = sa.insert(table).values(filtered)++ await session.execute(stmt)++ @staticmethoddef _replace_pivot_rows(session, table, agent_id: str, rows: list[dict]):"""Replace all pivot rows for an agent with *exactly* the provided list.@@ -157,6 +208,17 @@ class AgentManager:if rows:AgentManager._bulk_insert_pivot(session, table, rows)+ @staticmethod+ async def _replace_pivot_rows_async(session, table, agent_id: str, rows: list[dict]):+ """+ Replace all pivot rows for an agent with *exactly* the provided list.+ Uses two bulk statements (DELETE + INSERT ... ON CONFLICT DO NOTHING).+ """+ # delete all existing rows for this agent+ await session.execute(delete(table).where(table.c.agent_id == agent_id))+ if rows:+ await AgentManager._bulk_insert_pivot_async(session, table, rows)+# ======================================================================================================================# Basic CRUD operations# ======================================================================================================================@@ -252,6 +314,7 @@ class AgentManager:session.flush()aid = new_agent.id+ # Note: These methods may need async versions if they perform database operationsself._bulk_insert_pivot(session,ToolsAgents.__table__,@@ -259,10 +322,8 @@ class AgentManager:)if block_ids:- rows = [- {"agent_id": aid, "block_id": bid, "block_label": lbl}- for bid, lbl in session.execute(select(BlockModel.id, BlockModel.label).where(BlockModel.id.in_(block_ids))).all()- ]+ result = session.execute(select(BlockModel.id, BlockModel.label).where(BlockModel.id.in_(block_ids)))+ rows = [{"agent_id": aid, "block_id": bid, "block_label": lbl} for bid, lbl in result.all()]self._bulk_insert_pivot(session, BlocksAgents.__table__, rows)self._bulk_insert_pivot(@@ -303,9 +364,162 @@ class AgentManager:session.refresh(new_agent)+ # Using the synchronous version since we don't have an async version yet+ # If you implement an async version of create_many_messages, you can switch to thatself.message_manager.create_many_messages(pydantic_msgs=init_messages, actor=actor)return new_agent.to_pydantic()+ @trace_method+ async def create_agent_async(+ self, agent_create: CreateAgent, actor: PydanticUser, _test_only_force_id: Optional[str] = None+ ) -> PydanticAgentState:+ # validate required configs+ if not agent_create.llm_config or not agent_create.embedding_config:+ raise ValueError("llm_config and embedding_config are required")++ # blocks+ block_ids = list(agent_create.block_ids or [])+ if agent_create.memory_blocks:+ pydantic_blocks = [PydanticBlock(**b.model_dump(to_orm=True)) for b in agent_create.memory_blocks]+ created_blocks = self.block_manager.batch_create_blocks(+ pydantic_blocks,+ actor=actor,+ )+ block_ids.extend([blk.id for blk in created_blocks])++ # tools+ tool_names = set(agent_create.tools or [])+ if agent_create.include_base_tools:+ if agent_create.agent_type == AgentType.voice_sleeptime_agent:+ tool_names |= set(BASE_VOICE_SLEEPTIME_TOOLS)+ elif agent_create.agent_type == AgentType.voice_convo_agent:+ tool_names |= set(BASE_VOICE_SLEEPTIME_CHAT_TOOLS)+ elif agent_create.agent_type == AgentType.sleeptime_agent:+ tool_names |= set(BASE_SLEEPTIME_TOOLS)+ elif agent_create.enable_sleeptime:+ tool_names |= set(BASE_SLEEPTIME_CHAT_TOOLS)+ else:+ tool_names |= set(BASE_TOOLS + BASE_MEMORY_TOOLS)+ if agent_create.include_multi_agent_tools:+ tool_names |= set(MULTI_AGENT_TOOLS)++ supplied_ids = set(agent_create.tool_ids or [])++ source_ids = agent_create.source_ids or []+ identity_ids = agent_create.identity_ids or []+ tag_values = agent_create.tags or []++ async with db_registry.async_session() as session:+ async with session.begin():+ # Note: This will need to be modified if _resolve_tools needs an async version+ name_to_id, id_to_name = await self._resolve_tools_async(+ session,+ tool_names,+ supplied_ids,+ actor.organization_id,+ )++ tool_ids = set(name_to_id.values()) | set(id_to_name.keys())+ tool_names = set(name_to_id.keys()) # now canonical++ tool_rules = list(agent_create.tool_rules or [])+ if agent_create.include_base_tool_rules:+ for tn in tool_names:+ if tn in {"send_message", "send_message_to_agent_async", "memory_finish_edits"}:+ tool_rules.append(TerminalToolRule(tool_name=tn))+ elif tn in (BASE_TOOLS + BASE_MEMORY_TOOLS + BASE_SLEEPTIME_TOOLS):+ tool_rules.append(ContinueToolRule(tool_name=tn))++ if tool_rules:+ check_supports_structured_output(model=agent_create.llm_config.model, tool_rules=tool_rules)++ new_agent = AgentModel(+ name=agent_create.name,+ system=derive_system_message(+ agent_type=agent_create.agent_type,+ enable_sleeptime=agent_create.enable_sleeptime,+ system=agent_create.system,+ ),+ agent_type=agent_create.agent_type,+ llm_config=agent_create.llm_config,+ embedding_config=agent_create.embedding_config,+ organization_id=actor.organization_id,+ description=agent_create.description,+ metadata_=agent_create.metadata,+ tool_rules=tool_rules,+ project_id=agent_create.project_id,+ template_id=agent_create.template_id,+ base_template_id=agent_create.base_template_id,+ message_buffer_autoclear=agent_create.message_buffer_autoclear,+ enable_sleeptime=agent_create.enable_sleeptime,+ response_format=agent_create.response_format,+ created_by_id=actor.id,+ last_updated_by_id=actor.id,+ )++ if _test_only_force_id:+ new_agent.id = _test_only_force_id++ session.add(new_agent)+ await session.flush()+ aid = new_agent.id++ # Note: These methods may need async versions if they perform database operations+ await self._bulk_insert_pivot_async(+ session,+ ToolsAgents.__table__,+ [{"agent_id": aid, "tool_id": tid} for tid in tool_ids],+ )++ if block_ids:+ result = await session.execute(select(BlockModel.id, BlockModel.label).where(BlockModel.id.in_(block_ids)))+ rows = [{"agent_id": aid, "block_id": bid, "block_label": lbl} for bid, lbl in result.all()]+ await self._bulk_insert_pivot_async(session, BlocksAgents.__table__, rows)++ await self._bulk_insert_pivot_async(+ session,+ SourcesAgents.__table__,+ [{"agent_id": aid, "source_id": sid} for sid in source_ids],+ )+ await self._bulk_insert_pivot_async(+ session,+ AgentsTags.__table__,+ [{"agent_id": aid, "tag": tag} for tag in tag_values],+ )+ await self._bulk_insert_pivot_async(+ session,+ IdentitiesAgents.__table__,+ [{"agent_id": aid, "identity_id": iid} for iid in identity_ids],+ )++ if agent_create.tool_exec_environment_variables:+ env_rows = [+ {+ "agent_id": aid,+ "key": key,+ "value": val,+ "organization_id": actor.organization_id,+ }+ for key, val in agent_create.tool_exec_environment_variables.items()+ ]+ await session.execute(insert(AgentEnvironmentVariable).values(env_rows))++ # initial message sequence+ agent_state = await new_agent.to_pydantic_async(include_relationships={"memory"})+ init_messages = self._generate_initial_message_sequence(+ actor,+ agent_state=agent_state,+ supplied_initial_message_sequence=agent_create.initial_message_sequence,+ )+ new_agent.message_ids = [msg.id for msg in init_messages]++ await session.refresh(new_agent)++ # Using the synchronous version since we don't have an async version yet+ # If you implement an async version of create_many_messages, you can switch to that+ await self.message_manager.create_many_messages_async(pydantic_msgs=init_messages, actor=actor)+ return await new_agent.to_pydantic_async()+@enforce_typesdef _generate_initial_message_sequence(self, actor: PydanticUser, agent_state: PydanticAgentState, supplied_initial_message_sequence: Optional[List[MessageCreate]] = None@@ -459,6 +673,123 @@ class AgentManager:return agent.to_pydantic()+ @enforce_types+ async def update_agent_async(+ self,+ agent_id: str,+ agent_update: UpdateAgent,+ actor: PydanticUser,+ ) -> PydanticAgentState:++ new_tools = set(agent_update.tool_ids or [])+ new_sources = set(agent_update.source_ids or [])+ new_blocks = set(agent_update.block_ids or [])+ new_idents = set(agent_update.identity_ids or [])+ new_tags = set(agent_update.tags or [])++ async with db_registry.async_session() as session, session.begin():++ agent: AgentModel = await AgentModel.read_async(db_session=session, identifier=agent_id, actor=actor)+ agent.updated_at = datetime.now(timezone.utc)+ agent.last_updated_by_id = actor.id++ scalar_updates = {+ "name": agent_update.name,+ "system": agent_update.system,+ "llm_config": agent_update.llm_config,+ "embedding_config": agent_update.embedding_config,+ "message_ids": agent_update.message_ids,+ "tool_rules": agent_update.tool_rules,+ "description": agent_update.description,+ "project_id": agent_update.project_id,+ "template_id": agent_update.template_id,+ "base_template_id": agent_update.base_template_id,+ "message_buffer_autoclear": agent_update.message_buffer_autoclear,+ "enable_sleeptime": agent_update.enable_sleeptime,+ "response_format": agent_update.response_format,+ }+ for col, val in scalar_updates.items():+ if val is not None:+ setattr(agent, col, val)++ if agent_update.metadata is not None:+ agent.metadata_ = agent_update.metadata++ aid = agent.id++ if agent_update.tool_ids is not None:+ await self._replace_pivot_rows_async(+ session,+ ToolsAgents.__table__,+ aid,+ [{"agent_id": aid, "tool_id": tid} for tid in new_tools],+ )+ session.expire(agent, ["tools"])++ if agent_update.source_ids is not None:+ await self._replace_pivot_rows_async(+ session,+ SourcesAgents.__table__,+ aid,+ [{"agent_id": aid, "source_id": sid} for sid in new_sources],+ )+ session.expire(agent, ["sources"])++ if agent_update.block_ids is not None:+ rows = []+ if new_blocks:+ result = await session.execute(select(BlockModel.id, BlockModel.label).where(BlockModel.id.in_(new_blocks)))+ label_map = {bid: lbl for bid, lbl in result.all()}+ rows = [{"agent_id": aid, "block_id": bid, "block_label": label_map[bid]} for bid in new_blocks]++ await self._replace_pivot_rows_async(session, BlocksAgents.__table__, aid, rows)+ session.expire(agent, ["core_memory"])++ if agent_update.identity_ids is not None:+ await self._replace_pivot_rows_async(+ session,+ IdentitiesAgents.__table__,+ aid,+ [{"agent_id": aid, "identity_id": iid} for iid in new_idents],+ )+ session.expire(agent, ["identities"])++ if agent_update.tags is not None:+ await self._replace_pivot_rows_async(+ session,+ AgentsTags.__table__,+ aid,+ [{"agent_id": aid, "tag": tag} for tag in new_tags],+ )+ session.expire(agent, ["tags"])++ if agent_update.tool_exec_environment_variables is not None:+ await session.execute(delete(AgentEnvironmentVariable).where(AgentEnvironmentVariable.agent_id == aid))+ env_rows = [+ {+ "agent_id": aid,+ "key": k,+ "value": v,+ "organization_id": agent.organization_id,+ }+ for k, v in agent_update.tool_exec_environment_variables.items()+ ]+ if env_rows:+ await self._bulk_insert_pivot_async(session, AgentEnvironmentVariable.__table__, env_rows)+ session.expire(agent, ["tool_exec_environment_variables"])++ if agent_update.enable_sleeptime and agent_update.system is None:+ agent.system = derive_system_message(+ agent_type=agent.agent_type,+ enable_sleeptime=agent_update.enable_sleeptime,+ system=agent.system,+ )++ await session.flush()+ await session.refresh(agent)++ return await agent.to_pydantic_async()+# TODO: Make this general and think about how to roll this into sqlalchemybasedef list_agents(self,@@ -514,9 +845,73 @@ class AgentManager:if limit:query = query.limit(limit)- agents = session.execute(query).scalars().all()+ result = session.execute(query)+ agents = result.scalars().all()return [agent.to_pydantic(include_relationships=include_relationships) for agent in agents]+ async def list_agents_async(+ self,+ actor: PydanticUser,+ name: Optional[str] = None,+ tags: Optional[List[str]] = None,+ match_all_tags: bool = False,+ before: Optional[str] = None,+ after: Optional[str] = None,+ limit: Optional[int] = 50,+ query_text: Optional[str] = None,+ project_id: Optional[str] = None,+ template_id: Optional[str] = None,+ base_template_id: Optional[str] = None,+ identity_id: Optional[str] = None,+ identifier_keys: Optional[List[str]] = None,+ include_relationships: Optional[List[str]] = None,+ ascending: bool = True,+ ) -> List[PydanticAgentState]:+ """+ Retrieves agents with optimized filtering and optional field selection.++ Args:+ actor: The User requesting the list+ name (Optional[str]): Filter by agent name.+ tags (Optional[List[str]]): Filter agents by tags.+ match_all_tags (bool): If True, only return agents that match ALL given tags.+ before (Optional[str]): Cursor for pagination.+ after (Optional[str]): Cursor for pagination.+ limit (Optional[int]): Maximum number of agents to return.+ query_text (Optional[str]): Search agents by name.+ project_id (Optional[str]): Filter by project ID.+ template_id (Optional[str]): Filter by template ID.+ base_template_id (Optional[str]): Filter by base template ID.+ identity_id (Optional[str]): Filter by identifier ID.+ identifier_keys (Optional[List[str]]): Search agents by identifier keys.+ include_relationships (Optional[List[str]]): List of fields to load for performance optimization.+ ascending++ Returns:+ List[PydanticAgentState]: The filtered list of matching agents.+ """+ async with db_registry.async_session() as session:+ query = select(AgentModel).distinct(AgentModel.created_at, AgentModel.id)+ query = AgentModel.apply_access_predicate(query, actor, ["read"], AccessType.ORGANIZATION)++ # Apply filters+ query = _apply_filters(query, name, query_text, project_id, template_id, base_template_id)+ query = _apply_identity_filters(query, identity_id, identifier_keys)+ query = _apply_tag_filter(query, tags, match_all_tags)+ query = await _apply_pagination_async(query, before, after, session, ascending=ascending)++ if limit:+ query = query.limit(limit)++ result = await session.execute(query)+ agents = result.scalars().all()+ pydantic_agents = []+ for agent in agents:+ pydantic_agent = await agent.to_pydantic_async(include_relationships=include_relationships)+ pydantic_agents.append(pydantic_agent)++ return pydantic_agents+@enforce_typesdef list_agents_matching_tags(self,@@ -577,6 +972,20 @@ class AgentManager:agent = AgentModel.read(db_session=session, identifier=agent_id, actor=actor)return agent.to_pydantic()+ @enforce_types+ async def get_agent_by_id_async(self, agent_id: str, actor: PydanticUser) -> PydanticAgentState:+ """Fetch an agent by its ID."""+ async with db_registry.async_session() as session:+ agent = await AgentModel.read_async(db_session=session, identifier=agent_id, actor=actor)+ return agent.to_pydantic()++ @enforce_types+ async def get_agents_by_ids_async(self, agent_ids: list[str], actor: PydanticUser) -> list[PydanticAgentState]:+ """Fetch a list of agents by their IDs."""+ async with db_registry.async_session() as session:+ agents = await AgentModel.read_multiple_async(db_session=session, identifiers=agent_ids, actor=actor)+ return [await agent.to_pydantic_async() for agent in agents]+@enforce_typesdef get_agent_by_name(self, agent_name: str, actor: PydanticUser) -> PydanticAgentState:"""Fetch an agent by its ID."""@@ -784,6 +1193,11 @@ class AgentManager:message_ids = self.get_agent_by_id(agent_id=agent_id, actor=actor).message_idsreturn self.message_manager.get_messages_by_ids(message_ids=message_ids, actor=actor)+ @enforce_types+ async def get_in_context_messages_async(self, agent_id: str, actor: PydanticUser) -> List[PydanticMessage]:+ message_ids = self.get_agent_by_id(agent_id=agent_id, actor=actor).message_ids+ return await self.message_manager.get_messages_by_ids_async(message_ids=message_ids, actor=actor)+@enforce_typesdef get_system_message(self, agent_id: str, actor: PydanticUser) -> PydanticMessage:message_ids = self.get_agent_by_id(agent_id=agent_id, actor=actor).message_idsdiff --git a/letta/services/block_manager.py b/letta/services/block_manager.pyindex 30450f018..0d4e67da2 100644--- a/letta/services/block_manager.py+++ b/letta/services/block_manager.py@@ -1,5 +1,6 @@from typing import Dict, List, Optional+from sqlalchemy import selectfrom sqlalchemy.orm import Sessionfrom letta.log import get_logger@@ -454,7 +455,7 @@ class BlockManager:return block.to_pydantic()@enforce_types- def bulk_update_block_values(+ async def bulk_update_block_values_async(self, updates: Dict[str, str], actor: PydanticUser, return_hydrated: bool = False) -> Optional[List[PydanticBlock]]:"""@@ -469,12 +470,13 @@ class BlockManager:the updated Block objects as Pydantic schemasRaises:- NoResultFound if any block_id doesn’t exist or isn’t visible to this actor- ValueError if any new value exceeds its block’s limit+ NoResultFound if any block_id doesn't exist or isn't visible to this actor+ ValueError if any new value exceeds its block's limit"""- with db_registry.session() as session:- q = session.query(BlockModel).filter(BlockModel.id.in_(updates.keys()), BlockModel.organization_id == actor.organization_id)- blocks = q.all()+ async with db_registry.async_session() as session:+ query = select(BlockModel).where(BlockModel.id.in_(updates.keys()), BlockModel.organization_id == actor.organization_id)+ result = await session.execute(query)+ blocks = result.scalars().all()found_ids = {b.id for b in blocks}missing = set(updates.keys()) - found_ids@@ -488,8 +490,10 @@ class BlockManager:new_val = new_val[: block.limit]block.value = new_val- session.commit()+ await session.commit()if return_hydrated:- return [b.to_pydantic() for b in blocks]+ # TODO: implement for async+ pass+return Nonediff --git a/letta/services/helpers/agent_manager_helper.py b/letta/services/helpers/agent_manager_helper.pyindex 236d14c2a..26ac09674 100644--- a/letta/services/helpers/agent_manager_helper.py+++ b/letta/services/helpers/agent_manager_helper.py@@ -402,6 +402,25 @@ def _apply_pagination(query, before: Optional[str], after: Optional[str], sessioreturn query+async def _apply_pagination_async(query, before: Optional[str], after: Optional[str], session, ascending: bool = True) -> any:+ if after:+ result = (await session.execute(select(AgentModel.created_at, AgentModel.id).where(AgentModel.id == after))).first()+ if result:+ after_created_at, after_id = result+ query = query.where(_cursor_filter(AgentModel.created_at, AgentModel.id, after_created_at, after_id, forward=ascending))++ if before:+ result = (await session.execute(select(AgentModel.created_at, AgentModel.id).where(AgentModel.id == before))).first()+ if result:+ before_created_at, before_id = result+ query = query.where(_cursor_filter(AgentModel.created_at, AgentModel.id, before_created_at, before_id, forward=not ascending))++ # Apply ordering+ order_fn = asc if ascending else desc+ query = query.order_by(order_fn(AgentModel.created_at), order_fn(AgentModel.id))+ return query++def _apply_tag_filter(query, tags: Optional[List[str]], match_all_tags: bool):"""Apply tag-based filtering to the agent query.diff --git a/letta/services/job_manager.py b/letta/services/job_manager.pyindex 74576d4d6..87f957c7a 100644--- a/letta/services/job_manager.py+++ b/letta/services/job_manager.py@@ -44,6 +44,19 @@ class JobManager:job.create(session, actor=actor) # Save job in the databasereturn job.to_pydantic()+ @enforce_types+ async def create_job_async(+ self, pydantic_job: Union[PydanticJob, PydanticRun, PydanticBatchJob], actor: PydanticUser+ ) -> Union[PydanticJob, PydanticRun, PydanticBatchJob]:+ """Create a new job based on the JobCreate schema."""+ async with db_registry.async_session() as session:+ # Associate the job with the user+ pydantic_job.user_id = actor.id+ job_data = pydantic_job.model_dump(to_orm=True)+ job = JobModel(**job_data)+ await job.create_async(session, actor=actor) # Save job in the database+ return job.to_pydantic()+@enforce_typesdef update_job_by_id(self, job_id: str, job_update: JobUpdate, actor: PydanticUser) -> PydanticJob:"""Update a job by its ID with the given JobUpdate object."""@@ -68,6 +81,30 @@ class JobManager:return job.to_pydantic()+ @enforce_types+ async def update_job_by_id_async(self, job_id: str, job_update: JobUpdate, actor: PydanticUser) -> PydanticJob:+ """Update a job by its ID with the given JobUpdate object asynchronously."""+ async with db_registry.async_session() as session:+ # Fetch the job by ID+ job = await self._verify_job_access_async(session=session, job_id=job_id, actor=actor, access=["write"])++ # Update job attributes with only the fields that were explicitly set+ update_data = job_update.model_dump(to_orm=True, exclude_unset=True, exclude_none=True)++ # Automatically update the completion timestamp if status is set to 'completed'+ for key, value in update_data.items():+ setattr(job, key, value)++ if update_data.get("status") == JobStatus.completed and not job.completed_at:+ job.completed_at = get_utc_time()+ if job.callback_url:+ await self._dispatch_callback_async(session, job)++ # Save the updated job to the database+ await job.update_async(db_session=session, actor=actor)++ return job.to_pydantic()+@enforce_typesdef get_job_by_id(self, job_id: str, actor: PydanticUser) -> PydanticJob:"""Fetch a job by its ID."""@@ -76,6 +113,14 @@ class JobManager:job = JobModel.read(db_session=session, identifier=job_id, actor=actor, access_type=AccessType.USER)return job.to_pydantic()+ @enforce_types+ async def get_job_by_id_async(self, job_id: str, actor: PydanticUser) -> PydanticJob:+ """Fetch a job by its ID asynchronously."""+ async with db_registry.async_session() as session:+ # Retrieve job by ID using the Job model's read method+ job = await JobModel.read_async(db_session=session, identifier=job_id, actor=actor, access_type=AccessType.USER)+ return job.to_pydantic()+@enforce_typesdef list_jobs(self,@@ -438,6 +483,35 @@ class JobManager:raise NoResultFound(f"Job with id {job_id} does not exist or user does not have access")return job+ async def _verify_job_access_async(+ self,+ session: Session,+ job_id: str,+ actor: PydanticUser,+ access: List[Literal["read", "write", "delete"]] = ["read"],+ ) -> JobModel:+ """+ Verify that a job exists and the user has the required access.++ Args:+ session: The database session+ job_id: The ID of the job to verify+ actor: The user making the request++ Returns:+ The job if it exists and the user has access++ Raises:+ NoResultFound: If the job does not exist or user does not have access+ """+ job_query = select(JobModel).where(JobModel.id == job_id)+ job_query = JobModel.apply_access_predicate(job_query, actor, access, AccessType.USER)+ result = await session.execute(job_query)+ job = result.scalar_one_or_none()+ if not job:+ raise NoResultFound(f"Job with id {job_id} does not exist or user does not have access")+ return job+def _get_run_request_config(self, run_id: str) -> LettaRequestConfig:"""Get the request config for a job.@@ -476,3 +550,28 @@ class JobManager:session.add(job)session.commit()++ async def _dispatch_callback_async(self, session, job: JobModel) -> None:+ """+ POST a standard JSON payload to job.callback_url+ and record timestamp + HTTP status asynchronously.+ """++ payload = {+ "job_id": job.id,+ "status": job.status,+ "completed_at": job.completed_at.isoformat(),+ }+ try:+ import httpx++ async with httpx.AsyncClient() as client:+ resp = await client.post(job.callback_url, json=payload, timeout=5.0)+ job.callback_sent_at = get_utc_time()+ job.callback_status_code = resp.status_code++ except Exception:+ return++ session.add(job)+ await session.commit()diff --git a/letta/services/llm_batch_manager.py b/letta/services/llm_batch_manager.pyindex 7d7b4b543..052e2bbe2 100644--- a/letta/services/llm_batch_manager.py+++ b/letta/services/llm_batch_manager.py@@ -2,7 +2,7 @@ import datetimefrom typing import Any, Dict, List, Optional, Tuplefrom anthropic.types.beta.messages import BetaMessageBatch, BetaMessageBatchIndividualResponse-from sqlalchemy import desc, func, tuple_+from sqlalchemy import desc, func, select, tuple_from letta.jobs.types import BatchPollingResult, ItemUpdateInfo, RequestStatusUpdateInfo, StepStatusUpdateInfofrom letta.log import get_logger@@ -26,7 +26,7 @@ class LLMBatchManager:"""Manager for handling both LLMBatchJob and LLMBatchItem operations."""@enforce_types- def create_llm_batch_job(+ async def create_llm_batch_job_async(self,llm_provider: ProviderType,create_batch_response: BetaMessageBatch,@@ -35,7 +35,7 @@ class LLMBatchManager:status: JobStatus = JobStatus.created,) -> PydanticLLMBatchJob:"""Create a new LLM batch job."""- with db_registry.session() as session:+ async with db_registry.async_session() as session:batch = LLMBatchJob(status=status,llm_provider=llm_provider,@@ -43,14 +43,14 @@ class LLMBatchManager:organization_id=actor.organization_id,letta_batch_job_id=letta_batch_job_id,)- batch.create(session, actor=actor)+ await batch.create_async(session, actor=actor)return batch.to_pydantic()@enforce_types- def get_llm_batch_job_by_id(self, llm_batch_id: str, actor: Optional[PydanticUser] = None) -> PydanticLLMBatchJob:+ async def get_llm_batch_job_by_id_async(self, llm_batch_id: str, actor: Optional[PydanticUser] = None) -> PydanticLLMBatchJob:"""Retrieve a single batch job by ID."""- with db_registry.session() as session:- batch = LLMBatchJob.read(db_session=session, identifier=llm_batch_id, actor=actor)+ async with db_registry.async_session() as session:+ batch = await LLMBatchJob.read_async(db_session=session, identifier=llm_batch_id, actor=actor)return batch.to_pydantic()@enforce_types@@ -197,16 +197,16 @@ class LLMBatchManager:return [message.to_pydantic() for message in results]@enforce_types- def list_running_llm_batches(self, actor: Optional[PydanticUser] = None) -> List[PydanticLLMBatchJob]:+ async def list_running_llm_batches_async(self, actor: Optional[PydanticUser] = None) -> List[PydanticLLMBatchJob]:"""Return all running LLM batch jobs, optionally filtered by actor's organization."""- with db_registry.session() as session:- query = session.query(LLMBatchJob).filter(LLMBatchJob.status == JobStatus.running)+ async with db_registry.async_session() as session:+ query = select(LLMBatchJob).where(LLMBatchJob.status == JobStatus.running)if actor is not None:- query = query.filter(LLMBatchJob.organization_id == actor.organization_id)+ query = query.where(LLMBatchJob.organization_id == actor.organization_id)- results = query.all()- return [batch.to_pydantic() for batch in results]+ results = await session.execute(query)+ return [batch.to_pydantic() for batch in results.scalars().all()]@enforce_typesdef create_llm_batch_item(@@ -234,7 +234,9 @@ class LLMBatchManager:return item.to_pydantic()@enforce_types- def create_llm_batch_items_bulk(self, llm_batch_items: List[PydanticLLMBatchItem], actor: PydanticUser) -> List[PydanticLLMBatchItem]:+ async def create_llm_batch_items_bulk_async(+ self, llm_batch_items: List[PydanticLLMBatchItem], actor: PydanticUser+ ) -> List[PydanticLLMBatchItem]:"""Create multiple batch items in bulk for better performance.@@ -245,7 +247,7 @@ class LLMBatchManager:Returns:List of created batch items as Pydantic models"""- with db_registry.session() as session:+ async with db_registry.async_session() as session:# Convert Pydantic models to ORM objectsorm_items = []for item in llm_batch_items:@@ -261,8 +263,7 @@ class LLMBatchManager:)orm_items.append(orm_item)- # Use the batch_create method to create all items at once- created_items = LLMBatchItem.batch_create(orm_items, session, actor=actor)+ created_items = await LLMBatchItem.batch_create_async(orm_items, session, actor=actor)# Convert back to Pydantic modelsreturn [item.to_pydantic() for item in created_items]@@ -300,7 +301,7 @@ class LLMBatchManager:return item.update(db_session=session, actor=actor).to_pydantic()@enforce_types- def list_llm_batch_items(+ async def list_llm_batch_items_async(self,llm_batch_id: str,limit: Optional[int] = None,@@ -321,29 +322,29 @@ class LLMBatchManager:The results are ordered by their id in ascending order."""- with db_registry.session() as session:- query = session.query(LLMBatchItem).filter(LLMBatchItem.llm_batch_id == llm_batch_id)+ async with db_registry.async_session() as session:+ query = select(LLMBatchItem).where(LLMBatchItem.llm_batch_id == llm_batch_id)if actor is not None:- query = query.filter(LLMBatchItem.organization_id == actor.organization_id)+ query = query.where(LLMBatchItem.organization_id == actor.organization_id)# Additional optional filtersif agent_id is not None:- query = query.filter(LLMBatchItem.agent_id == agent_id)+ query = query.where(LLMBatchItem.agent_id == agent_id)if request_status is not None:- query = query.filter(LLMBatchItem.request_status == request_status)+ query = query.where(LLMBatchItem.request_status == request_status)if step_status is not None:- query = query.filter(LLMBatchItem.step_status == step_status)+ query = query.where(LLMBatchItem.step_status == step_status)if after is not None:- query = query.filter(LLMBatchItem.id > after)+ query = query.where(LLMBatchItem.id > after)query = query.order_by(LLMBatchItem.id.asc())if limit is not None:query = query.limit(limit)- results = query.all()- return [item.to_pydantic() for item in results]+ results = await session.execute(query)+ return [item.to_pydantic() for item in results.scalars()]def bulk_update_llm_batch_items(self, llm_batch_id_agent_id_pairs: List[Tuple[str, str]], field_updates: List[Dict[str, Any]], strict: bool = Truediff --git a/letta/services/message_manager.py b/letta/services/message_manager.pyindex 9c50b3626..426743bfa 100644--- a/letta/services/message_manager.py+++ b/letta/services/message_manager.py@@ -36,15 +36,29 @@ class MessageManager:"""Fetch messages by ID and return them in the requested order."""with db_registry.session() as session:results = MessageModel.list(db_session=session, id=message_ids, organization_id=actor.organization_id, limit=len(message_ids))+ return self._get_messages_by_id_postprocess(results, message_ids)- if len(results) != len(message_ids):- logger.warning(- f"Expected {len(message_ids)} messages, but found {len(results)}. Missing ids={set(message_ids) - set([r.id for r in results])}"- )+ @enforce_types+ async def get_messages_by_ids_async(self, message_ids: List[str], actor: PydanticUser) -> List[PydanticMessage]:+ """Fetch messages by ID and return them in the requested order. Async version of above function."""+ async with db_registry.async_session() as session:+ results = await MessageModel.list_async(+ db_session=session, id=message_ids, organization_id=actor.organization_id, limit=len(message_ids)+ )+ return self._get_messages_by_id_postprocess(results, message_ids)- # Sort results directly based on message_ids- result_dict = {msg.id: msg.to_pydantic() for msg in results}- return list(filter(lambda x: x is not None, [result_dict.get(msg_id, None) for msg_id in message_ids]))+ def _get_messages_by_id_postprocess(+ self,+ results: List[MessageModel],+ message_ids: List[str],+ ) -> List[PydanticMessage]:+ if len(results) != len(message_ids):+ logger.warning(+ f"Expected {len(message_ids)} messages, but found {len(results)}. Missing ids={set(message_ids) - set([r.id for r in results])}"+ )+ # Sort results directly based on message_ids+ result_dict = {msg.id: msg.to_pydantic() for msg in results}+ return list(filter(lambda x: x is not None, [result_dict.get(msg_id, None) for msg_id in message_ids]))@enforce_typesdef create_message(self, pydantic_msg: PydanticMessage, actor: PydanticUser) -> PydanticMessage:@@ -57,11 +71,20 @@ class MessageManager:msg.create(session, actor=actor) # Persist to databasereturn msg.to_pydantic()+ def _create_many_preprocess(self, pydantic_msgs: List[PydanticMessage], actor: PydanticUser) -> List[MessageModel]:+ # Create ORM model instances for all messages+ orm_messages = []+ for pydantic_msg in pydantic_msgs:+ # Set the organization id of the Pydantic message+ pydantic_msg.organization_id = actor.organization_id+ msg_data = pydantic_msg.model_dump(to_orm=True)+ orm_messages.append(MessageModel(**msg_data))+ return orm_messages+@enforce_typesdef create_many_messages(self, pydantic_msgs: List[PydanticMessage], actor: PydanticUser) -> List[PydanticMessage]:"""Create multiple messages in a single database transaction.-Args:pydantic_msgs: List of Pydantic message models to createactor: User performing the action@@ -69,23 +92,32 @@ class MessageManager:Returns:List of created Pydantic message models"""-if not pydantic_msgs:return []- # Create ORM model instances for all messages- orm_messages = []- for pydantic_msg in pydantic_msgs:- # Set the organization id of the Pydantic message- pydantic_msg.organization_id = actor.organization_id- msg_data = pydantic_msg.model_dump(to_orm=True)- orm_messages.append(MessageModel(**msg_data))-- # Use the batch_create method for efficient creation+ orm_messages = self._create_many_preprocess(pydantic_msgs, actor)with db_registry.session() as session:created_messages = MessageModel.batch_create(orm_messages, session, actor=actor)+ return [msg.to_pydantic() for msg in created_messages]++ @enforce_types+ async def create_many_messages_async(self, pydantic_msgs: List[PydanticMessage], actor: PydanticUser) -> List[PydanticMessage]:+ """+ Create multiple messages in a single database transaction asynchronously.++ Args:+ pydantic_msgs: List of Pydantic message models to create+ actor: User performing the action++ Returns:+ List of created Pydantic message models+ """+ if not pydantic_msgs:+ return []- # Convert back to Pydantic models+ orm_messages = self._create_many_preprocess(pydantic_msgs, actor)+ async with db_registry.async_session() as session:+ created_messages = await MessageModel.batch_create_async(orm_messages, session, actor=actor)return [msg.to_pydantic() for msg in created_messages]@enforce_typesdiff --git a/letta/services/tool_executor/tool_executor.py b/letta/services/tool_executor/tool_executor.pyindex 50879e570..9424520cc 100644--- a/letta/services/tool_executor/tool_executor.py+++ b/letta/services/tool_executor/tool_executor.py@@ -3,7 +3,12 @@ import tracebackfrom abc import ABC, abstractmethodfrom typing import Any, Dict, Optional-from letta.constants import COMPOSIO_ENTITY_ENV_VAR_KEY, CORE_MEMORY_LINE_NUMBER_WARNING, RETRIEVAL_QUERY_DEFAULT_PAGE_SIZE+from letta.constants import (+ COMPOSIO_ENTITY_ENV_VAR_KEY,+ CORE_MEMORY_LINE_NUMBER_WARNING,+ READ_ONLY_BLOCK_EDIT_ERROR,+ RETRIEVAL_QUERY_DEFAULT_PAGE_SIZE,+)from letta.functions.ast_parsers import coerce_dict_args_by_annotations, get_function_annotations_from_sourcefrom letta.functions.composio_helpers import execute_composio_action_async, generate_composio_action_from_func_namefrom letta.helpers.composio_helpers import get_composio_api_key@@ -203,6 +208,8 @@ class LettaCoreToolExecutor(ToolExecutor):Returns:Optional[str]: None is always returned as this function does not produce a response."""+ if agent_state.memory.get_block(label).read_only:+ raise ValueError(f"{READ_ONLY_BLOCK_EDIT_ERROR}")current_value = str(agent_state.memory.get_block(label).value)new_value = current_value + "\n" + str(content)agent_state.memory.update_block_value(label=label, value=new_value)@@ -228,6 +235,8 @@ class LettaCoreToolExecutor(ToolExecutor):Returns:Optional[str]: None is always returned as this function does not produce a response."""+ if agent_state.memory.get_block(label).read_only:+ raise ValueError(f"{READ_ONLY_BLOCK_EDIT_ERROR}")current_value = str(agent_state.memory.get_block(label).value)if old_content not in current_value:raise ValueError(f"Old content '{old_content}' not found in memory block '{label}'")@@ -260,6 +269,9 @@ class LettaCoreToolExecutor(ToolExecutor):"""import re+ if agent_state.memory.get_block(label).read_only:+ raise ValueError(f"{READ_ONLY_BLOCK_EDIT_ERROR}")+if bool(re.search(r"\nLine \d+: ", old_str)):raise ValueError("old_str contains a line number prefix, which is not allowed. "@@ -349,6 +361,9 @@ class LettaCoreToolExecutor(ToolExecutor):"""import re+ if agent_state.memory.get_block(label).read_only:+ raise ValueError(f"{READ_ONLY_BLOCK_EDIT_ERROR}")+if bool(re.search(r"\nLine \d+: ", new_str)):raise ValueError("new_str contains a line number prefix, which is not allowed. Do not "@@ -426,6 +441,9 @@ class LettaCoreToolExecutor(ToolExecutor):"""import re+ if agent_state.memory.get_block(label).read_only:+ raise ValueError(f"{READ_ONLY_BLOCK_EDIT_ERROR}")+if bool(re.search(r"\nLine \d+: ", new_memory)):raise ValueError("new_memory contains a line number prefix, which is not allowed. Do not "diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.pyindex 5b0cff898..eebff5ea7 100644--- a/letta/services/tool_manager.py+++ b/letta/services/tool_manager.py@@ -115,6 +115,16 @@ class ToolManager:except NoResultFound:return None+ @enforce_types+ async def get_tool_by_name_async(self, tool_name: str, actor: PydanticUser) -> Optional[PydanticTool]:+ """Retrieve a tool by its name and a user. We derive the organization from the user, and retrieve that tool."""+ try:+ async with db_registry.async_session() as session:+ tool = await ToolModel.read_async(db_session=session, name=tool_name, actor=actor)+ return tool.to_pydantic()+ except NoResultFound:+ return None+@enforce_typesdef get_tool_id_by_name(self, tool_name: str, actor: PydanticUser) -> Optional[str]:"""Retrieve a tool by its name and a user. We derive the organization from the user, and retrieve that tool."""@@ -126,10 +136,10 @@ class ToolManager:return None@enforce_types- def list_tools(self, actor: PydanticUser, after: Optional[str] = None, limit: Optional[int] = 50) -> List[PydanticTool]:+ async def list_tools_async(self, actor: PydanticUser, after: Optional[str] = None, limit: Optional[int] = 50) -> List[PydanticTool]:"""List all tools with optional pagination."""- with db_registry.session() as session:- tools = ToolModel.list(+ async with db_registry.async_session() as session:+ tools = await ToolModel.list_async(db_session=session,after=after,limit=limit,diff --git a/letta/types/__init__.py b/letta/types/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/poetry.lock b/poetry.lockindex 2df139691..6d001a9c8 100644--- a/poetry.lock+++ b/poetry.lock@@ -3241,14 +3241,14 @@ pytest = ["pytest (>=7.0.0)", "rich (>=13.9.4,<14.0.0)"][[package]]name = "letta-client"-version = "0.1.141"+version = "0.1.143"description = ""optional = falsepython-versions = "<4.0,>=3.8"groups = ["main"]files = [- {file = "letta_client-0.1.141-py3-none-any.whl", hash = "sha256:c37a5d74f0e45267a43b95f32e5170309c9887049944726e46b17329e386ea5e"},- {file = "letta_client-0.1.141.tar.gz", hash = "sha256:acdf59965ee54ac36739399d4c19ab1b92c8f3f9a1d953deaf775877f325b036"},+ {file = "letta_client-0.1.143-py3-none-any.whl", hash = "sha256:814651c142063191726b4933d6ff10039f84e7a90dbe196e00ae9c997c40b9b1"},+ {file = "letta_client-0.1.143.tar.gz", hash = "sha256:bdff6a752145e1e2acc629c552afc433a20263f8723d8b1074d24b877cf71451"},][package.dependencies]@@ -5533,19 +5533,19 @@ dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments[[package]]name = "pytest-asyncio"-version = "0.23.8"+version = "0.24.0"description = "Pytest support for asyncio"optional = truepython-versions = ">=3.8"groups = ["main"]markers = "extra == \"dev\" or extra == \"all\""files = [- {file = "pytest_asyncio-0.23.8-py3-none-any.whl", hash = "sha256:50265d892689a5faefb84df80819d1ecef566eb3549cf915dfb33569359d1ce2"},- {file = "pytest_asyncio-0.23.8.tar.gz", hash = "sha256:759b10b33a6dc61cce40a8bd5205e302978bbbcc00e279a8b61d9a6a3c82e4d3"},+ {file = "pytest_asyncio-0.24.0-py3-none-any.whl", hash = "sha256:a811296ed596b69bf0b6f3dc40f83bcaf341b155a269052d82efa2b25ac7037b"},+ {file = "pytest_asyncio-0.24.0.tar.gz", hash = "sha256:d081d828e576d85f875399194281e92bf8a68d60d72d1a2faf2feddb6c46b276"},][package.dependencies]-pytest = ">=7.0.0,<9"+pytest = ">=8.2,<9"[package.extras]docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1.0)"]@@ -7570,4 +7570,4 @@ tests = ["wikipedia"][metadata]lock-version = "2.1"python-versions = "<3.14,>=3.10"-content-hash = "862dc5a31d4385e89dc9a751cd171a611da3102c6832447a5f61926b25f03e06"+content-hash = "19eee9b3cd3d270cb748183bc332dd69706bb0bd3150c62e73e61ed437a40c78"diff --git a/pyproject.toml b/pyproject.tomlindex 6e29dc644..046a2bd02 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -1,6 +1,6 @@[tool.poetry]name = "letta"-version = "0.7.15"+version = "0.7.16"packages = [{include = "letta"},]@@ -47,7 +47,7 @@ autoflake = {version = "^2.3.0", optional = true}python-multipart = "^0.0.19"sqlalchemy-utils = "^0.41.2"pytest-order = {version = "^1.2.0", optional = true}-pytest-asyncio = {version = "^0.23.2", optional = true}+pytest-asyncio = {version = "^0.24.0", optional = true}pydantic-settings = "^2.2.1"httpx-sse = "^0.4.0"isort = { version = "^5.13.2", optional = true }@@ -73,7 +73,7 @@ llama-index = "^0.12.2"llama-index-embeddings-openai = "^0.3.1"e2b-code-interpreter = {version = "^1.0.3", optional = true}anthropic = "^0.49.0"-letta_client = "^0.1.141"+letta_client = "^0.1.143"openai = "^1.60.0"opentelemetry-api = "1.30.0"opentelemetry-sdk = "1.30.0"diff --git a/tests/integration_test_batch_api_cron_jobs.py b/tests/integration_test_batch_api_cron_jobs.pyindex 39306568f..4479b0dd1 100644--- a/tests/integration_test_batch_api_cron_jobs.py+++ b/tests/integration_test_batch_api_cron_jobs.py@@ -174,16 +174,16 @@ def create_test_agent(name, actor, test_id: Optional[str] = None, model="anthropreturn agent_manager.create_agent(agent_create=agent_create, actor=actor, _test_only_force_id=test_id)-def create_test_letta_batch_job(server, default_user):+async def create_test_letta_batch_job_async(server, default_user):"""Create a test batch job with the given batch response."""- return server.job_manager.create_job(BatchJob(user_id=default_user.id), actor=default_user)+ return await server.job_manager.create_job_async(BatchJob(user_id=default_user.id), actor=default_user)-def create_test_llm_batch_job(server, batch_response, default_user):+async def create_test_llm_batch_job_async(server, batch_response, default_user):"""Create a test batch job with the given batch response."""- letta_batch_job = create_test_letta_batch_job(server, default_user)+ letta_batch_job = await create_test_letta_batch_job_async(server, default_user)- return server.batch_manager.create_llm_batch_job(+ return await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=batch_response,actor=default_user,@@ -262,7 +262,7 @@ def mock_anthropic_client(server, batch_a_resp, batch_b_resp, agent_b_id, agent_# -----------------------------# End-to-End Test# -----------------------------[email protected](loop_scope="session")async def test_polling_simple_real_batch(client, default_user, server):# --- Step 1: Prepare test data ---# Create batch responses with different statuses@@ -276,7 +276,7 @@ async def test_polling_simple_real_batch(client, default_user, server):agent_c = create_test_agent("agent_c", default_user, test_id="agent-6156f470-a09d-4d51-aa62-7114e0971d56")# --- Step 2: Create batch jobs ---- job_a = create_test_llm_batch_job(server, batch_a_resp, default_user)+ job_a = await create_test_llm_batch_job_async(server, batch_a_resp, default_user)# --- Step 3: Create batch items ---item_a = create_test_batch_item(server, job_a.id, agent_a.id, default_user)@@ -293,7 +293,7 @@ async def test_polling_simple_real_batch(client, default_user, server):await poll_running_llm_batches(server)# --- Step 5: Verify batch job status updates ---- updated_job_a = server.batch_manager.get_llm_batch_job_by_id(llm_batch_id=job_a.id, actor=default_user)+ updated_job_a = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_id=job_a.id, actor=default_user)assert updated_job_a.status == JobStatus.completed@@ -403,7 +403,7 @@ async def test_polling_simple_real_batch(client, default_user, server):)[email protected](loop_scope="session")async def test_polling_mixed_batch_jobs(client, default_user, server):"""End-to-end test for polling batch jobs with mixed statuses and idempotency.@@ -433,8 +433,8 @@ async def test_polling_mixed_batch_jobs(client, default_user, server):agent_c = create_test_agent("agent_c", default_user)# --- Step 2: Create batch jobs ---- job_a = create_test_llm_batch_job(server, batch_a_resp, default_user)- job_b = create_test_llm_batch_job(server, batch_b_resp, default_user)+ job_a = await create_test_llm_batch_job_async(server, batch_a_resp, default_user)+ job_b = await create_test_llm_batch_job_async(server, batch_b_resp, default_user)# --- Step 3: Create batch items ---item_a = create_test_batch_item(server, job_a.id, agent_a.id, default_user)@@ -449,8 +449,8 @@ async def test_polling_mixed_batch_jobs(client, default_user, server):await poll_running_llm_batches(server)# --- Step 6: Verify batch job status updates ---- updated_job_a = server.batch_manager.get_llm_batch_job_by_id(llm_batch_id=job_a.id, actor=default_user)- updated_job_b = server.batch_manager.get_llm_batch_job_by_id(llm_batch_id=job_b.id, actor=default_user)+ updated_job_a = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_id=job_a.id, actor=default_user)+ updated_job_b = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_id=job_b.id, actor=default_user)# Job A should remain running since its processing_status is "in_progress"assert updated_job_a.status == JobStatus.running@@ -498,8 +498,8 @@ async def test_polling_mixed_batch_jobs(client, default_user, server):# --- Step 9: Verify that nothing changed for completed jobs ---# Refresh all objects- final_job_a = server.batch_manager.get_llm_batch_job_by_id(llm_batch_id=job_a.id, actor=default_user)- final_job_b = server.batch_manager.get_llm_batch_job_by_id(llm_batch_id=job_b.id, actor=default_user)+ final_job_a = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_id=job_a.id, actor=default_user)+ final_job_b = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_id=job_b.id, actor=default_user)final_item_a = server.batch_manager.get_llm_batch_item_by_id(item_a.id, actor=default_user)final_item_b = server.batch_manager.get_llm_batch_item_by_id(item_b.id, actor=default_user)final_item_c = server.batch_manager.get_llm_batch_item_by_id(item_c.id, actor=default_user)diff --git a/tests/integration_test_voice_agent.py b/tests/integration_test_voice_agent.pyindex a9c4a711e..f928baf59 100644--- a/tests/integration_test_voice_agent.py+++ b/tests/integration_test_voice_agent.py@@ -268,7 +268,7 @@ def _assert_valid_chunk(chunk, idx, chunks):# --- Tests --- #[email protected](loop_scope="session")@pytest.mark.parametrize("model", ["openai/gpt-4o-mini", "anthropic/claude-3-5-sonnet-20241022"])async def test_model_compatibility(disable_e2b_api_key, client, model, server, group_id, actor):request = _get_chat_request("How are you?")@@ -303,7 +303,7 @@ async def test_model_compatibility(disable_e2b_api_key, client, model, server, gprint(chunk.choices[0].delta.content)[email protected](loop_scope="session")@pytest.mark.parametrize("message", ["Use search memory tool to recall what my name is."])@pytest.mark.parametrize("endpoint", ["v1/voice-beta"])async def test_voice_recall_memory(disable_e2b_api_key, client, voice_agent, message, endpoint):@@ -318,7 +318,7 @@ async def test_voice_recall_memory(disable_e2b_api_key, client, voice_agent, mesprint(chunk.choices[0].delta.content)[email protected](loop_scope="session")@pytest.mark.parametrize("endpoint", ["v1/voice-beta"])async def test_trigger_summarization(disable_e2b_api_key, client, server, voice_agent, group_id, endpoint, actor):server.group_manager.modify_group(@@ -350,7 +350,7 @@ async def test_trigger_summarization(disable_e2b_api_key, client, server, voice_print(chunk.choices[0].delta.content)[email protected](loop_scope="session")async def test_summarization(disable_e2b_api_key, voice_agent):agent_manager = AgentManager()user_manager = UserManager()@@ -422,16 +422,17 @@ async def test_summarization(disable_e2b_api_key, voice_agent):summarizer.fire_and_forget.assert_called_once()[email protected](loop_scope="session")async def test_voice_sleeptime_agent(disable_e2b_api_key, client, voice_agent):"""Tests chat completion streaming using the Async OpenAI client."""agent_manager = AgentManager()+ tool_manager = ToolManager()user_manager = UserManager()actor = user_manager.get_default_user()- finish_rethinking_memory_tool = client.tools.list(name="finish_rethinking_memory")[0]- store_memories_tool = client.tools.list(name="store_memories")[0]- rethink_user_memory_tool = client.tools.list(name="rethink_user_memory")[0]+ finish_rethinking_memory_tool = tool_manager.get_tool_by_name(tool_name="finish_rethinking_memory", actor=actor)+ store_memories_tool = tool_manager.get_tool_by_name(tool_name="store_memories", actor=actor)+ rethink_user_memory_tool = tool_manager.get_tool_by_name(tool_name="rethink_user_memory", actor=actor)request = CreateAgent(name=voice_agent.name + "-sleeptime",agent_type=AgentType.voice_sleeptime_agent,@@ -487,7 +488,7 @@ async def test_voice_sleeptime_agent(disable_e2b_api_key, client, voice_agent):assert not missing, f"Did not see calls to: {', '.join(missing)}"[email protected](loop_scope="session")async def test_init_voice_convo_agent(voice_agent, server, actor):assert voice_agent.enable_sleeptime == Truediff --git a/tests/test_letta_agent_batch.py b/tests/test_letta_agent_batch.pyindex ee668fd03..11da3a197 100644--- a/tests/test_letta_agent_batch.py+++ b/tests/test_letta_agent_batch.py@@ -368,7 +368,7 @@ class MockAsyncIterable:# --------------------------------------------------------------------------- #[email protected](loop_scope="session")async def test_rethink_tool_modify_agent_state(client, disable_e2b_api_key, server, default_user, batch_job, rethink_tool):target_block_label = "human"new_memory = "banana"@@ -450,7 +450,7 @@ async def test_rethink_tool_modify_agent_state(client, disable_e2b_api_key, servassert block.value == new_memory[email protected](loop_scope="session")async def test_partial_error_from_anthropic_batch(disable_e2b_api_key, server, default_user, agents: Tuple[AgentState], batch_requests, step_state_map, batch_job):@@ -509,13 +509,13 @@ async def test_partial_error_from_anthropic_batch(new_batch_responses = await poll_running_llm_batches(server)# Verify database records were updated correctly- llm_batch_job = server.batch_manager.get_llm_batch_job_by_id(llm_batch_job.id, actor=default_user)+ llm_batch_job = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_job.id, actor=default_user)# Verify job propertiesassert llm_batch_job.status == JobStatus.completed, "Job status should be 'completed'"# Verify batch items- items = server.batch_manager.list_llm_batch_items(llm_batch_id=llm_batch_job.id, actor=default_user)+ items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=llm_batch_job.id, actor=default_user)assert len(items) == 3, f"Expected 3 batch items, got {len(items)}"# Verify only one new batch response@@ -533,7 +533,7 @@ async def test_partial_error_from_anthropic_batch(assert post_resume_response.agent_count == 2# New batch‑items should exist, initialised in (created, paused) state- new_items = server.batch_manager.list_llm_batch_items(+ new_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=post_resume_response.last_llm_batch_id, actor=default_user)assert len(new_items) == 2, f"Expected 2 new batch item, got {len(new_items)}"@@ -554,7 +554,7 @@ async def test_partial_error_from_anthropic_batch(# Old items must have been flipped to completed / finished earlier# (sanity – we already asserted this above, but we keep it close for clarity)- old_items = server.batch_manager.list_llm_batch_items(+ old_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=pre_resume_response.last_llm_batch_id, actor=default_user)for item in old_items:@@ -594,7 +594,7 @@ async def test_partial_error_from_anthropic_batch(letta_batch_job_id=pre_resume_response.letta_batch_id, limit=200, actor=default_user)assert len(messages) == (len(agents) - 1) * 4 + 1- assert_descending_order(messages)+ _assert_descending_order(messages)# Check that each agent is representedfor agent in agents_continue:agent_messages = [m for m in messages if m.agent_id == agent.id]@@ -610,7 +610,7 @@ async def test_partial_error_from_anthropic_batch(assert agent_messages[0].role == MessageRole.user, "Expected initial user message"[email protected](loop_scope="session")async def test_resume_step_some_stop(disable_e2b_api_key, server, default_user, agents: Tuple[AgentState], batch_requests, step_state_map, batch_job):@@ -671,13 +671,13 @@ async def test_resume_step_some_stop(new_batch_responses = await poll_running_llm_batches(server)# Verify database records were updated correctly- llm_batch_job = server.batch_manager.get_llm_batch_job_by_id(llm_batch_job.id, actor=default_user)+ llm_batch_job = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_job.id, actor=default_user)# Verify job propertiesassert llm_batch_job.status == JobStatus.completed, "Job status should be 'completed'"# Verify batch items- items = server.batch_manager.list_llm_batch_items(llm_batch_id=llm_batch_job.id, actor=default_user)+ items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=llm_batch_job.id, actor=default_user)assert len(items) == 3, f"Expected 3 batch items, got {len(items)}"assert all([item.request_status == JobStatus.completed for item in items])@@ -696,7 +696,7 @@ async def test_resume_step_some_stop(assert post_resume_response.agent_count == 1# New batch‑items should exist, initialised in (created, paused) state- new_items = server.batch_manager.list_llm_batch_items(+ new_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=post_resume_response.last_llm_batch_id, actor=default_user)assert len(new_items) == 1, f"Expected 1 new batch item, got {len(new_items)}"@@ -717,7 +717,7 @@ async def test_resume_step_some_stop(# Old items must have been flipped to completed / finished earlier# (sanity – we already asserted this above, but we keep it close for clarity)- old_items = server.batch_manager.list_llm_batch_items(+ old_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=pre_resume_response.last_llm_batch_id, actor=default_user)assert {i.request_status for i in old_items} == {JobStatus.completed}@@ -743,7 +743,7 @@ async def test_resume_step_some_stop(letta_batch_job_id=pre_resume_response.letta_batch_id, limit=200, actor=default_user)assert len(messages) == len(agents) * 3 + 1- assert_descending_order(messages)+ _assert_descending_order(messages)# Check that each agent is representedfor agent in agents_continue:agent_messages = [m for m in messages if m.agent_id == agent.id]@@ -761,21 +761,19 @@ async def test_resume_step_some_stop(assert agent_messages[-3].role == MessageRole.tool, "Expected tool response after assistant tool call"-def assert_descending_order(messages):- """Assert messages are in descending order by created_at timestamps."""+def _assert_descending_order(messages):+ """Assert messages are in monotonically decreasing by created_at timestamps."""if len(messages) <= 1:return True- for i in range(1, len(messages)):- assert messages[i].created_at <= messages[i - 1].created_at, (- f"Order violation: {messages[i - 1].id} ({messages[i - 1].created_at}) "- f"followed by {messages[i].id} ({messages[i].created_at})"- )-+ for prev, next in zip(messages[:-1], messages[1:]):+ assert (+ prev.created_at >= next.created_at+ ), f"Order violation: {prev.id} ({prev.created_at}) followed by {next.id} ({next.created_at})"return True[email protected](loop_scope="session")async def test_resume_step_after_request_all_continue(disable_e2b_api_key, server, default_user, agents: Tuple[AgentState], batch_requests, step_state_map, batch_job):@@ -811,7 +809,7 @@ async def test_resume_step_after_request_all_continue(assert len(llm_batch_jobs) == 1, f"Expected 1 llm_batch_jobs, got {len(llm_batch_jobs)}"llm_batch_job = llm_batch_jobs[0]- llm_batch_items = server.batch_manager.list_llm_batch_items(llm_batch_id=llm_batch_job.id, actor=default_user)+ llm_batch_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=llm_batch_job.id, actor=default_user)assert len(llm_batch_items) == 3, f"Expected 3 llm_batch_items, got {len(llm_batch_items)}"# 2. Invoke the polling job and mock responses from Anthropic@@ -833,13 +831,13 @@ async def test_resume_step_after_request_all_continue(new_batch_responses = await poll_running_llm_batches(server)# Verify database records were updated correctly- llm_batch_job = server.batch_manager.get_llm_batch_job_by_id(llm_batch_job.id, actor=default_user)+ llm_batch_job = await server.batch_manager.get_llm_batch_job_by_id_async(llm_batch_job.id, actor=default_user)# Verify job propertiesassert llm_batch_job.status == JobStatus.completed, "Job status should be 'completed'"# Verify batch items- items = server.batch_manager.list_llm_batch_items(llm_batch_id=llm_batch_job.id, actor=default_user)+ items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=llm_batch_job.id, actor=default_user)assert len(items) == 3, f"Expected 3 batch items, got {len(items)}"assert all([item.request_status == JobStatus.completed for item in items])@@ -857,7 +855,7 @@ async def test_resume_step_after_request_all_continue(assert post_resume_response.agent_count == 3# New batch‑items should exist, initialised in (created, paused) state- new_items = server.batch_manager.list_llm_batch_items(+ new_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=post_resume_response.last_llm_batch_id, actor=default_user)assert len(new_items) == 3, f"Expected 3 new batch items, got {len(new_items)}"@@ -876,7 +874,7 @@ async def test_resume_step_after_request_all_continue(# Old items must have been flipped to completed / finished earlier# (sanity – we already asserted this above, but we keep it close for clarity)- old_items = server.batch_manager.list_llm_batch_items(+ old_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=pre_resume_response.last_llm_batch_id, actor=default_user)assert {i.request_status for i in old_items} == {JobStatus.completed}@@ -902,7 +900,7 @@ async def test_resume_step_after_request_all_continue(letta_batch_job_id=pre_resume_response.letta_batch_id, limit=200, actor=default_user)assert len(messages) == len(agents) * 4- assert_descending_order(messages)+ _assert_descending_order(messages)# Check that each agent is representedfor agent in agents:agent_messages = [m for m in messages if m.agent_id == agent.id]@@ -913,7 +911,7 @@ async def test_resume_step_after_request_all_continue(assert agent_messages[-4].role == MessageRole.user, "Expected final system-level heartbeat user message"[email protected](loop_scope="session")async def test_step_until_request_prepares_and_submits_batch_correctly(disable_e2b_api_key, server, default_user, agents, batch_requests, step_state_map, dummy_batch_response, batch_job):@@ -1006,7 +1004,7 @@ async def test_step_until_request_prepares_and_submits_batch_correctly(assert len(llm_batch_jobs) == 1, f"Expected 1 llm_batch_jobs, got {len(llm_batch_jobs)}"llm_batch_job = llm_batch_jobs[0]- llm_batch_items = server.batch_manager.list_llm_batch_items(llm_batch_id=llm_batch_job.id, actor=default_user)+ llm_batch_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=llm_batch_job.id, actor=default_user)assert len(llm_batch_items) == 3, f"Expected 3 llm_batch_items, got {len(llm_batch_items)}"# Verify job propertiesdiff --git a/tests/test_managers.py b/tests/test_managers.pyindex ab539879e..0be3d7a6c 100644--- a/tests/test_managers.py+++ b/tests/test_managers.py@@ -505,7 +505,8 @@ def server():@pytest.fixture-def agent_passages_setup(server, default_source, default_user, sarah_agent):+async def agent_passages_setup(server, default_source, default_user, sarah_agent):"""Setup fixture for agent passages tests"""agent_id = sarah_agent.idactor = default_user@@ -640,13 +641,14 @@ def event_loop(request):# ======================================================================================================================# AgentManager Tests - Basic# ======================================================================================================================-def test_create_get_list_agent(server: SyncServer, comprehensive_test_agent_fixture, default_user):+async def test_create_get_list_agent(server: SyncServer, comprehensive_test_agent_fixture, default_user, event_loop):# Test agent creationcreated_agent, create_agent_request = comprehensive_test_agent_fixturecomprehensive_agent_checks(created_agent, create_agent_request, actor=default_user)# Test get agent- get_agent = server.agent_manager.get_agent_by_id(agent_id=created_agent.id, actor=default_user)+ get_agent = await server.agent_manager.get_agent_by_id_async(agent_id=created_agent.id, actor=default_user)comprehensive_agent_checks(get_agent, create_agent_request, actor=default_user)# Test get agent name@@ -996,35 +998,37 @@ def test_list_agents_ordering_and_pagination(server: SyncServer, default_user):# ======================================================================================================================-def test_attach_tool(server: SyncServer, sarah_agent, print_tool, default_user):+async def test_attach_tool(server: SyncServer, sarah_agent, print_tool, default_user, event_loop):"""Test attaching a tool to an agent."""# Attach the toolserver.agent_manager.attach_tool(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)# Verify attachment through get_agent_by_id- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)assert print_tool.id in [t.id for t in agent.tools]# Verify that attaching the same tool again doesn't cause duplicationserver.agent_manager.attach_tool(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)assert len([t for t in agent.tools if t.id == print_tool.id]) == 1-def test_detach_tool(server: SyncServer, sarah_agent, print_tool, default_user):+async def test_detach_tool(server: SyncServer, sarah_agent, print_tool, default_user, event_loop):"""Test detaching a tool from an agent."""# Attach the tool firstserver.agent_manager.attach_tool(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)# Verify it's attached- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)assert print_tool.id in [t.id for t in agent.tools]# Detach the toolserver.agent_manager.detach_tool(agent_id=sarah_agent.id, tool_id=print_tool.id, actor=default_user)# Verify it's detached- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)assert print_tool.id not in [t.id for t in agent.tools]# Verify that detaching an already detached tool doesn't cause issues@@ -1049,10 +1053,11 @@ def test_detach_tool_nonexistent_agent(server: SyncServer, print_tool, default_userver.agent_manager.detach_tool(agent_id="nonexistent-agent-id", tool_id=print_tool.id, actor=default_user)-def test_list_attached_tools(server: SyncServer, sarah_agent, print_tool, other_tool, default_user):+async def test_list_attached_tools(server: SyncServer, sarah_agent, print_tool, other_tool, default_user, event_loop):"""Test listing tools attached to an agent."""# Initially should have no tools- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert len(agent.tools) == 0# Attach tools@@ -1060,7 +1065,7 @@ def test_list_attached_tools(server: SyncServer, sarah_agent, print_tool, other_server.agent_manager.attach_tool(agent_id=sarah_agent.id, tool_id=other_tool.id, actor=default_user)# List tools and verify- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)attached_tool_ids = [t.id for t in agent.tools]assert len(attached_tool_ids) == 2assert print_tool.id in attached_tool_ids@@ -1072,18 +1077,19 @@ def test_list_attached_tools(server: SyncServer, sarah_agent, print_tool, other_# ======================================================================================================================-def test_attach_source(server: SyncServer, sarah_agent, default_source, default_user):+async def test_attach_source(server: SyncServer, sarah_agent, default_source, default_user, event_loop):"""Test attaching a source to an agent."""# Attach the sourceserver.agent_manager.attach_source(agent_id=sarah_agent.id, source_id=default_source.id, actor=default_user)# Verify attachment through get_agent_by_id- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert default_source.id in [s.id for s in agent.sources]# Verify that attaching the same source again doesn't cause issuesserver.agent_manager.attach_source(agent_id=sarah_agent.id, source_id=default_source.id, actor=default_user)- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert len([s for s in agent.sources if s.id == default_source.id]) == 1@@ -1105,20 +1111,21 @@ def test_list_attached_source_ids(server: SyncServer, sarah_agent, default_sourcassert other_source.id in source_ids-def test_detach_source(server: SyncServer, sarah_agent, default_source, default_user):+async def test_detach_source(server: SyncServer, sarah_agent, default_source, default_user, event_loop):"""Test detaching a source from an agent."""# Attach sourceserver.agent_manager.attach_source(sarah_agent.id, default_source.id, actor=default_user)# Verify it's attached- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert default_source.id in [s.id for s in agent.sources]# Detach sourceserver.agent_manager.detach_source(sarah_agent.id, default_source.id, actor=default_user)# Verify it's detached- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert default_source.id not in [s.id for s in agent.sources]# Verify that detaching an already detached source doesn't cause issues@@ -1239,14 +1246,14 @@ def test_list_agents_by_tags_match_all(server: SyncServer, sarah_agent, charles_server.agent_manager.update_agent(charles_agent.id, UpdateAgent(tags=["test", "development", "gpt4"]), actor=default_user)# Search for agents with all specified tags- agents = server.agent_manager.list_agents(tags=["test", "gpt4"], match_all_tags=True, actor=default_user)+ agents = server.agent_manager.list_agents(actor=default_user, tags=["test", "gpt4"], match_all_tags=True)assert len(agents) == 2agent_ids = [a.id for a in agents]assert sarah_agent.id in agent_idsassert charles_agent.id in agent_ids# Search for tags that only sarah_agent has- agents = server.agent_manager.list_agents(tags=["test", "production"], match_all_tags=True, actor=default_user)+ agents = server.agent_manager.list_agents(actor=default_user, tags=["test", "production"], match_all_tags=True)assert len(agents) == 1assert agents[0].id == sarah_agent.id@@ -1258,14 +1265,14 @@ def test_list_agents_by_tags_match_any(server: SyncServer, sarah_agent, charles_server.agent_manager.update_agent(charles_agent.id, UpdateAgent(tags=["development", "gpt3"]), actor=default_user)# Search for agents with any of the specified tags- agents = server.agent_manager.list_agents(tags=["production", "development"], match_all_tags=False, actor=default_user)+ agents = server.agent_manager.list_agents(actor=default_user, tags=["production", "development"], match_all_tags=False)assert len(agents) == 2agent_ids = [a.id for a in agents]assert sarah_agent.id in agent_idsassert charles_agent.id in agent_ids# Search for tags where only sarah_agent matches- agents = server.agent_manager.list_agents(tags=["production", "nonexistent"], match_all_tags=False, actor=default_user)+ agents = server.agent_manager.list_agents(actor=default_user, tags=["production", "nonexistent"], match_all_tags=False)assert len(agents) == 1assert agents[0].id == sarah_agent.id@@ -1277,10 +1284,10 @@ def test_list_agents_by_tags_no_matches(server: SyncServer, sarah_agent, charlesserver.agent_manager.update_agent(charles_agent.id, UpdateAgent(tags=["development", "gpt3"]), actor=default_user)# Search for nonexistent tags- agents = server.agent_manager.list_agents(tags=["nonexistent1", "nonexistent2"], match_all_tags=True, actor=default_user)+ agents = server.agent_manager.list_agents(actor=default_user, tags=["nonexistent1", "nonexistent2"], match_all_tags=True)assert len(agents) == 0- agents = server.agent_manager.list_agents(tags=["nonexistent1", "nonexistent2"], match_all_tags=False, actor=default_user)+ agents = server.agent_manager.list_agents(actor=default_user, tags=["nonexistent1", "nonexistent2"], match_all_tags=False)assert len(agents) == 0@@ -1328,20 +1335,20 @@ def test_list_agents_by_tags_pagination(server: SyncServer, default_user, defaul)# Get first page- first_page = server.agent_manager.list_agents(tags=["pagination_test"], match_all_tags=True, actor=default_user, limit=1)+ first_page = server.agent_manager.list_agents(actor=default_user, tags=["pagination_test"], match_all_tags=True, limit=1)assert len(first_page) == 1first_agent_id = first_page[0].id# Get second page using cursorsecond_page = server.agent_manager.list_agents(- tags=["pagination_test"], match_all_tags=True, actor=default_user, after=first_agent_id, limit=1+ actor=default_user, tags=["pagination_test"], match_all_tags=True, after=first_agent_id, limit=1)assert len(second_page) == 1assert second_page[0].id != first_agent_id# Get previous page using beforeprev_page = server.agent_manager.list_agents(- tags=["pagination_test"], match_all_tags=True, actor=default_user, before=second_page[0].id, limit=1+ actor=default_user, tags=["pagination_test"], match_all_tags=True, before=second_page[0].id, limit=1)assert len(prev_page) == 1assert prev_page[0].id == first_agent_id@@ -1435,14 +1442,15 @@ def test_list_agents_query_text_pagination(server: SyncServer, default_user, def# ======================================================================================================================-def test_reset_messages_no_messages(server: SyncServer, sarah_agent, default_user):+async def test_reset_messages_no_messages(server: SyncServer, sarah_agent, default_user, event_loop):"""Test that resetting messages on an agent that has zero messagesdoes not fail and clears out message_ids if somehow it's non-empty."""# Force a weird scenario: Suppose the message_ids field was set non-empty (without actual messages).server.agent_manager.update_agent(sarah_agent.id, UpdateAgent(message_ids=["ghost-message-id"]), actor=default_user)- updated_agent = server.agent_manager.get_agent_by_id(sarah_agent.id, default_user)+ updated_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)assert updated_agent.message_ids == ["ghost-message-id"]# Reset messages@@ -1452,14 +1460,15 @@ def test_reset_messages_no_messages(server: SyncServer, sarah_agent, default_useassert server.message_manager.size(agent_id=sarah_agent.id, actor=default_user) == 1-def test_reset_messages_default_messages(server: SyncServer, sarah_agent, default_user):+async def test_reset_messages_default_messages(server: SyncServer, sarah_agent, default_user, event_loop):"""Test that resetting messages on an agent that has zero messagesdoes not fail and clears out message_ids if somehow it's non-empty."""# Force a weird scenario: Suppose the message_ids field was set non-empty (without actual messages).server.agent_manager.update_agent(sarah_agent.id, UpdateAgent(message_ids=["ghost-message-id"]), actor=default_user)- updated_agent = server.agent_manager.get_agent_by_id(sarah_agent.id, default_user)+ updated_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)assert updated_agent.message_ids == ["ghost-message-id"]# Reset messages@@ -1469,7 +1478,8 @@ def test_reset_messages_default_messages(server: SyncServer, sarah_agent, defaulassert server.message_manager.size(agent_id=sarah_agent.id, actor=default_user) == 4-def test_reset_messages_with_existing_messages(server: SyncServer, sarah_agent, default_user):+async def test_reset_messages_with_existing_messages(server: SyncServer, sarah_agent, default_user, event_loop):"""Test that resetting messages on an agent with actual messagesdeletes them from the database and clears message_ids.@@ -1495,7 +1505,7 @@ def test_reset_messages_with_existing_messages(server: SyncServer, sarah_agent,)# Verify the messages were created- agent_before = server.agent_manager.get_agent_by_id(sarah_agent.id, default_user)+ agent_before = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)# This is 4 because creating the message does not necessarily add it to the in context message idsassert len(agent_before.message_ids) == 4assert server.message_manager.size(agent_id=sarah_agent.id, actor=default_user) == 6@@ -1647,13 +1657,14 @@ def test_list_messages_with_query_text_filter(server: SyncServer, sarah_agent, d# ======================================================================================================================-def test_attach_block(server: SyncServer, sarah_agent, default_block, default_user):+async def test_attach_block(server: SyncServer, sarah_agent, default_block, default_user, event_loop):"""Test attaching a block to an agent."""# Attach blockserver.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)# Verify attachment- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert len(agent.memory.blocks) == 1assert agent.memory.blocks[0].id == default_block.idassert agent.memory.blocks[0].label == default_block.label@@ -1674,7 +1685,8 @@ def test_attach_block_duplicate_label(server: SyncServer, sarah_agent, default_bserver.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=other_block.id, actor=default_user)-def test_detach_block(server: SyncServer, sarah_agent, default_block, default_user):+async def test_detach_block(server: SyncServer, sarah_agent, default_block, default_user, event_loop):"""Test detaching a block by ID."""# Set up: attach blockserver.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)@@ -1683,7 +1695,7 @@ def test_detach_block(server: SyncServer, sarah_agent, default_block, default_usserver.agent_manager.detach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)# Verify detachment- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert len(agent.memory.blocks) == 0# Check that block still exists@@ -1697,7 +1709,8 @@ def test_detach_nonexistent_block(server: SyncServer, sarah_agent, default_user)server.agent_manager.detach_block(agent_id=sarah_agent.id, block_id="nonexistent-block-id", actor=default_user)-def test_update_block_label(server: SyncServer, sarah_agent, default_block, default_user):+async def test_update_block_label(server: SyncServer, sarah_agent, default_block, default_user, event_loop):"""Test updating a block's label updates the relationship."""# Attach blockserver.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)@@ -1707,13 +1720,14 @@ def test_update_block_label(server: SyncServer, sarah_agent, default_block, defaserver.block_manager.update_block(default_block.id, BlockUpdate(label=new_label), actor=default_user)# Verify relationship is updated- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)block = agent.memory.blocks[0]assert block.id == default_block.idassert block.label == new_label-def test_update_block_label_multiple_agents(server: SyncServer, sarah_agent, charles_agent, default_block, default_user):+async def test_update_block_label_multiple_agents(server: SyncServer, sarah_agent, charles_agent, default_block, default_user, event_loop):"""Test updating a block's label updates relationships for all agents."""# Attach block to both agentsserver.agent_manager.attach_block(agent_id=sarah_agent.id, block_id=default_block.id, actor=default_user)@@ -1725,7 +1739,7 @@ def test_update_block_label_multiple_agents(server: SyncServer, sarah_agent, cha# Verify both relationships are updatedfor agent_id in [sarah_agent.id, charles_agent.id]:- agent = server.agent_manager.get_agent_by_id(agent_id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(agent_id, actor=default_user)# Find our specific block by IDblock = next(b for b in agent.memory.blocks if b.id == default_block.id)assert block.label == new_label@@ -2258,9 +2272,10 @@ def test_get_tool_with_actor(server: SyncServer, print_tool, default_user):assert fetched_tool.tool_type == ToolType.CUSTOM-def test_list_tools(server: SyncServer, print_tool, default_user):+async def test_list_tools(server: SyncServer, print_tool, default_user, event_loop):# List tools (should include the one created by the fixture)- tools = server.tool_manager.list_tools(actor=default_user)+ tools = await server.tool_manager.list_tools_async(actor=default_user)# Assertions to check that the created tool is listedassert len(tools) == 1@@ -2384,11 +2399,12 @@ def test_update_tool_multi_user(server: SyncServer, print_tool, default_user, otassert updated_tool.created_by_id == default_user.id-def test_delete_tool_by_id(server: SyncServer, print_tool, default_user):+async def test_delete_tool_by_id(server: SyncServer, print_tool, default_user, event_loop):# Delete the print_tool using the manager methodserver.tool_manager.delete_tool_by_id(print_tool.id, actor=default_user)- tools = server.tool_manager.list_tools(actor=default_user)+ tools = await server.tool_manager.list_tools_async(actor=default_user)assert len(tools) == 0@@ -2801,7 +2817,7 @@ async def test_delete_block_detaches_from_agent(server: SyncServer, sarah_agent,assert len(blocks) == 0# Check that block has been detached too- agent_state = server.agent_manager.get_agent_by_id(agent_id=sarah_agent.id, actor=default_user)+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)assert not (block.id in [b.id for b in agent_state.memory.blocks])@@ -2856,7 +2872,10 @@ async def test_batch_create_multiple_blocks(server: SyncServer, default_user, evassert expected_labels.issubset(all_labels)-def test_bulk_update_skips_missing_and_truncates_then_returns_none(server: SyncServer, default_user: PydanticUser, caplog):+async def test_bulk_update_skips_missing_and_truncates_then_returns_none(+ server: SyncServer, default_user: PydanticUser, caplog, event_loop+):mgr = BlockManager()# create one block with a small limit@@ -2873,7 +2892,7 @@ def test_bulk_update_skips_missing_and_truncates_then_returns_none(server: SyncS}caplog.set_level(logging.WARNING)- result = mgr.bulk_update_block_values(updates, actor=default_user)+ result = await mgr.bulk_update_block_values_async(updates, actor=default_user)# default return_hydrated=False → should be Noneassert result is None@@ -2887,7 +2906,9 @@ def test_bulk_update_skips_missing_and_truncates_then_returns_none(server: SyncSassert reloaded.value == long_val[:5]-def test_bulk_update_return_hydrated_true(server: SyncServer, default_user: PydanticUser):[email protected](reason="TODO: implement for async")+async def test_bulk_update_return_hydrated_true(server: SyncServer, default_user: PydanticUser, event_loop):mgr = BlockManager()# create a block@@ -2897,7 +2918,7 @@ def test_bulk_update_return_hydrated_true(server: SyncServer, default_user: Pyda)updates = {b.id: "new-val"}- updated = mgr.bulk_update_block_values(updates, actor=default_user, return_hydrated=True)+ updated = await mgr.bulk_update_block_values_async(updates, actor=default_user, return_hydrated=True)# with return_hydrated=True, we get back a list of schemasassert isinstance(updated, list) and len(updated) == 1@@ -2905,7 +2926,10 @@ def test_bulk_update_return_hydrated_true(server: SyncServer, default_user: Pydaassert updated[0].value == "new-val"-def test_bulk_update_respects_org_scoping(server: SyncServer, default_user: PydanticUser, other_user_different_org: PydanticUser, caplog):+async def test_bulk_update_respects_org_scoping(+ server: SyncServer, default_user: PydanticUser, other_user_different_org: PydanticUser, caplog, event_loop+):mgr = BlockManager()# one block in each org@@ -2924,7 +2948,7 @@ def test_bulk_update_respects_org_scoping(server: SyncServer, default_user: Pyda}caplog.set_level(logging.WARNING)- mgr.bulk_update_block_values(updates, actor=default_user)+ await mgr.bulk_update_block_values_async(updates, actor=default_user)# mine should be updated...reloaded_mine = mgr.get_block_by_id(actor=default_user, block_id=mine.id)@@ -3573,7 +3597,8 @@ def test_get_identities(server, default_user):server.identity_manager.delete_identity(identity_id=org.id, actor=default_user)-def test_update_identity(server: SyncServer, sarah_agent, charles_agent, default_user):+async def test_update_identity(server: SyncServer, sarah_agent, charles_agent, default_user, event_loop):identity = server.identity_manager.create_identity(IdentityCreate(name="caren", identifier_key="1234", identity_type=IdentityType.user), actor=default_user)@@ -3592,15 +3617,16 @@ def test_update_identity(server: SyncServer, sarah_agent, charles_agent, defaultassert updated_identity.agent_ids.sort() == update_data.agent_ids.sort()assert updated_identity.properties == update_data.properties- agent_state = server.agent_manager.get_agent_by_id(agent_id=sarah_agent.id, actor=default_user)+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)assert identity.id in agent_state.identity_ids- agent_state = server.agent_manager.get_agent_by_id(agent_id=charles_agent.id, actor=default_user)+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=charles_agent.id, actor=default_user)assert identity.id in agent_state.identity_idsserver.identity_manager.delete_identity(identity_id=identity.id, actor=default_user)-def test_attach_detach_identity_from_agent(server: SyncServer, sarah_agent, default_user):+async def test_attach_detach_identity_from_agent(server: SyncServer, sarah_agent, default_user, event_loop):# Create an identityidentity = server.identity_manager.create_identity(IdentityCreate(name="caren", identifier_key="1234", identity_type=IdentityType.user), actor=default_user@@ -3620,7 +3646,7 @@ def test_attach_detach_identity_from_agent(server: SyncServer, sarah_agent, defaassert len(identities) == 0# Check that block has been detached too- agent_state = server.agent_manager.get_agent_by_id(agent_id=sarah_agent.id, actor=default_user)+ agent_state = await server.agent_manager.get_agent_by_id_async(agent_id=sarah_agent.id, actor=default_user)assert not identity.id in agent_state.identity_ids@@ -3864,7 +3890,8 @@ def test_delete_source(server: SyncServer, default_user):assert len(sources) == 0-def test_delete_attached_source(server: SyncServer, sarah_agent, default_user):+async def test_delete_attached_source(server: SyncServer, sarah_agent, default_user, event_loop):"""Test deleting a source."""source_pydantic = PydanticSource(name="To Delete", description="This source will be deleted.", embedding_config=DEFAULT_EMBEDDING_CONFIG@@ -3884,7 +3911,7 @@ def test_delete_attached_source(server: SyncServer, sarah_agent, default_user):assert len(sources) == 0# Verify that agent is not deleted- agent = server.agent_manager.get_agent_by_id(sarah_agent.id, actor=default_user)+ agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, actor=default_user)assert agent is not None@@ -5037,8 +5064,9 @@ def test_list_tags(server: SyncServer, default_user, default_organization):# ======================================================================================================================-def test_create_and_get_batch_request(server, default_user, dummy_beta_message_batch, letta_batch_job):- batch = server.batch_manager.create_llm_batch_job(+async def test_create_and_get_batch_request(server, default_user, dummy_beta_message_batch, letta_batch_job, event_loop):+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.created,create_batch_response=dummy_beta_message_batch,@@ -5047,12 +5075,13 @@ def test_create_and_get_batch_request(server, default_user, dummy_beta_message_b)assert batch.id.startswith("batch_req-")assert batch.create_batch_response == dummy_beta_message_batch- fetched = server.batch_manager.get_llm_batch_job_by_id(batch.id, actor=default_user)+ fetched = await server.batch_manager.get_llm_batch_job_by_id_async(batch.id, actor=default_user)assert fetched.id == batch.id-def test_update_batch_status(server, default_user, dummy_beta_message_batch, letta_batch_job):- batch = server.batch_manager.create_llm_batch_job(+async def test_update_batch_status(server, default_user, dummy_beta_message_batch, letta_batch_job, event_loop):+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.created,create_batch_response=dummy_beta_message_batch,@@ -5068,16 +5097,17 @@ def test_update_batch_status(server, default_user, dummy_beta_message_batch, letactor=default_user,)- updated = server.batch_manager.get_llm_batch_job_by_id(batch.id, actor=default_user)+ updated = await server.batch_manager.get_llm_batch_job_by_id_async(batch.id, actor=default_user)assert updated.status == JobStatus.completedassert updated.latest_polling_response == dummy_beta_message_batchassert updated.last_polled_at >= before-def test_create_and_get_batch_item(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_create_and_get_batch_item(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.created,create_batch_response=dummy_beta_message_batch,@@ -5101,7 +5131,8 @@ def test_create_and_get_batch_item(assert fetched.id == item.id-def test_update_batch_item(+async def test_update_batch_item(server,default_user,sarah_agent,@@ -5110,8 +5141,9 @@ def test_update_batch_item(dummy_step_state,dummy_successful_response,letta_batch_job,+ event_loop,):- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.created,create_batch_response=dummy_beta_message_batch,@@ -5143,10 +5175,11 @@ def test_update_batch_item(assert updated.batch_request_result == dummy_successful_response-def test_delete_batch_item(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_delete_batch_item(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.created,create_batch_response=dummy_beta_message_batch,@@ -5168,8 +5201,9 @@ def test_delete_batch_item(server.batch_manager.get_llm_batch_item_by_id(item.id, actor=default_user)-def test_list_running_batches(server, default_user, dummy_beta_message_batch, letta_batch_job):- server.batch_manager.create_llm_batch_job(+async def test_list_running_batches(server, default_user, dummy_beta_message_batch, letta_batch_job, event_loop):+ await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.running,create_batch_response=dummy_beta_message_batch,@@ -5177,13 +5211,14 @@ def test_list_running_batches(server, default_user, dummy_beta_message_batch, leletta_batch_job_id=letta_batch_job.id,)- running_batches = server.batch_manager.list_running_llm_batches(actor=default_user)+ running_batches = await server.batch_manager.list_running_llm_batches_async(actor=default_user)assert len(running_batches) >= 1assert all(batch.status == JobStatus.running for batch in running_batches)-def test_bulk_update_batch_statuses(server, default_user, dummy_beta_message_batch, letta_batch_job):- batch = server.batch_manager.create_llm_batch_job(+async def test_bulk_update_batch_statuses(server, default_user, dummy_beta_message_batch, letta_batch_job, event_loop):+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.created,create_batch_response=dummy_beta_message_batch,@@ -5193,12 +5228,13 @@ def test_bulk_update_batch_statuses(server, default_user, dummy_beta_message_batserver.batch_manager.bulk_update_llm_batch_statuses([(batch.id, JobStatus.completed, dummy_beta_message_batch)])- updated = server.batch_manager.get_llm_batch_job_by_id(batch.id, actor=default_user)+ updated = await server.batch_manager.get_llm_batch_job_by_id_async(batch.id, actor=default_user)assert updated.status == JobStatus.completedassert updated.latest_polling_response == dummy_beta_message_batch-def test_bulk_update_batch_items_results_by_agent(+async def test_bulk_update_batch_items_results_by_agent(server,default_user,sarah_agent,@@ -5207,8 +5243,9 @@ def test_bulk_update_batch_items_results_by_agent(dummy_step_state,dummy_successful_response,letta_batch_job,+ event_loop,):- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5231,10 +5268,11 @@ def test_bulk_update_batch_items_results_by_agent(assert updated.batch_request_result == dummy_successful_response-def test_bulk_update_batch_items_step_status_by_agent(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_bulk_update_batch_items_step_status_by_agent(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5256,10 +5294,11 @@ def test_bulk_update_batch_items_step_status_by_agent(assert updated.step_status == AgentStepStatus.resumed-def test_list_batch_items_limit_and_filter(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_list_batch_items_limit_and_filter(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5275,18 +5314,19 @@ def test_list_batch_items_limit_and_filter(actor=default_user,)- all_items = server.batch_manager.list_llm_batch_items(llm_batch_id=batch.id, actor=default_user)- limited_items = server.batch_manager.list_llm_batch_items(llm_batch_id=batch.id, limit=2, actor=default_user)+ all_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=batch.id, actor=default_user)+ limited_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=batch.id, limit=2, actor=default_user)assert len(all_items) >= 3assert len(limited_items) == 2-def test_list_batch_items_pagination(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_list_batch_items_pagination(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):# Create a batch job.- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5306,7 +5346,7 @@ def test_list_batch_items_pagination(created_items.append(item)# Retrieve all items (without pagination).- all_items = server.batch_manager.list_llm_batch_items(llm_batch_id=batch.id, actor=default_user)+ all_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=batch.id, actor=default_user)assert len(all_items) >= 10, f"Expected at least 10 items, got {len(all_items)}"# Verify the items are ordered ascending by id (based on our implementation).@@ -5318,7 +5358,7 @@ def test_list_batch_items_pagination(cursor = all_items[4].id# Retrieve items after the cursor.- paged_items = server.batch_manager.list_llm_batch_items(llm_batch_id=batch.id, actor=default_user, after=cursor)+ paged_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=batch.id, actor=default_user, after=cursor)# All returned items should have an id greater than the cursor.for item in paged_items:@@ -5332,7 +5372,9 @@ def test_list_batch_items_pagination(# Test pagination with a limit.limit = 3- limited_page = server.batch_manager.list_llm_batch_items(llm_batch_id=batch.id, actor=default_user, after=cursor, limit=limit)+ limited_page = await server.batch_manager.list_llm_batch_items_async(+ llm_batch_id=batch.id, actor=default_user, after=cursor, limit=limit+ )# If more than 'limit' items remain, we should only get exactly 'limit' items.assert len(limited_page) == min(limit, expected_remaining@@ -5340,15 +5382,16 @@ def test_list_batch_items_pagination(# Optional: Test with a cursor beyond the last item returns an empty list.last_cursor = sorted_ids[-1]- empty_page = server.batch_manager.list_llm_batch_items(llm_batch_id=batch.id, actor=default_user, after=last_cursor)+ empty_page = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=batch.id, actor=default_user, after=last_cursor)assert empty_page == [], "Expected an empty list when cursor is after the last item"-def test_bulk_update_batch_items_request_status_by_agent(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_bulk_update_batch_items_request_status_by_agent(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):# Create a batch job- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5374,15 +5417,17 @@ def test_bulk_update_batch_items_request_status_by_agent(assert updated.request_status == JobStatus.expired-def test_bulk_update_nonexistent_items_should_error(+async def test_bulk_update_nonexistent_items_should_error(server,default_user,dummy_beta_message_batch,dummy_successful_response,letta_batch_job,+ event_loop,):# Create a batch job- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5415,9 +5460,12 @@ def test_bulk_update_nonexistent_items_should_error()-def test_bulk_update_nonexistent_items(server, default_user, dummy_beta_message_batch, dummy_successful_response, letta_batch_job):+async def test_bulk_update_nonexistent_items(+ server, default_user, dummy_beta_message_batch, dummy_successful_response, letta_batch_job, event_loop+):# Create a batch job- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5450,11 +5498,12 @@ def test_bulk_update_nonexistent_items(server, default_user, dummy_beta_message_)-def test_create_batch_items_bulk(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_create_batch_items_bulk(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):# Create a batch job- llm_batch_job = server.batch_manager.create_llm_batch_job(+ llm_batch_job = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,create_batch_response=dummy_beta_message_batch,actor=default_user,@@ -5477,7 +5526,7 @@ def test_create_batch_items_bulk(batch_items.append(batch_item)# Call the bulk create function- created_items = server.batch_manager.create_llm_batch_items_bulk(batch_items, actor=default_user)+ created_items = await server.batch_manager.create_llm_batch_items_bulk_async(batch_items, actor=default_user)# Verify the correct number of items were createdassert len(created_items) == len(agent_ids)@@ -5493,7 +5542,7 @@ def test_create_batch_items_bulk(assert item.step_state == dummy_step_state# Verify items can be retrieved from the database- all_items = server.batch_manager.list_llm_batch_items(llm_batch_id=llm_batch_job.id, actor=default_user)+ all_items = await server.batch_manager.list_llm_batch_items_async(llm_batch_id=llm_batch_job.id, actor=default_user)assert len(all_items) >= len(agent_ids)# Verify the IDs of created items match what's in the database@@ -5503,11 +5552,12 @@ def test_create_batch_items_bulk(assert fetched.id in created_ids-def test_count_batch_items(- server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job+async def test_count_batch_items(+ server, default_user, sarah_agent, dummy_beta_message_batch, dummy_llm_config, dummy_step_state, letta_batch_job, event_loop):# Create a batch job first.- batch = server.batch_manager.create_llm_batch_job(+ batch = await server.batch_manager.create_llm_batch_job_async(llm_provider=ProviderType.anthropic,status=JobStatus.created,create_batch_response=dummy_beta_message_batch,diff --git a/tests/test_memory.py b/tests/test_memory.pyindex 85e12e801..87e02a0e3 100644--- a/tests/test_memory.py+++ b/tests/test_memory.py@@ -25,23 +25,6 @@ def test_memory_limit_validation(sample_memory: Memory):sample_memory.get_block("persona").value = "x " * 10000-def test_memory_jinja2_template(sample_memory: Memory):- """Test to make sure the jinja2 template string is equivalent to the old __repr__ method"""-- def old_repr(self: Memory) -> str:- """Generate a string representation of the memory in-context"""- section_strs = []- for block in sample_memory.get_blocks():- section = block.label- module = block- section_strs.append(f'<{section} characters="{len(module.value)}/{module.limit}">\n{module.value}\n</{section}>')- return "\n".join(section_strs)-- old_repr_str = old_repr(sample_memory)- new_repr_str = sample_memory.compile()- assert new_repr_str == old_repr_str, f"Expected '{old_repr_str}' to be '{new_repr_str}'"--def test_memory_jinja2_set_template(sample_memory: Memory):"""Test setting the template for the memory"""diff --git a/tests/test_sdk_client.py b/tests/test_sdk_client.pyindex c7493239c..91fd016cb 100644--- a/tests/test_sdk_client.py+++ b/tests/test_sdk_client.py@@ -122,6 +122,45 @@ def test_shared_blocks(client: LettaSDKClient):client.agents.delete(agent_state2.id)+def test_read_only_block(client: LettaSDKClient):+ block_value = "username: sarah"+ agent = client.agents.create(+ memory_blocks=[+ CreateBlock(+ label="human",+ value=block_value,+ read_only=True,+ ),+ ],+ model="openai/gpt-4o-mini",+ embedding="openai/text-embedding-ada-002",+ )++ # make sure agent cannot update read-only block+ client.agents.messages.create(+ agent_id=agent.id,+ messages=[+ MessageCreate(+ role="user",+ content="my name is actually charles",+ )+ ],+ )++ # make sure block value is still the same+ block = client.agents.blocks.retrieve(agent_id=agent.id, block_label="human")+ assert block.value == block_value++ # make sure can update from client+ new_value = "hello"+ client.agents.blocks.modify(agent_id=agent.id, block_label="human", value=new_value)+ block = client.agents.blocks.retrieve(agent_id=agent.id, block_label="human")+ assert block.value == new_value++ # cleanup+ client.agents.delete(agent.id)++def test_add_and_manage_tags_for_agent(client: LettaSDKClient):"""Comprehensive happy path test for adding, retrieving, and managing tags on an agent.