Serialize dataclasses & rich types in mk_app responses#7
Merged
Conversation
endpoint.py built the success response with JSONResponse(content=output), which serializes via the stdlib json encoder — only dict/list/scalar. A dataclass (or pydantic model, datetime, Enum, set, UUID, Decimal, …) raised "Object of type ... is not JSON serializable" and 500'd. Run output through FastAPI's jsonable_encoder before constructing the JSONResponse. Plain dict/list/scalar outputs pass through unchanged, so the change is non-breaking — it strictly broadens what serializes. Closes #6
This was referenced May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
qh.mk_app()endpoints returned HTTP 500 when the wrapped functionreturned a value the stdlib
jsonencoder can't handle directly — a@dataclass, a pydantic model,datetime,Enum,set,UUID,Decimal, etc.:endpoint.pybuilt the success response withJSONResponse(content=output);Starlette's
JSONResponse.render()callsjson.dumpswith the defaultencoder. A
TypedDictinstance is a plaindictat runtime so it worked — adataclass instance is not, so it raised and qh's generic
exceptwrapped itas a 500.
Fix
Run the output through FastAPI's
jsonable_encoderbefore constructing theJSONResponse. It recursively converts dataclasses, pydantic models (v1/v2),datetime,Enum,set,UUID,Decimal, … into JSON-compatibleprimitives. Plain dict/list/scalar outputs pass through unchanged, so the
change is non-breaking — it strictly broadens what serializes.
Tests
Three new tests in
test_mk_app.py— a dataclass return, alistofdataclasses (the
ef.EfService.search()shape that surfaced this), anddatetime/Enum. Full suite: 139 passed.Closes #6