summaryrefslogtreecommitdiffstats
path: root/g4f/api
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-07 10:36:13 +0200
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-07 10:36:13 +0200
commitb35dfcd1b01c575b65e0299ef71d285dc8f41459 (patch)
treecfe5f4a390af62fafefd1d27ca2c82a23cdcab49 /g4f/api
parentUpdate Gemini.py (diff)
downloadgpt4free-b35dfcd1b01c575b65e0299ef71d285dc8f41459.tar
gpt4free-b35dfcd1b01c575b65e0299ef71d285dc8f41459.tar.gz
gpt4free-b35dfcd1b01c575b65e0299ef71d285dc8f41459.tar.bz2
gpt4free-b35dfcd1b01c575b65e0299ef71d285dc8f41459.tar.lz
gpt4free-b35dfcd1b01c575b65e0299ef71d285dc8f41459.tar.xz
gpt4free-b35dfcd1b01c575b65e0299ef71d285dc8f41459.tar.zst
gpt4free-b35dfcd1b01c575b65e0299ef71d285dc8f41459.zip
Diffstat (limited to '')
-rw-r--r--g4f/api/__init__.py28
1 files changed, 25 insertions, 3 deletions
diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py
index b5af629e..579090fe 100644
--- a/g4f/api/__init__.py
+++ b/g4f/api/__init__.py
@@ -3,10 +3,13 @@ import json
import uvicorn
import nest_asyncio
-from fastapi import FastAPI, Response, Request
+from fastapi import FastAPI, Response, Request
from fastapi.responses import StreamingResponse, RedirectResponse, HTMLResponse, JSONResponse
-from pydantic import BaseModel
-from typing import List, Union
+from fastapi.exceptions import RequestValidationError
+from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
+from fastapi.encoders import jsonable_encoder
+from pydantic import BaseModel
+from typing import List, Union
import g4f
import g4f.debug
@@ -39,6 +42,25 @@ class Api:
self.app = FastAPI()
self.routes()
+ self.register_validation_exception_handler()
+
+ def register_validation_exception_handler(self):
+ @self.app.exception_handler(RequestValidationError)
+ async def validation_exception_handler(request: Request, exc: RequestValidationError):
+ details = exc.errors()
+ modified_details = []
+ for error in details:
+ modified_details.append(
+ {
+ "loc": error["loc"],
+ "message": error["msg"],
+ "type": error["type"],
+ }
+ )
+ return JSONResponse(
+ status_code=HTTP_422_UNPROCESSABLE_ENTITY,
+ content=jsonable_encoder({"detail": modified_details}),
+ )
def routes(self):
@self.app.get("/")