Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
chore: bump v0.8.17 (#2725)
cthomas2d00ac423c4b
diff --git a/letta/__init__.py b/letta/__init__.pyindex c78496a64..a2cca7dfd 100644--- a/letta/__init__.py+++ b/letta/__init__.py@@ -5,7 +5,7 @@ try:__version__ = version("letta")except PackageNotFoundError:# Fallback for development installations- __version__ = "0.8.16"+ __version__ = "0.8.17"if os.environ.get("LETTA_VERSION"):__version__ = os.environ["LETTA_VERSION"]diff --git a/letta/schemas/letta_message.py b/letta/schemas/letta_message.pyindex d1e9b2a70..1e772191d 100644--- a/letta/schemas/letta_message.py+++ b/letta/schemas/letta_message.py@@ -63,13 +63,13 @@ class LettaMessage(BaseModel):dt = dt.replace(tzinfo=timezone.utc)return dt.isoformat(timespec="seconds")- @field_serializer("is_err", when_used="unless-none")- def serialize_is_err(self, value: bool | None, _info):+ @field_serializer("is_err", mode="wrap")+ def serialize_is_err(self, value: bool | None, handler, _info):"""Only serialize is_err field when it's True (for debugging purposes).When is_err is None or False, this field will be excluded from the JSON output."""- return value if value is True else None+ return handler(value) if value is True else Noneclass SystemMessage(LettaMessage):diff --git a/letta/schemas/message.py b/letta/schemas/message.pyindex ec690577a..73bf73db6 100644--- a/letta/schemas/message.py+++ b/letta/schemas/message.py@@ -269,6 +269,7 @@ class Message(BaseMessage):otid=otid,sender_id=self.sender_id,step_id=self.step_id,+ is_err=self.is_err,))# Otherwise, we may have a list of multiple types@@ -287,6 +288,7 @@ class Message(BaseMessage):otid=otid,sender_id=self.sender_id,step_id=self.step_id,+ is_err=self.is_err,))elif isinstance(content_part, ReasoningContent):@@ -301,6 +303,7 @@ class Message(BaseMessage):name=self.name,otid=otid,step_id=self.step_id,+ is_err=self.is_err,))elif isinstance(content_part, RedactedReasoningContent):@@ -315,6 +318,7 @@ class Message(BaseMessage):otid=otid,sender_id=self.sender_id,step_id=self.step_id,+ is_err=self.is_err,))elif isinstance(content_part, OmittedReasoningContent):@@ -328,6 +332,7 @@ class Message(BaseMessage):name=self.name,otid=otid,step_id=self.step_id,+ is_err=self.is_err,))else:@@ -355,6 +360,7 @@ class Message(BaseMessage):otid=otid,sender_id=self.sender_id,step_id=self.step_id,+ is_err=self.is_err,))else:@@ -371,6 +377,7 @@ class Message(BaseMessage):otid=otid,sender_id=self.sender_id,step_id=self.step_id,+ is_err=self.is_err,))elif self.role == MessageRole.tool:@@ -416,6 +423,7 @@ class Message(BaseMessage):otid=Message.generate_otid_from_id(self.id, len(messages)),sender_id=self.sender_id,step_id=self.step_id,+ is_err=self.is_err,))elif self.role == MessageRole.user:@@ -437,6 +445,7 @@ class Message(BaseMessage):otid=self.otid,sender_id=self.sender_id,step_id=self.step_id,+ is_err=self.is_err,))elif self.role == MessageRole.system:diff --git a/letta/server/db.py b/letta/server/db.pyindex 11365522f..2e6984a6a 100644--- a/letta/server/db.py+++ b/letta/server/db.py@@ -1,3 +1,4 @@+import asyncioimport osimport threadingimport time@@ -116,6 +117,13 @@ class DatabaseRegistry:self.config = LettaConfig.load()self.logger = get_logger(__name__)+ if settings.db_max_concurrent_sessions:+ self._db_semaphore = asyncio.Semaphore(settings.db_max_concurrent_sessions)+ self.logger.info(f"Initialized database throttling with max {settings.db_max_concurrent_sessions} concurrent sessions")+ else:+ self.logger.info("Database throttling is disabled")+ self._db_semaphore = None+def initialize_sync(self, force: bool = False) -> None:"""Initialize the synchronous database engine if not already initialized."""with self._lock:@@ -364,16 +372,33 @@ class DatabaseRegistry:@trace_method@asynccontextmanagerasync def async_session(self, name: str = "default") -> AsyncGenerator[AsyncSession, None]:- """Async context manager for database sessions."""- session_factory = self.get_async_session_factory(name)- if not session_factory:- raise ValueError(f"No async session factory found for '{name}' or async database is not configured")+ """Async context manager for database sessions with throttling."""+ if self._db_semaphore:+ async with self._db_semaphore:+ session_factory = self.get_async_session_factory(name)+ if not session_factory:+ raise ValueError(f"No async session factory found for '{name}' or async database is not configured")++ session = session_factory()+ try:+ yield session+ finally:+ await session.close()+ else:+ session_factory = self.get_async_session_factory(name)+ if not session_factory:+ raise ValueError(f"No async session factory found for '{name}' or async database is not configured")- session = session_factory()- try:- yield session- finally:- await session.close()+ session = session_factory()+ try:+ yield session+ finally:+ await session.close()++ @trace_method+ def session_caller_trace(self, caller_info: str):+ """Trace sync db caller information for debugging purposes."""+ pass # wrapper used for otel tracing only@trace_methoddef session_caller_trace(self, caller_info: str):diff --git a/letta/server/rest_api/streaming_response.py b/letta/server/rest_api/streaming_response.pyindex 4daf65a7f..344d067df 100644--- a/letta/server/rest_api/streaming_response.py+++ b/letta/server/rest_api/streaming_response.py@@ -125,14 +125,7 @@ class StreamingResponseWithStatusCode(StreamingResponse):if not isinstance(content, bytes):content = content.encode(self.charset)more_body = False- await send(- {- "type": "http.response.body",- "body": content,- "more_body": more_body,- }- )- raise Exception(f"An exception occurred mid-stream with status code {status_code}", detail={"content": content})+ raise Exception(f"An exception occurred mid-stream with status code {status_code} with content {content}")else:content = chunkdiff --git a/letta/server/rest_api/utils.py b/letta/server/rest_api/utils.pyindex 17077fe35..6b14ff4a8 100644--- a/letta/server/rest_api/utils.py+++ b/letta/server/rest_api/utils.py@@ -120,22 +120,22 @@ async def sse_async_generator(yield sse_formatter(usage.model_dump(exclude={"steps_messages"}))except ContextWindowExceededError as e:- log_error_to_sentry(e)+ capture_sentry_exception(e)logger.error(f"ContextWindowExceededError error: {e}")yield sse_formatter({"error": f"Stream failed: {e}", "code": str(e.code.value) if e.code else None})except RateLimitExceededError as e:- log_error_to_sentry(e)+ capture_sentry_exception(e)logger.error(f"RateLimitExceededError error: {e}")yield sse_formatter({"error": f"Stream failed: {e}", "code": str(e.code.value) if e.code else None})except Exception as e:- log_error_to_sentry(e)+ capture_sentry_exception(e)logger.error(f"Caught unexpected Exception: {e}")yield sse_formatter({"error": "Stream failed (internal error occurred)"})except Exception as e:- log_error_to_sentry(e)+ capture_sentry_exception(e)logger.error(f"Caught unexpected Exception: {e}")yield sse_formatter({"error": "Stream failed (decoder encountered an error)"})diff --git a/letta/services/agent_file_manager.py b/letta/services/agent_file_manager.pyindex da2789122..78f34b896 100644--- a/letta/services/agent_file_manager.py+++ b/letta/services/agent_file_manager.py@@ -149,7 +149,7 @@ class AgentFileManager:return sorted(unique_blocks.values(), key=lambda x: x.label)async def _extract_unique_sources_and_files_from_agents(- self, agent_states: List[AgentState], actor: User+ self, agent_states: List[AgentState], actor: User, files_agents_cache: dict = None) -> tuple[List[Source], List[FileMetadata]]:"""Extract unique sources and files from agent states using bulk operations"""@@ -160,6 +160,10 @@ class AgentFileManager:files_agents = await self.file_agent_manager.list_files_for_agent(agent_id=agent_state.id, actor=actor, is_open_only=False, return_as_blocks=False)+ # cache the results for reuse during conversion+ if files_agents_cache is not None:+ files_agents_cache[agent_state.id] = files_agents+for file_agent in files_agents:all_source_ids.add(file_agent.source_id)all_file_ids.add(file_agent.file_id)@@ -168,13 +172,18 @@ class AgentFileManager:return sources, files- async def _convert_agent_state_to_schema(self, agent_state: AgentState, actor: User) -> AgentSchema:+ async def _convert_agent_state_to_schema(self, agent_state: AgentState, actor: User, files_agents_cache: dict = None) -> AgentSchema:"""Convert AgentState to AgentSchema with ID remapping"""agent_file_id = self._map_db_to_file_id(agent_state.id, AgentSchema.__id_prefix__)- files_agents = await self.file_agent_manager.list_files_for_agent(- agent_id=agent_state.id, actor=actor, is_open_only=False, return_as_blocks=False- )++ # use cached file-agent data if available, otherwise fetch+ if files_agents_cache is not None and agent_state.id in files_agents_cache:+ files_agents = files_agents_cache[agent_state.id]+ else:+ files_agents = await self.file_agent_manager.list_files_for_agent(+ agent_id=agent_state.id, actor=actor, is_open_only=False, return_as_blocks=False+ )agent_schema = await AgentSchema.from_agent_state(agent_state, message_manager=self.message_manager, files_agents=files_agents, actor=actor)@@ -264,15 +273,21 @@ class AgentFileManager:missing_ids = [agent_id for agent_id in agent_ids if agent_id not in found_ids]raise AgentFileExportError(f"The following agent IDs were not found: {missing_ids}")+ # cache for file-agent relationships to avoid duplicate queries+ files_agents_cache = {} # Maps agent_id to list of file_agent relationships+# Extract unique entities across all agentstool_set = self._extract_unique_tools(agent_states)block_set = self._extract_unique_blocks(agent_states)- # Extract sources and files from agent states BEFORE conversion- source_set, file_set = await self._extract_unique_sources_and_files_from_agents(agent_states, actor)+ # Extract sources and files from agent states BEFORE conversion (with caching)+ source_set, file_set = await self._extract_unique_sources_and_files_from_agents(agent_states, actor, files_agents_cache)- # Convert to schemas with ID remapping- agent_schemas = [await self._convert_agent_state_to_schema(agent_state, actor=actor) for agent_state in agent_states]+ # Convert to schemas with ID remapping (reusing cached file-agent data)+ agent_schemas = [+ await self._convert_agent_state_to_schema(agent_state, actor=actor, files_agents_cache=files_agents_cache)+ for agent_state in agent_states+ ]tool_schemas = [self._convert_tool_to_schema(tool) for tool in tool_set]block_schemas = [self._convert_block_to_schema(block) for block in block_set]source_schemas = [self._convert_source_to_schema(source) for source in source_set]@@ -332,32 +347,66 @@ class AgentFileManager:# Import in dependency orderimported_count = 0file_to_db_ids = {} # Maps file IDs to new database IDs-- # 1. Create tools first (no dependencies)- for tool_schema in schema.tools:- # Convert ToolSchema back to ToolCreate- created_tool = await self.tool_manager.create_or_update_tool_async(- pydantic_tool=Tool(**tool_schema.model_dump(exclude={"id"})), actor=actor- )- file_to_db_ids[tool_schema.id] = created_tool.id- imported_count += 1-- # 2. Create blocks (no dependencies)- for block_schema in schema.blocks:- # Convert BlockSchema back to CreateBlock- block = Block(**block_schema.model_dump(exclude={"id"}))- created_block = await self.block_manager.create_or_update_block_async(block, actor)- file_to_db_ids[block_schema.id] = created_block.id- imported_count += 1-- # 3. Create sources (no dependencies)- for source_schema in schema.sources:- # Convert SourceSchema back to Source- source_data = source_schema.model_dump(exclude={"id", "embedding", "embedding_chunk_size"})- source = Source(**source_data)- created_source = await self.source_manager.create_source(source, actor)- file_to_db_ids[source_schema.id] = created_source.id- imported_count += 1+ # in-memory cache for file metadata to avoid repeated db calls+ file_metadata_cache = {} # Maps database file ID to FileMetadata++ # 1. Create tools first (no dependencies) - using bulk upsert for efficiency+ if schema.tools:+ # convert tool schemas to pydantic tools+ pydantic_tools = []+ for tool_schema in schema.tools:+ pydantic_tools.append(Tool(**tool_schema.model_dump(exclude={"id"})))++ # bulk upsert all tools at once+ created_tools = await self.tool_manager.bulk_upsert_tools_async(pydantic_tools, actor)++ # map file ids to database ids+ # note: tools are matched by name during upsert, so we need to match by name here too+ created_tools_by_name = {tool.name: tool for tool in created_tools}+ for tool_schema in schema.tools:+ created_tool = created_tools_by_name.get(tool_schema.name)+ if created_tool:+ file_to_db_ids[tool_schema.id] = created_tool.id+ imported_count += 1+ else:+ logger.warning(f"Tool {tool_schema.name} was not created during bulk upsert")++ # 2. Create blocks (no dependencies) - using batch create for efficiency+ if schema.blocks:+ # convert block schemas to pydantic blocks (excluding IDs to create new blocks)+ pydantic_blocks = []+ for block_schema in schema.blocks:+ pydantic_blocks.append(Block(**block_schema.model_dump(exclude={"id"})))++ # batch create all blocks at once+ created_blocks = await self.block_manager.batch_create_blocks_async(pydantic_blocks, actor)++ # map file ids to database ids+ for block_schema, created_block in zip(schema.blocks, created_blocks):+ file_to_db_ids[block_schema.id] = created_block.id+ imported_count += 1++ # 3. Create sources (no dependencies) - using bulk upsert for efficiency+ if schema.sources:+ # convert source schemas to pydantic sources+ pydantic_sources = []+ for source_schema in schema.sources:+ source_data = source_schema.model_dump(exclude={"id", "embedding", "embedding_chunk_size"})+ pydantic_sources.append(Source(**source_data))++ # bulk upsert all sources at once+ created_sources = await self.source_manager.bulk_upsert_sources_async(pydantic_sources, actor)++ # map file ids to database ids+ # note: sources are matched by name during upsert, so we need to match by name here too+ created_sources_by_name = {source.name: source for source in created_sources}+ for source_schema in schema.sources:+ created_source = created_sources_by_name.get(source_schema.name)+ if created_source:+ file_to_db_ids[source_schema.id] = created_source.id+ imported_count += 1+ else:+ logger.warning(f"Source {source_schema.name} was not created during bulk upsert")# 4. Create files (depends on sources)for file_schema in schema.files:@@ -383,8 +432,10 @@ class AgentFileManager:file_db_id = file_to_db_ids[file_schema.id]source_db_id = file_to_db_ids[file_schema.source_id]- # Get the created file metadata- file_metadata = await self.file_manager.get_file_by_id(file_db_id, actor)+ # Get the created file metadata (with caching)+ if file_db_id not in file_metadata_cache:+ file_metadata_cache[file_db_id] = await self.file_manager.get_file_by_id(file_db_id, actor)+ file_metadata = file_metadata_cache[file_db_id]# Save the db call of fetching content againfile_metadata.content = file_schema.content@@ -447,7 +498,11 @@ class AgentFileManager:for file_agent_schema in agent_schema.files_agents:file_db_id = file_to_db_ids[file_agent_schema.file_id]- file_metadata = await self.file_manager.get_file_by_id(file_db_id, actor)++ # Use cached file metadata if available+ if file_db_id not in file_metadata_cache:+ file_metadata_cache[file_db_id] = await self.file_manager.get_file_by_id(file_db_id, actor)+ file_metadata = file_metadata_cache[file_db_id]files_for_agent.append(file_metadata)if file_agent_schema.visible_content:@@ -530,9 +585,16 @@ class AgentFileManager:entity_ids = [entity.id for entity in entities]# Check for duplicates within this entity type- if len(entity_ids) != len(set(entity_ids)):- duplicates = [id for id in entity_ids if entity_ids.count(id) > 1]- errors.append(f"Duplicate {entity_type} IDs found: {set(duplicates)}")+ seen = set()+ duplicates = set()+ for entity_id in entity_ids:+ if entity_id in seen:+ duplicates.add(entity_id)+ else:+ seen.add(entity_id)++ if duplicates:+ errors.append(f"Duplicate {entity_type} IDs found: {duplicates}")# Check for duplicates across all entity typesfor entity_id in entity_ids:@@ -545,9 +607,16 @@ class AgentFileManager:message_ids = [msg.id for msg in agent.messages]# Check for duplicates within agent messages- if len(message_ids) != len(set(message_ids)):- duplicates = [id for id in message_ids if message_ids.count(id) > 1]- errors.append(f"Duplicate message IDs in agent {agent.id}: {set(duplicates)}")+ seen = set()+ duplicates = set()+ for message_id in message_ids:+ if message_id in seen:+ duplicates.add(message_id)+ else:+ seen.add(message_id)++ if duplicates:+ errors.append(f"Duplicate message IDs in agent {agent.id}: {duplicates}")# Check for duplicates across all entity typesfor message_id in message_ids:diff --git a/letta/services/source_manager.py b/letta/services/source_manager.pyindex e3287130c..967869199 100644--- a/letta/services/source_manager.py+++ b/letta/services/source_manager.py@@ -58,6 +58,123 @@ class SourceManager:await source.create_async(session, actor=actor)return source.to_pydantic()+ @enforce_types+ @trace_method+ async def bulk_upsert_sources_async(self, pydantic_sources: List[PydanticSource], actor: PydanticUser) -> List[PydanticSource]:+ """+ Bulk create or update multiple sources in a single database transaction.++ Uses optimized PostgreSQL bulk upsert when available, falls back to individual+ upserts for SQLite. This is much more efficient than calling create_source+ in a loop.++ IMPORTANT BEHAVIOR NOTES:+ - Sources are matched by (name, organization_id) unique constraint, NOT by ID+ - If a source with the same name already exists for the organization, it will be updated+ regardless of any ID provided in the input source+ - The existing source's ID is preserved during updates+ - If you provide a source with an explicit ID but a name that matches an existing source,+ the existing source will be updated and the provided ID will be ignored+ - This matches the behavior of create_source which also checks by ID first++ PostgreSQL optimization:+ - Uses native ON CONFLICT (name, organization_id) DO UPDATE for atomic upserts+ - All sources are processed in a single SQL statement for maximum efficiency++ SQLite fallback:+ - Falls back to individual create_source calls+ - Still benefits from batched transaction handling++ Args:+ pydantic_sources: List of sources to create or update+ actor: User performing the action++ Returns:+ List of created/updated sources+ """+ if not pydantic_sources:+ return []++ from letta.settings import settings++ if settings.letta_pg_uri_no_default:+ # use optimized postgresql bulk upsert+ async with db_registry.async_session() as session:+ return await self._bulk_upsert_postgresql(session, pydantic_sources, actor)+ else:+ # fallback to individual upserts for sqlite+ return await self._upsert_sources_individually(pydantic_sources, actor)++ @trace_method+ async def _bulk_upsert_postgresql(self, session, source_data_list: List[PydanticSource], actor: PydanticUser) -> List[PydanticSource]:+ """Hyper-optimized PostgreSQL bulk upsert using ON CONFLICT DO UPDATE."""+ from sqlalchemy import func, select+ from sqlalchemy.dialects.postgresql import insert++ # prepare data for bulk insert+ table = SourceModel.__table__+ valid_columns = {col.name for col in table.columns}++ insert_data = []+ for source in source_data_list:+ source_dict = source.model_dump(to_orm=True)+ # set created/updated by fields++ if actor:+ source_dict["_created_by_id"] = actor.id+ source_dict["_last_updated_by_id"] = actor.id+ source_dict["organization_id"] = actor.organization_id++ # filter to only include columns that exist in the table+ filtered_dict = {k: v for k, v in source_dict.items() if k in valid_columns}+ insert_data.append(filtered_dict)++ # use postgresql's native bulk upsert+ stmt = insert(table).values(insert_data)++ # on conflict, update all columns except id, created_at, and _created_by_id+ excluded = stmt.excluded+ update_dict = {}+ for col in table.columns:+ if col.name not in ("id", "created_at", "_created_by_id"):+ if col.name == "updated_at":+ update_dict[col.name] = func.now()+ else:+ update_dict[col.name] = excluded[col.name]++ upsert_stmt = stmt.on_conflict_do_update(index_elements=["name", "organization_id"], set_=update_dict)++ await session.execute(upsert_stmt)+ await session.commit()++ # fetch results+ source_names = [source.name for source in source_data_list]+ result_query = select(SourceModel).where(+ SourceModel.name.in_(source_names), SourceModel.organization_id == actor.organization_id, SourceModel.is_deleted == False+ )+ result = await session.execute(result_query)+ return [source.to_pydantic() for source in result.scalars()]++ @trace_method+ async def _upsert_sources_individually(self, source_data_list: List[PydanticSource], actor: PydanticUser) -> List[PydanticSource]:+ """Fallback to individual upserts for SQLite."""+ sources = []+ for source in source_data_list:+ # try to get existing source by name+ existing_source = await self.get_source_by_name(source.name, actor)+ if existing_source:+ # update existing source+ from letta.schemas.source import SourceUpdate++ update_data = source.model_dump(exclude={"id"}, exclude_none=True)+ updated_source = await self.update_source(existing_source.id, SourceUpdate(**update_data), actor)+ sources.append(updated_source)+ else:+ # create new source+ created_source = await self.create_source(source, actor)+ sources.append(created_source)+ return sources+@enforce_types@trace_methodasync def update_source(self, source_id: str, source_update: SourceUpdate, actor: PydanticUser) -> PydanticSource:diff --git a/letta/services/tool_executor/builtin_tool_executor.py b/letta/services/tool_executor/builtin_tool_executor.pyindex d8cd24a6b..33120a5ea 100644--- a/letta/services/tool_executor/builtin_tool_executor.py+++ b/letta/services/tool_executor/builtin_tool_executor.py@@ -176,7 +176,9 @@ class LettaBuiltinToolExecutor(ToolExecutor):app = AsyncFirecrawlApp(api_key=firecrawl_api_key)# Process all search tasks in parallel- search_task_coroutines = [self._process_single_search_task(app, task, limit, return_raw, api_key_source) for task in search_tasks]+ search_task_coroutines = [+ self._process_single_search_task(app, task, limit, return_raw, api_key_source, agent_state) for task in search_tasks+ ]# Execute all searches concurrentlysearch_results = await asyncio.gather(*search_task_coroutines, return_exceptions=True)@@ -205,7 +207,7 @@ class LettaBuiltinToolExecutor(ToolExecutor):@trace_methodasync def _process_single_search_task(- self, app: "AsyncFirecrawlApp", task: SearchTask, limit: int, return_raw: bool, api_key_source: str+ self, app: "AsyncFirecrawlApp", task: SearchTask, limit: int, return_raw: bool, api_key_source: str, agent_state: "AgentState") -> Dict[str, Any]:"""Process a single search task."""from firecrawl import ScrapeOptions@@ -246,7 +248,9 @@ class LettaBuiltinToolExecutor(ToolExecutor):for result in search_result.get("data"):if result.get("markdown"):# Create async task for OpenAI analysis- analysis_task = self._analyze_document_with_openai(client, result["markdown"], task.query, task.question)+ analysis_task = self._analyze_document_with_openai(+ client, result["markdown"], task.query, task.question, agent_state+ )analysis_tasks.append(analysis_task)results_with_markdown.append(result)else:@@ -300,7 +304,9 @@ class LettaBuiltinToolExecutor(ToolExecutor):return {"query": task.query, "question": task.question, "raw_results": search_result}@trace_method- async def _analyze_document_with_openai(self, client, markdown_content: str, query: str, question: str) -> Optional[DocumentAnalysis]:+ async def _analyze_document_with_openai(+ self, client, markdown_content: str, query: str, question: str, agent_state: "AgentState"+ ) -> Optional[DocumentAnalysis]:"""Use OpenAI to analyze a document and extract relevant passages using line numbers."""original_length = len(markdown_content)@@ -324,7 +330,9 @@ class LettaBuiltinToolExecutor(ToolExecutor):# Time the OpenAI requeststart_time = time.time()- model = os.getenv(WEB_SEARCH_MODEL_ENV_VAR_NAME, WEB_SEARCH_MODEL_ENV_VAR_DEFAULT_VALUE)+ # Check agent state env vars first, then fall back to os.getenv+ agent_state_tool_env_vars = agent_state.get_agent_env_vars_as_dict()+ model = agent_state_tool_env_vars.get(WEB_SEARCH_MODEL_ENV_VAR_NAME) or WEB_SEARCH_MODEL_ENV_VAR_DEFAULT_VALUElogger.info(f"Using model {model} for web search result parsing")response = await client.beta.chat.completions.parse(model=model,diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.pyindex 132540496..b4af8d4b2 100644--- a/letta/services/tool_manager.py+++ b/letta/services/tool_manager.py@@ -176,6 +176,56 @@ class ToolManager:await tool.create_async(session, actor=actor) # Re-raise other database-related errorsreturn tool.to_pydantic()+ @enforce_types+ @trace_method+ async def bulk_upsert_tools_async(self, pydantic_tools: List[PydanticTool], actor: PydanticUser) -> List[PydanticTool]:+ """+ Bulk create or update multiple tools in a single database transaction.++ Uses optimized PostgreSQL bulk upsert when available, falls back to individual+ upserts for SQLite. This is much more efficient than calling create_or_update_tool_async+ in a loop.++ IMPORTANT BEHAVIOR NOTES:+ - Tools are matched by (name, organization_id) unique constraint, NOT by ID+ - If a tool with the same name already exists for the organization, it will be updated+ regardless of any ID provided in the input tool+ - The existing tool's ID is preserved during updates+ - If you provide a tool with an explicit ID but a name that matches an existing tool,+ the existing tool will be updated and the provided ID will be ignored+ - This matches the behavior of create_or_update_tool_async which also matches by name++ PostgreSQL optimization:+ - Uses native ON CONFLICT (name, organization_id) DO UPDATE for atomic upserts+ - All tools are processed in a single SQL statement for maximum efficiency++ SQLite fallback:+ - Falls back to individual create_or_update_tool_async calls+ - Still benefits from batched transaction handling++ Args:+ pydantic_tools: List of tools to create or update+ actor: User performing the action++ Returns:+ List of created/updated tools+ """+ if not pydantic_tools:+ return []++ # auto-generate descriptions if not provided+ for tool in pydantic_tools:+ if tool.description is None:+ tool.description = tool.json_schema.get("description", None)++ if settings.letta_pg_uri_no_default:+ # use optimized postgresql bulk upsert+ async with db_registry.async_session() as session:+ return await self._bulk_upsert_postgresql(session, pydantic_tools, actor)+ else:+ # fallback to individual upserts for sqlite+ return await self._upsert_tools_individually(pydantic_tools, actor)+@enforce_types@trace_methoddef get_tool_by_id(self, tool_id: str, actor: PydanticUser) -> PydanticTool:diff --git a/letta/settings.py b/letta/settings.pyindex e438faa21..619fd466d 100644--- a/letta/settings.py+++ b/letta/settings.py@@ -214,6 +214,7 @@ class Settings(BaseSettings):pool_pre_ping: bool = True # Pre ping to check for dead connectionspool_use_lifo: bool = Truedisable_sqlalchemy_pooling: bool = False+ db_max_concurrent_sessions: Optional[int] = Noneredis_host: Optional[str] = Field(default=None, description="Host for Redis instance")redis_port: Optional[int] = Field(default=6379, description="Port for Redis instance")diff --git a/pyproject.toml b/pyproject.tomlindex 0e1070044..c1e25af1e 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -1,6 +1,6 @@[tool.poetry]name = "letta"-version = "0.8.16"+version = "0.8.17"packages = [{include = "letta"},]diff --git a/tests/integration_test_builtin_tools.py b/tests/integration_test_builtin_tools.pyindex 4e8538257..83af3608e 100644--- a/tests/integration_test_builtin_tools.py+++ b/tests/integration_test_builtin_tools.py@@ -321,6 +321,7 @@ async def test_web_search_uses_agent_env_var_model():patch("openai.AsyncOpenAI") as mock_openai_class,patch("letta.services.tool_executor.builtin_tool_executor.model_settings") as mock_model_settings,patch.dict(os.environ, {WEB_SEARCH_MODEL_ENV_VAR_NAME: "gpt-4o"}),+ patch("firecrawl.AsyncFirecrawlApp") as mock_firecrawl_class,):# setup mocks@@ -330,6 +331,23 @@ async def test_web_search_uses_agent_env_var_model():mock_openai_class.return_value = mock_openai_clientmock_openai_client.beta.chat.completions.parse.return_value = mock_openai_response+ # Mock Firecrawl+ mock_firecrawl_app = AsyncMock()+ mock_firecrawl_class.return_value = mock_firecrawl_app++ # Mock search results with markdown content+ mock_search_result = {+ "data": [+ {+ "url": "https://example.com/test",+ "title": "Test Result",+ "description": "Test description",+ "markdown": "This is test markdown content for the search result.",+ }+ ]+ }+ mock_firecrawl_app.search.return_value = mock_search_result+# create executor with mock dependenciesexecutor = LettaBuiltinToolExecutor(message_manager=MagicMock(),diff --git a/tests/test_managers.py b/tests/test_managers.pyindex 7fac3af35..ffc583c05 100644--- a/tests/test_managers.py+++ b/tests/test_managers.py@@ -3290,6 +3290,190 @@ async def test_upsert_base_tools_with_empty_type_filter(server: SyncServer, defaassert tools == []+async def test_bulk_upsert_tools_async(server: SyncServer, default_user):+ """Test bulk upserting multiple tools at once"""+ # create multiple test tools+ tools_data = []+ for i in range(5):+ tool = PydanticTool(+ name=f"bulk_test_tool_{i}",+ description=f"Test tool {i} for bulk operations",+ tags=["bulk", "test"],+ source_code=f"def bulk_test_tool_{i}():\n '''Test tool {i} function'''\n return 'result_{i}'",+ source_type="python",+ )+ tools_data.append(tool)++ # initial bulk upsert - should create all tools+ created_tools = await server.tool_manager.bulk_upsert_tools_async(tools_data, default_user)+ assert len(created_tools) == 5+ assert all(t.name.startswith("bulk_test_tool_") for t in created_tools)+ assert all(t.description for t in created_tools)++ # verify all tools were created+ for i in range(5):+ tool = await server.tool_manager.get_tool_by_name_async(f"bulk_test_tool_{i}", default_user)+ assert tool is not None+ assert tool.description == f"Test tool {i} for bulk operations"++ # modify some tools and upsert again - should update existing tools+ tools_data[0].description = "Updated description for tool 0"+ tools_data[2].tags = ["bulk", "test", "updated"]++ updated_tools = await server.tool_manager.bulk_upsert_tools_async(tools_data, default_user)+ assert len(updated_tools) == 5++ # verify updates were applied+ tool_0 = await server.tool_manager.get_tool_by_name_async("bulk_test_tool_0", default_user)+ assert tool_0.description == "Updated description for tool 0"++ tool_2 = await server.tool_manager.get_tool_by_name_async("bulk_test_tool_2", default_user)+ assert "updated" in tool_2.tags++ # test with empty list+ empty_result = await server.tool_manager.bulk_upsert_tools_async([], default_user)+ assert empty_result == []++ # test with tools missing descriptions (should auto-generate from json schema)+ no_desc_tool = PydanticTool(+ name="no_description_tool",+ tags=["test"],+ source_code="def no_description_tool():\n '''This is a docstring description'''\n return 'result'",+ source_type="python",+ )+ result = await server.tool_manager.bulk_upsert_tools_async([no_desc_tool], default_user)+ assert len(result) == 1+ assert result[0].description is not None # should be auto-generated from docstring+++async def test_bulk_upsert_tools_name_conflict(server: SyncServer, default_user):+ """Test bulk upserting tools handles name+org_id unique constraint correctly"""++ # create a tool with a specific name+ original_tool = PydanticTool(+ name="unique_name_tool",+ description="Original description",+ tags=["original"],+ source_code="def unique_name_tool():\n '''Original function'''\n return 'original'",+ source_type="python",+ )++ # create it+ created = await server.tool_manager.create_tool_async(original_tool, default_user)+ original_id = created.id++ # now try to bulk upsert with same name but different id+ conflicting_tool = PydanticTool(+ name="unique_name_tool", # same name+ description="Updated via bulk upsert",+ tags=["updated", "bulk"],+ source_code="def unique_name_tool():\n '''Updated function'''\n return 'updated'",+ source_type="python",+ )++ # bulk upsert should update the existing tool based on name conflict+ result = await server.tool_manager.bulk_upsert_tools_async([conflicting_tool], default_user)+ assert len(result) == 1+ assert result[0].name == "unique_name_tool"+ assert result[0].description == "Updated via bulk upsert"+ assert "updated" in result[0].tags+ assert "bulk" in result[0].tags++ # verify only one tool exists with this name+ all_tools = await server.tool_manager.list_tools_async(actor=default_user)+ tools_with_name = [t for t in all_tools if t.name == "unique_name_tool"]+ assert len(tools_with_name) == 1++ # the id should remain the same as the original+ assert tools_with_name[0].id == original_id+++async def test_bulk_upsert_tools_mixed_create_update(server: SyncServer, default_user):+ """Test bulk upserting with mix of new tools and updates to existing ones"""++ # create some existing tools+ existing_tools = []+ for i in range(3):+ tool = PydanticTool(+ name=f"existing_tool_{i}",+ description=f"Existing tool {i}",+ tags=["existing"],+ source_code=f"def existing_tool_{i}():\n '''Existing {i}'''\n return 'existing_{i}'",+ source_type="python",+ )+ created = await server.tool_manager.create_tool_async(tool, default_user)+ existing_tools.append(created)++ # prepare bulk upsert with mix of updates and new tools+ bulk_tools = []++ # update existing tool 0 by name+ bulk_tools.append(+ PydanticTool(+ name="existing_tool_0", # matches by name+ description="Updated existing tool 0",+ tags=["existing", "updated"],+ source_code="def existing_tool_0():\n '''Updated 0'''\n return 'updated_0'",+ source_type="python",+ )+ )++ # update existing tool 1 by name (since bulk upsert matches by name, not id)+ bulk_tools.append(+ PydanticTool(+ name="existing_tool_1", # matches by name+ description="Updated existing tool 1",+ tags=["existing", "updated"],+ source_code="def existing_tool_1():\n '''Updated 1'''\n return 'updated_1'",+ source_type="python",+ )+ )++ # add completely new tools+ for i in range(3, 6):+ bulk_tools.append(+ PydanticTool(+ name=f"new_tool_{i}",+ description=f"New tool {i}",+ tags=["new"],+ source_code=f"def new_tool_{i}():\n '''New {i}'''\n return 'new_{i}'",+ source_type="python",+ )+ )++ # perform bulk upsert+ result = await server.tool_manager.bulk_upsert_tools_async(bulk_tools, default_user)+ assert len(result) == 5 # 2 updates + 3 new++ # verify updates+ tool_0 = await server.tool_manager.get_tool_by_name_async("existing_tool_0", default_user)+ assert tool_0.description == "Updated existing tool 0"+ assert "updated" in tool_0.tags+ assert tool_0.id == existing_tools[0].id # id should remain same++ # verify tool 1 was updated+ tool_1 = await server.tool_manager.get_tool_by_id_async(existing_tools[1].id, default_user)+ assert tool_1.name == "existing_tool_1" # name stays same+ assert tool_1.description == "Updated existing tool 1"+ assert "updated" in tool_1.tags++ # verify new tools were created+ for i in range(3, 6):+ new_tool = await server.tool_manager.get_tool_by_name_async(f"new_tool_{i}", default_user)+ assert new_tool is not None+ assert new_tool.description == f"New tool {i}"+ assert "new" in new_tool.tags++ # verify existing_tool_2 was not affected+ tool_2 = await server.tool_manager.get_tool_by_id_async(existing_tools[2].id, default_user)+ assert tool_2.name == "existing_tool_2"+ assert tool_2.description == "Existing tool 2"+ assert tool_2.tags == ["existing"]++@pytest.mark.asyncioasync def test_create_tool_with_pip_requirements(server: SyncServer, default_user, default_organization):def test_tool_with_deps():@@ -3667,6 +3851,71 @@ def test_create_block(server: SyncServer, default_user):assert block.metadata == block_create.metadata+async def test_batch_create_blocks_async(server: SyncServer, default_user):+ """Test batch creating multiple blocks at once"""+ block_manager = BlockManager()++ # create multiple test blocks+ blocks_data = []+ for i in range(5):+ block = PydanticBlock(+ label=f"test_block_{i}",+ is_template=False,+ value=f"Content for block {i}",+ description=f"Test block {i} for batch operations",+ limit=1000 + i * 100, # varying limits+ metadata={"index": i, "batch": "test"},+ )+ blocks_data.append(block)++ # batch create all blocks at once+ created_blocks = await block_manager.batch_create_blocks_async(blocks_data, default_user)++ # verify all blocks were created+ assert len(created_blocks) == 5+ assert all(b.label.startswith("test_block_") for b in created_blocks)++ # verify block properties were preserved+ for i, block in enumerate(created_blocks):+ assert block.label == f"test_block_{i}"+ assert block.value == f"Content for block {i}"+ assert block.description == f"Test block {i} for batch operations"+ assert block.limit == 1000 + i * 100+ assert block.metadata["index"] == i+ assert block.metadata["batch"] == "test"+ assert block.id is not None # should have generated ids+ # blocks have organization_id at the orm level, not in the pydantic model++ # verify blocks can be retrieved individually+ for created_block in created_blocks:+ retrieved = await block_manager.get_block_by_id_async(created_block.id, default_user)+ assert retrieved.id == created_block.id+ assert retrieved.label == created_block.label+ assert retrieved.value == created_block.value++ # test with empty list+ empty_result = await block_manager.batch_create_blocks_async([], default_user)+ assert empty_result == []++ # test creating blocks with same labels (should create separate blocks since no unique constraint)+ duplicate_blocks = [+ PydanticBlock(label="duplicate_label", value="Block 1"),+ PydanticBlock(label="duplicate_label", value="Block 2"),+ PydanticBlock(label="duplicate_label", value="Block 3"),+ ]++ created_duplicates = await block_manager.batch_create_blocks_async(duplicate_blocks, default_user)+ assert len(created_duplicates) == 3+ assert all(b.label == "duplicate_label" for b in created_duplicates)+ # all should have different ids+ ids = [b.id for b in created_duplicates]+ assert len(set(ids)) == 3 # all unique ids+ # but different values+ values = [b.value for b in created_duplicates]+ assert set(values) == {"Block 1", "Block 2", "Block 3"}++@pytest.mark.asyncioasync def test_get_blocks(server, default_user, event_loop):block_manager = BlockManager()@@ -5013,6 +5262,164 @@ async def test_update_source_no_changes(server: SyncServer, default_user):assert updated_source.description == source.description+async def test_bulk_upsert_sources_async(server: SyncServer, default_user):+ """Test bulk upserting sources."""+ sources_data = [+ PydanticSource(+ name="Bulk Source 1",+ description="First bulk source",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ PydanticSource(+ name="Bulk Source 2",+ description="Second bulk source",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ PydanticSource(+ name="Bulk Source 3",+ description="Third bulk source",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ ]++ # Bulk upsert sources+ created_sources = await server.source_manager.bulk_upsert_sources_async(sources_data, default_user)++ # Verify all sources were created+ assert len(created_sources) == 3++ # Verify source details+ created_names = {source.name for source in created_sources}+ expected_names = {"Bulk Source 1", "Bulk Source 2", "Bulk Source 3"}+ assert created_names == expected_names++ # Verify organization assignment+ for source in created_sources:+ assert source.organization_id == default_user.organization_id+++async def test_bulk_upsert_sources_name_conflict(server: SyncServer, default_user):+ """Test bulk upserting sources with name conflicts."""+ # Create an existing source+ existing_source = await server.source_manager.create_source(+ PydanticSource(+ name="Existing Source",+ description="Already exists",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ default_user,+ )++ # Try to bulk upsert with the same name+ sources_data = [+ PydanticSource(+ name="Existing Source", # Same name as existing+ description="Updated description",+ metadata={"updated": True},+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ PydanticSource(+ name="New Bulk Source",+ description="Completely new",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ ]++ # Bulk upsert should update existing and create new+ result_sources = await server.source_manager.bulk_upsert_sources_async(sources_data, default_user)++ # Should return 2 sources+ assert len(result_sources) == 2++ # Find the updated source+ updated_source = next(s for s in result_sources if s.name == "Existing Source")++ # Verify the existing source was updated, not replaced+ assert updated_source.id == existing_source.id # ID should be preserved+ assert updated_source.description == "Updated description"+ assert updated_source.metadata == {"updated": True}++ # Verify new source was created+ new_source = next(s for s in result_sources if s.name == "New Bulk Source")+ assert new_source.description == "Completely new"+++async def test_bulk_upsert_sources_mixed_create_update(server: SyncServer, default_user):+ """Test bulk upserting with a mix of creates and updates."""+ # Create some existing sources+ existing1 = await server.source_manager.create_source(+ PydanticSource(+ name="Mixed Source 1",+ description="Original 1",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ default_user,+ )+ existing2 = await server.source_manager.create_source(+ PydanticSource(+ name="Mixed Source 2",+ description="Original 2",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ default_user,+ )++ # Bulk upsert with updates and new sources+ sources_data = [+ PydanticSource(+ name="Mixed Source 1", # Update existing+ description="Updated 1",+ instructions="New instructions 1",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ PydanticSource(+ name="Mixed Source 3", # Create new+ description="New 3",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ PydanticSource(+ name="Mixed Source 2", # Update existing+ description="Updated 2",+ metadata={"version": 2},+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ PydanticSource(+ name="Mixed Source 4", # Create new+ description="New 4",+ embedding_config=DEFAULT_EMBEDDING_CONFIG,+ ),+ ]++ # Perform bulk upsert+ result_sources = await server.source_manager.bulk_upsert_sources_async(sources_data, default_user)++ # Should return 4 sources+ assert len(result_sources) == 4++ # Verify updates preserved IDs+ source1 = next(s for s in result_sources if s.name == "Mixed Source 1")+ assert source1.id == existing1.id+ assert source1.description == "Updated 1"+ assert source1.instructions == "New instructions 1"++ source2 = next(s for s in result_sources if s.name == "Mixed Source 2")+ assert source2.id == existing2.id+ assert source2.description == "Updated 2"+ assert source2.metadata == {"version": 2}++ # Verify new sources were created+ source3 = next(s for s in result_sources if s.name == "Mixed Source 3")+ assert source3.description == "New 3"+ assert source3.id != existing1.id and source3.id != existing2.id++ source4 = next(s for s in result_sources if s.name == "Mixed Source 4")+ assert source4.description == "New 4"+ assert source4.id != existing1.id and source4.id != existing2.id++# ======================================================================================================================# Source Manager Tests - Files# ======================================================================================================================@@ -5554,95 +5961,6 @@ async def test_get_organization_sources_metadata(server, default_user):assert file3_stats.file_size == 512-async def test_get_organization_sources_metadata(server, default_user):- """Test getting organization sources metadata with aggregated file information."""- # Create test sources- source1 = await server.source_manager.create_source(- source=PydanticSource(- name="test_source_1",- embedding_config=DEFAULT_EMBEDDING_CONFIG,- ),- actor=default_user,- )-- source2 = await server.source_manager.create_source(- source=PydanticSource(- name="test_source_2",- embedding_config=DEFAULT_EMBEDDING_CONFIG,- ),- actor=default_user,- )-- # Create test files for source1- file1_meta = PydanticFileMetadata(- source_id=source1.id,- file_name="file1.txt",- file_type="text/plain",- file_size=1024,- )- file1 = await server.file_manager.create_file(file_metadata=file1_meta, actor=default_user)-- file2_meta = PydanticFileMetadata(- source_id=source1.id,- file_name="file2.txt",- file_type="text/plain",- file_size=2048,- )- file2 = await server.file_manager.create_file(file_metadata=file2_meta, actor=default_user)-- # Create test file for source2- file3_meta = PydanticFileMetadata(- source_id=source2.id,- file_name="file3.txt",- file_type="text/plain",- file_size=512,- )- file3 = await server.file_manager.create_file(file_metadata=file3_meta, actor=default_user)-- # Get organization metadata- metadata = await server.file_manager.get_organization_sources_metadata(actor=default_user)-- # Verify top-level aggregations- assert metadata.total_sources >= 2 # May have other sources from other tests- assert metadata.total_files >= 3- assert metadata.total_size >= 3584-- # Find our test sources in the results- source1_meta = next((s for s in metadata.sources if s.source_id == source1.id), None)- source2_meta = next((s for s in metadata.sources if s.source_id == source2.id), None)-- assert source1_meta is not None- assert source1_meta.source_name == "test_source_1"- assert source1_meta.file_count == 2- assert source1_meta.total_size == 3072 # 1024 + 2048- assert len(source1_meta.files) == 2-- # Verify file details in source1- file1_stats = next((f for f in source1_meta.files if f.file_id == file1.id), None)- file2_stats = next((f for f in source1_meta.files if f.file_id == file2.id), None)-- assert file1_stats is not None- assert file1_stats.file_name == "file1.txt"- assert file1_stats.file_size == 1024-- assert file2_stats is not None- assert file2_stats.file_name == "file2.txt"- assert file2_stats.file_size == 2048-- assert source2_meta is not None- assert source2_meta.source_name == "test_source_2"- assert source2_meta.file_count == 1- assert source2_meta.total_size == 512- assert len(source2_meta.files) == 1-- # Verify file details in source2- file3_stats = source2_meta.files[0]- assert file3_stats.file_id == file3.id- assert file3_stats.file_name == "file3.txt"- assert file3_stats.file_size == 512--# ======================================================================================================================# SandboxConfigManager Tests - Sandbox Configs# ======================================================================================================================