Skip to content

armgit / letta

Public Imported from GitHub
0

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

feat: add openai-style include param for agents relationship loading [LET-5820] (#5681)

cthomas899f70661c44
diff --git a/fern/openapi.json b/fern/openapi.json
index cbabf4339..da276fc39 100644
--- a/fern/openapi.json
+++ b/fern/openapi.json
@@ -3915,10 +3915,34 @@
"type": "null"
}
],
- "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.",
+ "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.This is a legacy parameter, and no longer supported after 1.0.0 SDK versions.",
"title": "Include Relationships"
},
- "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."
+ "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.This is a legacy parameter, and no longer supported after 1.0.0 SDK versions."
+ },
+ {
+ "name": "include",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "type": "array",
+ "items": {
+ "enum": [
+ "agent.blocks",
+ "agent.identities",
+ "agent.managed_group",
+ "agent.secrets",
+ "agent.sources",
+ "agent.tags",
+ "agent.tools"
+ ],
+ "type": "string"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default.",
+ "default": [],
+ "title": "Include"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default."
},
{
"name": "order",
@@ -4375,10 +4399,34 @@
"type": "null"
}
],
- "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.",
+ "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.This is a legacy parameter, and no longer supported after 1.0.0 SDK versions.",
"title": "Include Relationships"
},
- "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."
+ "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.This is a legacy parameter, and no longer supported after 1.0.0 SDK versions."
+ },
+ {
+ "name": "include",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "type": "array",
+ "items": {
+ "enum": [
+ "agent.blocks",
+ "agent.identities",
+ "agent.managed_group",
+ "agent.secrets",
+ "agent.sources",
+ "agent.tags",
+ "agent.tools"
+ ],
+ "type": "string"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default.",
+ "default": [],
+ "title": "Include"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default."
}
],
"responses": {
@@ -8953,6 +9001,30 @@
"title": "Order By"
},
"description": "Field to sort by"
+ },
+ {
+ "name": "include",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "type": "array",
+ "items": {
+ "enum": [
+ "agent.blocks",
+ "agent.identities",
+ "agent.managed_group",
+ "agent.secrets",
+ "agent.sources",
+ "agent.tags",
+ "agent.tools"
+ ],
+ "type": "string"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default.",
+ "default": [],
+ "title": "Include"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default."
}
],
"responses": {
@@ -10482,10 +10554,34 @@
"type": "null"
}
],
- "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.",
+ "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.This is a legacy parameter, and no longer supported after 1.0.0 SDK versions.",
"title": "Include Relationships"
},
- "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."
+ "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.This is a legacy parameter, and no longer supported after 1.0.0 SDK versions."
+ },
+ {
+ "name": "include",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "type": "array",
+ "items": {
+ "enum": [
+ "agent.blocks",
+ "agent.identities",
+ "agent.managed_group",
+ "agent.secrets",
+ "agent.sources",
+ "agent.tags",
+ "agent.tools"
+ ],
+ "type": "string"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default.",
+ "default": [],
+ "title": "Include"
+ },
+ "description": "Specify which relational fields to include in the response. No relationships are included by default."
}
],
"responses": {
diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.py
index e1c89166b..c7dcb79e5 100644
--- a/letta/server/rest_api/routers/v1/agents.py
+++ b/letta/server/rest_api/routers/v1/agents.py
@@ -28,7 +28,7 @@ from letta.log import get_logger
from letta.orm.errors import NoResultFound
from letta.otel.context import get_ctx_attributes
from letta.otel.metric_registry import MetricRegistry
-from letta.schemas.agent import AgentState, CreateAgent, UpdateAgent
+from letta.schemas.agent import AgentRelationships, AgentState, CreateAgent, UpdateAgent
from letta.schemas.agent_file import AgentFileSchema
from letta.schemas.block import BaseBlock, Block, BlockUpdate
from letta.schemas.enums import AgentType, RunStatus
@@ -58,7 +58,7 @@ from letta.server.server import SyncServer
from letta.services.lettuce import LettuceClient
from letta.services.run_manager import RunManager
from letta.settings import settings
-from letta.utils import safe_create_shielded_task, safe_create_task, truncate_file_visible_content
+from letta.utils import is_1_0_sdk_version, safe_create_shielded_task, safe_create_task, truncate_file_visible_content
from letta.validators import AgentId, BlockId, FileId, MessageId, SourceId, ToolId
# These can be forward refs, but because Fastapi needs them at runtime the must be imported normally
@@ -94,8 +94,13 @@ async def list_agents(
"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."
+ "This is a legacy parameter, and no longer supported after 1.0.0 SDK versions."
),
),
+ include: List[AgentRelationships] = Query(
+ [],
+ description=("Specify which relational fields to include in the response. No relationships are included by default."),
+ ),
order: Literal["asc", "desc"] = Query(
"desc", description="Sort order for agents by creation time. 'asc' for oldest first, 'desc' for newest first"
),
@@ -126,6 +131,8 @@ async def list_agents(
# Handle backwards compatibility - prefer new parameters over legacy ones
final_ascending = (order == "asc") if order else ascending
final_sort_by = order_by if order_by else sort_by
+ if include_relationships is None and is_1_0_sdk_version(headers):
+ include_relationships = [] # don't default include all if using new SDK version
# Call list_agents directly without unnecessary dict handling
return await server.agent_manager.list_agents_async(
@@ -143,6 +150,7 @@ async def list_agents(
identity_id=identity_id,
identifier_keys=identifier_keys,
include_relationships=include_relationships,
+ include=include,
ascending=final_ascending,
sort_by=final_sort_by,
show_hidden_agents=show_hidden_agents,
@@ -720,8 +728,13 @@ async def retrieve_agent(
"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."
+ "This is a legacy parameter, and no longer supported after 1.0.0 SDK versions."
),
),
+ include: List[AgentRelationships] = Query(
+ [],
+ description=("Specify which relational fields to include in the response. No relationships are included by default."),
+ ),
server: "SyncServer" = Depends(get_letta_server),
headers: HeaderParams = Depends(get_headers),
):
@@ -731,7 +744,11 @@ async def retrieve_agent(
actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
- return await server.agent_manager.get_agent_by_id_async(agent_id=agent_id, include_relationships=include_relationships, actor=actor)
+ if include_relationships is None and is_1_0_sdk_version(headers):
+ include_relationships = [] # don't default include all if using new SDK version
+ return await server.agent_manager.get_agent_by_id_async(
+ agent_id=agent_id, include_relationships=include_relationships, include=include, actor=actor
+ )
@router.delete("/{agent_id}", response_model=None, operation_id="delete_agent")
diff --git a/letta/server/rest_api/routers/v1/blocks.py b/letta/server/rest_api/routers/v1/blocks.py
index c0a4ad3aa..f34fd4d8c 100644
--- a/letta/server/rest_api/routers/v1/blocks.py
+++ b/letta/server/rest_api/routers/v1/blocks.py
@@ -3,10 +3,11 @@ from typing import TYPE_CHECKING, List, Literal, Optional
from fastapi import APIRouter, Body, Depends, HTTPException, Query
from letta.orm.errors import NoResultFound
-from letta.schemas.agent import AgentState
+from letta.schemas.agent import AgentRelationships, AgentState
from letta.schemas.block import BaseBlock, Block, BlockUpdate, CreateBlock
from letta.server.rest_api.dependencies import HeaderParams, get_headers, get_letta_server
from letta.server.server import SyncServer
+from letta.utils import is_1_0_sdk_version
from letta.validators import BlockId
if TYPE_CHECKING:
@@ -183,8 +184,13 @@ async def list_agents_for_block(
"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."
+ "This is a legacy parameter, and no longer supported after 1.0.0 SDK versions."
),
),
+ include: List[AgentRelationships] = Query(
+ [],
+ description=("Specify which relational fields to include in the response. No relationships are included by default."),
+ ),
server: SyncServer = Depends(get_letta_server),
headers: HeaderParams = Depends(get_headers),
):
@@ -193,6 +199,8 @@ async def list_agents_for_block(
Raises a 404 if the block does not exist.
"""
actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)
+ if include_relationships is None and is_1_0_sdk_version(headers):
+ include_relationships = [] # don't default include all if using new SDK version
agents = await server.block_manager.get_agents_for_block_async(
block_id=block_id,
before=before,
@@ -200,6 +208,7 @@ async def list_agents_for_block(
limit=limit,
ascending=(order == "asc"),
include_relationships=include_relationships,
+ include=include,
actor=actor,
)
return agents
diff --git a/letta/server/rest_api/routers/v1/identities.py b/letta/server/rest_api/routers/v1/identities.py
index e74c50900..d6c991a68 100644
--- a/letta/server/rest_api/routers/v1/identities.py
+++ b/letta/server/rest_api/routers/v1/identities.py
@@ -3,10 +3,11 @@ from typing import TYPE_CHECKING, List, Literal, Optional
from fastapi import APIRouter, Body, Depends, Header, Query
from letta.orm.errors import NoResultFound, UniqueConstraintViolationError
-from letta.schemas.agent import AgentState
+from letta.schemas.agent import AgentRelationships, AgentState
from letta.schemas.block import Block
from letta.schemas.identity import Identity, IdentityBase, IdentityCreate, IdentityProperty, IdentityType, IdentityUpdate, IdentityUpsert
from letta.server.rest_api.dependencies import HeaderParams, get_headers, get_letta_server
+from letta.utils import is_1_0_sdk_version
from letta.validators import IdentityId
if TYPE_CHECKING:
@@ -158,6 +159,10 @@ async def list_agents_for_identity(
"desc", description="Sort order for agents by creation time. 'asc' for oldest first, 'desc' for newest first"
),
order_by: Literal["created_at"] = Query("created_at", description="Field to sort by"),
+ include: List[AgentRelationships] = Query(
+ [],
+ description=("Specify which relational fields to include in the response. No relationships are included by default."),
+ ),
server: "SyncServer" = Depends(get_letta_server),
headers: HeaderParams = Depends(get_headers),
):
@@ -171,6 +176,7 @@ async def list_agents_for_identity(
after=after,
limit=limit,
ascending=(order == "asc"),
+ include=include,
actor=actor,
)