fix pydantic json schema warnings

This commit is contained in:
Taylor Wilsdon 2025-04-17 15:59:49 -07:00
parent bd133a7578
commit f27804f4f9

View File

@ -2,7 +2,7 @@
import os import os
import httpx import httpx
import inspect import inspect
from typing import Optional, List, Dict, Any, Type from typing import Optional, List, Dict, Any, Type, Callable
from fastapi import FastAPI, HTTPException, Body, Depends from fastapi import FastAPI, HTTPException, Body, Depends
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
@ -269,31 +269,35 @@ TOOL_MAPPING = {
}, },
} }
# Dynamically create endpoints for each tool # Define a function factory to create endpoint handlers
for tool_name, config in TOOL_MAPPING.items(): def create_endpoint_handler(tool_name: str, method: Callable, args_model: Type[BaseModel]):
args_model = config["args_model"] async def endpoint_handler(args: args_model = Body(...)) -> ToolResponse:
method_to_call = config["method"]
tool_description = config["description"]
async def endpoint_func(args: args_model = Body(...), # type: ignore
method=method_to_call): # Capture method in closure
try: try:
result = await method(args=args) result = await method(args=args)
return {"content": result} return {"content": result}
except HTTPException as e: except HTTPException as e:
raise e raise e
except Exception as e: except Exception as e:
print(f"Error executing tool: {e}") print(f"Error executing tool {tool_name}: {e}")
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
return endpoint_handler
# Register endpoints for each tool
for tool_name, config in TOOL_MAPPING.items():
handler = create_endpoint_handler(
tool_name=tool_name,
method=config["method"],
args_model=config["args_model"]
)
app.post( app.post(
f"/{tool_name}", f"/{tool_name}",
response_model=ToolResponse, response_model=ToolResponse,
summary=tool_description, summary=config["description"],
description=f"Executes the {tool_name} tool. Arguments are passed in the request body.", description=f"Executes the {tool_name} tool. Arguments are passed in the request body.",
tags=["Slack Tools"], tags=["Slack Tools"],
name=tool_name name=tool_name
)(endpoint_func) )(handler)
# --- Root Endpoint --- # --- Root Endpoint ---
@app.get("/", summary="Root endpoint", include_in_schema=False) @app.get("/", summary="Root endpoint", include_in_schema=False)