Skip to content

armgit / letta

Public Imported from GitHub
0

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

fix: skip agent joins in blocks endpoint (#3226)

cthomas5e4b094a80b8
diff --git a/letta/server/rest_api/routers/v1/blocks.py b/letta/server/rest_api/routers/v1/blocks.py
index b09097ad5..9142e915b 100644
--- a/letta/server/rest_api/routers/v1/blocks.py
+++ b/letta/server/rest_api/routers/v1/blocks.py
@@ -101,6 +101,14 @@ async def retrieve_block(
@router.get("/{block_id}/agents", response_model=List[AgentState], operation_id="list_agents_for_block")
async def list_agents_for_block(
block_id: str,
+ include_relationships: list[str] | None = Query(
+ None,
+ description=(
+ "Specify which relational fields (e.g., 'tools', 'sources', 'memory') to include in the response. "
+ "If not provided, all relationships are loaded by default. "
+ "Using this can optimize performance by reducing unnecessary joins."
+ ),
+ ),
server: SyncServer = Depends(get_letta_server),
actor_id: Optional[str] = Header(None, alias="user_id"),
):
@@ -110,7 +118,9 @@ async def list_agents_for_block(
"""
actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)
try:
- agents = await server.block_manager.get_agents_for_block_async(block_id=block_id, actor=actor)
+ agents = await server.block_manager.get_agents_for_block_async(
+ block_id=block_id, include_relationships=include_relationships, actor=actor
+ )
return agents
except NoResultFound:
raise HTTPException(status_code=404, detail=f"Block with id={block_id} not found")
diff --git a/letta/services/block_manager.py b/letta/services/block_manager.py
index 820158050..6aa891449 100644
--- a/letta/services/block_manager.py
+++ b/letta/services/block_manager.py
@@ -286,14 +286,20 @@ class BlockManager:
@trace_method
@enforce_types
- async def get_agents_for_block_async(self, block_id: str, actor: PydanticUser) -> List[PydanticAgentState]:
+ async def get_agents_for_block_async(
+ self,
+ block_id: str,
+ actor: PydanticUser,
+ include_relationships: Optional[List[str]] = None,
+ ) -> List[PydanticAgentState]:
"""
Retrieve all agents associated with a given block.
"""
async with db_registry.async_session() as session:
block = await BlockModel.read_async(db_session=session, identifier=block_id, actor=actor)
agents_orm = block.agents
- return await asyncio.gather(*[agent.to_pydantic_async() for agent in agents_orm])
+ agents = await asyncio.gather(*[agent.to_pydantic_async(include_relationships=include_relationships) for agent in agents_orm])
+ return agents
@trace_method
@enforce_types