Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add exception handler for litellm API error #432

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions agents-api/agents_api/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from agents_api.exceptions import PromptTooBigError
from pycozo.client import QueryException
from temporalio.service import RPCError
from litellm.exceptions import APIError

from agents_api.dependencies.auth import get_api_key
from agents_api.env import sentry_dsn
Expand Down Expand Up @@ -120,6 +121,14 @@ async def prompt_too_big_error(request: Request, exc: PromptTooBigError):
)


@app.exception_handler(APIError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refining the exception handling for APIError to provide more appropriate HTTP status codes based on the error specifics, rather than defaulting to 500 Internal Server Error. This can help in accurately reflecting the nature of the error to the clients and aid in better error diagnostics.

async def litellm_api_error(request: Request, exc: APIError):
return JSONResponse(
status_code=status.HTTP_502_BAD_GATEWAY,
content={"error": {"message": str(exc)}},
)


def main(
host="127.0.0.1",
port=8000,
Expand Down
Loading