From a7e2c8cff7802c1e685112c128919b93048114ef Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Tue, 3 Dec 2024 16:13:38 -0800 Subject: [PATCH] fix: patch bug in pydantic request model for `/v1/tools/run` (#2152) --- letta/schemas/tool.py | 4 ++-- letta/server/rest_api/routers/v1/tools.py | 28 +++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/letta/schemas/tool.py b/letta/schemas/tool.py index ff39ed7c..ba844551 100644 --- a/letta/schemas/tool.py +++ b/letta/schemas/tool.py @@ -209,7 +209,7 @@ class ToolRun(LettaBase): class ToolRunFromSource(LettaBase): + source_code: str = Field(..., description="The source code of the function.") args: str = Field(..., description="The arguments to pass to the tool (as stringified JSON).") - name: Optional[str] = Field(..., description="The name of the tool to run.") - source_code: str = Field(None, description="The source code of the function.") + name: Optional[str] = Field(None, description="The name of the tool to run.") source_type: Optional[str] = Field(None, description="The type of the source code.") diff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.py index d9e24473..c9b60f78 100644 --- a/letta/server/rest_api/routers/v1/tools.py +++ b/letta/server/rest_api/routers/v1/tools.py @@ -186,13 +186,27 @@ def run_tool_from_source( """ actor = server.get_user_or_default(user_id=user_id) - return server.run_tool_from_source( - tool_source=request.source_code, - tool_source_type=request.source_type, - tool_args=request.args, - tool_name=request.name, - user_id=actor.id, - ) + try: + return server.run_tool_from_source( + tool_source=request.source_code, + tool_source_type=request.source_type, + tool_args=request.args, + tool_name=request.name, + user_id=actor.id, + ) + except LettaToolCreateError as e: + # HTTP 400 == Bad Request + print(f"Error occurred during tool creation: {e}") + # print the full stack trace + import traceback + + print(traceback.format_exc()) + raise HTTPException(status_code=400, detail=str(e)) + + except Exception as e: + # Catch other unexpected errors and raise an internal server error + print(f"Unexpected error occurred: {e}") + raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}") # Specific routes for Composio