Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

If your FastAPI error appears as [object Object], first inspect the raw response: FastAPI allows the value inside detail to be a string or a JSON structure such as a dictionary or list. The response may be valid; the display is likely turning that structured value into plain text without selecting or formatting a field. Check the response shape before changing your exception handler.

What FastAPI puts in an HTTP error response

FastAPI’s documented HTTP exception response uses a top-level detail key. With a string detail, the response body looks like this:

{"detail": "Item not found"}

But detail does not have to be a string. The FastAPI documentation says it can be any value convertible to JSON, including a dictionary or list. For example, a structured detail might look like this:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
{"detail": {"message": "Item not found", "code": "missing_item"}}

The example’s message and code keys are illustrative; FastAPI does not require those names. Its documentation explains that the detail parameter can accept JSON-convertible values, not only strings: FastAPI: Handle Errors.

Why the screen may show [object Object]

If the raw JSON contains an object under detail, but your UI shows [object Object], a likely explanation is that client-side JavaScript is converting the object directly to a string for display. That is a general diagnostic inference, not behavior established by FastAPI’s documentation. The FastAPI sources establish that structured JSON can be returned; they do not prescribe how a frontend should render it.

Inspect the response body in your browser’s network panel or API client. If it contains a string, investigate how that string is passed through the UI. If it contains an object or array, find the frontend expression that renders detail. Display a meaningful field, such as detail.message when your API defines that field, or format the structure as JSON if showing the full object is appropriate. These are general debugging options, not a FastAPI-specific frontend requirement.

How FastAPI constructs the response

FastAPI’s default HTTP exception handler places the exception’s detail in {"detail": exc.detail} and returns the exception’s status code and headers when a response body is allowed. That means the shape you supply as detail is reflected inside the response envelope; it is not automatically converted into a user-facing sentence. The handler implementation is available in the FastAPI exception handler source. Because master is a mutable branch, this source link is not a version-pinned guarantee.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which HTTPException should a custom handler catch?

FastAPI’s HTTPException subclasses Starlette’s HTTPException. FastAPI accepts JSON-convertible values for detail, while Starlette’s class accepts strings. FastAPI recommends registering a custom handler for Starlette’s exception class if you want it to catch exceptions raised by Starlette internals or extensions as well as FastAPI exceptions. See the FastAPI error-handling guide and the FastAPI exception reference for the documented distinction.

Changing the exception class or handler is not the first fix to try when the server response is valid JSON and only the UI looks wrong. Start with the raw body and then adjust the client rendering or, if your API should return a simple message, supply a string detail.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.