From 223b6bc0a98a2f0268328ecfe9a4a86d0c6a83ac Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Tue, 10 Dec 2024 14:00:31 -0800 Subject: [PATCH] fix: cleanup error trace for no llm/embedding_config on POST (#2218) --- letta/server/rest_api/app.py | 15 ++++++++++----- letta/server/server.py | 6 ++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/letta/server/rest_api/app.py b/letta/server/rest_api/app.py index 9b567bf4..ebbd5860 100644 --- a/letta/server/rest_api/app.py +++ b/letta/server/rest_api/app.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Optional import uvicorn -from fastapi import FastAPI +from fastapi import FastAPI, Request from fastapi.responses import JSONResponse from starlette.middleware.base import BaseHTTPMiddleware from starlette.middleware.cors import CORSMiddleware @@ -136,17 +136,18 @@ def create_application() -> "FastAPI": }, ) + debug_mode = "--debug" in sys.argv app = FastAPI( swagger_ui_parameters={"docExpansion": "none"}, # openapi_tags=TAGS_METADATA, title="Letta", summary="Create LLM agents with long-term memory and custom tools 📚🦙", version="1.0.0", # TODO wire this up to the version in the package - debug=True, + debug=debug_mode, # if True, the stack trace will be printed in the response ) @app.exception_handler(Exception) - async def generic_error_handler(request, exc): + async def generic_error_handler(request: Request, exc: Exception): # Log the actual error for debugging log.error(f"Unhandled error: {exc}", exc_info=True) @@ -166,12 +167,16 @@ def create_application() -> "FastAPI": }, ) + @app.exception_handler(ValueError) + async def value_error_handler(request: Request, exc: ValueError): + return JSONResponse(status_code=400, content={"detail": str(exc)}) + @app.exception_handler(LettaAgentNotFoundError) - async def agent_not_found_handler(request, exc): + async def agent_not_found_handler(request: Request, exc: LettaAgentNotFoundError): return JSONResponse(status_code=404, content={"detail": "Agent not found"}) @app.exception_handler(LettaUserNotFoundError) - async def user_not_found_handler(request, exc): + async def user_not_found_handler(request: Request, exc: LettaUserNotFoundError): return JSONResponse(status_code=404, content={"detail": "User not found"}) settings.cors_origins.append("https://app.letta.com") diff --git a/letta/server/server.py b/letta/server/server.py index 609e8eab..c60abe48 100644 --- a/letta/server/server.py +++ b/letta/server/server.py @@ -826,6 +826,12 @@ class SyncServer(Server): if not user: raise ValueError(f"cannot find user with associated client id: {user_id}") + if request.llm_config is None: + raise ValueError("llm_config is required") + + if request.embedding_config is None: + raise ValueError("embedding_config is required") + # created and persist the agent state in the DB agent_state = PersistedAgentState( name=request.name,