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: throw `404` for missing `agent_id` (#4804)

Sarah Wooders3593e7cda6f0
diff --git a/letta/services/helpers/agent_manager_helper.py b/letta/services/helpers/agent_manager_helper.py
index fffe6ccd8..bd317697c 100644
--- a/letta/services/helpers/agent_manager_helper.py
+++ b/letta/services/helpers/agent_manager_helper.py
@@ -19,6 +19,7 @@ from letta.constants import (
MULTI_AGENT_TOOLS,
STRUCTURED_OUTPUT_MODELS,
)
+from letta.errors import LettaAgentNotFoundError
from letta.helpers import ToolRulesSolver
from letta.helpers.datetime_helpers import get_local_time
from letta.llm_api.llm_client import LLMClient
@@ -1238,4 +1239,4 @@ async def validate_agent_exists_async(session, agent_id: str, actor: User) -> No
result = await session.execute(agent_exists_query)
if not result.scalar():
- raise NoResultFound(f"Agent with ID {agent_id} not found")
+ raise LettaAgentNotFoundError(f"Agent with ID {agent_id} not found")
diff --git a/letta/services/job_manager.py b/letta/services/job_manager.py
index cd1dccb80..572ae6ba0 100644
--- a/letta/services/job_manager.py
+++ b/letta/services/job_manager.py
@@ -25,6 +25,7 @@ from letta.schemas.step import Step as PydanticStep
from letta.schemas.usage import LettaUsageStatistics
from letta.schemas.user import User as PydanticUser
from letta.server.db import db_registry
+from letta.services.helpers.agent_manager_helper import validate_agent_exists_async
from letta.utils import enforce_types
logger = get_logger(__name__)
@@ -48,6 +49,9 @@ class JobManager:
# Get agent_id if present
agent_id = getattr(pydantic_job, "agent_id", None)
+ # Verify agent exists before creating the job
+ await validate_agent_exists_async(session, agent_id, actor)
+
job_data = pydantic_job.model_dump(to_orm=True)
# Remove agent_id from job_data as it's not a field in the Job ORM model
# The relationship is handled through the AgentsRuns association table
@@ -79,6 +83,9 @@ class JobManager:
# Get agent_id if present
agent_id = getattr(pydantic_job, "agent_id", None)
+ # Verify agent exists before creating the job
+ await validate_agent_exists_async(session, agent_id, actor)
+
job_data = pydantic_job.model_dump(to_orm=True)
# Remove agent_id from job_data as it's not a field in the Job ORM model
# The relationship is handled through the AgentsRuns association table