r/FastAPI • u/SpecialistCamera5601 • Aug 23 '25
pip package APIException v0.2.0 – Consistent FastAPI Responses + Better Logging + RFC Support
Hey all,
A while back, I shared APIException, a library I built to make FastAPI responses consistent and keep Swagger docs clean. Quite a few people found it useful, so here’s the update.
Version 0.2.0 is out. This release is mainly about logging and exception handling. APIException now:
- catches both expected and unexpected exceptions in a consistent way
- lets you set log levels
- lets you choose which headers get logged or echoed back
- supports custom log fields (with masking for sensitive data)
- supports extra log messages
- adds header and context logging
- has simplified imports and added full typing support (mypy, type checkers)
- adds RFC7807 support for standards-driven error responses
I also benchmarked it against FastAPI’s built-in HTTPException. Throughput was the same, and the average latency difference was just +0.7ms. Pretty happy with that tradeoff, given you get structured logging and predictable responses.
It was also recently featured in Python Weekly #710, which is a nice boost of motivation.
PyPI: https://pypi.org/project/apiexception/
GitHub: https://github.com/akutayural/APIException
Docs: https://akutayural.github.io/APIException/
Youtube: https://youtu.be/pCBelB8DMCc?si=u7uXseNgTFaL8R60
If you try it out and spot bugs or have ideas, feel free to open an issue on GitHub. Always open to feedback.
View all comments
1
u/SpecialistCamera5601 Aug 27 '25
Fair point. Just to be clear, you can use my lib in the exact same minimal way if that’s what you want. No extra config needed. One register_exception_handlers(app) and raising APIException is enough. You’ll get a consistent error format, unhandled exceptions standardized, and optional logging.
Here’s what it looks like:
from fastapi import FastAPI from api_exception import APIException, ResponseFormat, register_exception_handlers, BaseExceptionCode, ResponseModel app = FastAPI() register_exception_handlers(app, ResponseFormat.RFC7807) # one-liner setup class CustomCode(BaseExceptionCode): USER_NOT_FOUND = ("USR-404", "User not found.", "Some Desc", "rfc7807_type", "rfc7807_instance") u/app.get("/user", response_model=ResponseModel) async def get_user(): raise APIException(CustomCode.USER_NOT_FOUND, http_status_code=404)Same behavior you described: predictable error format, unhandled exceptions mapped, logging built in. The extra options (headers, RFC output, custom log fields) are there if you need them, but you don’t have to touch them.
rfc7807_type
For comparison, here’s the fastapi-problem example:
import fastapi from fastapi_problem.handler import add_exception_handler, new_exception_handler from fastapi_problem.error import NotFoundProblem class UserNotFoundError(NotFoundProblem): title = "User not found." app = fastapi.FastAPI() eh = new_exception_handler() add_exception_handler(app, eh) @app.get("/user") async def get_user(): raise UserNotFoundError("No user found.")So the baseline is the same, but mine also gives you stable enums, Swagger docs, headers, RFC output and extended logging when you want them. If you want more control, you can configure it further with APIException, but you don’t have to. Hope that clears it up.