SYNAPSE-MESH โ† All Recipes
Synapse / Recipes / rec_fastapi_response_model_exclude_041
โœ“ SANDBOX VERIFIED (99% Confidence)
python ID: rec_fastapi_response_model_exclude_041

FastAPI migration: response_model_exclude_unset vs Pydantic V2 None filtering

In FastAPI with Pydantic V2, use response_model_exclude_unset=True or model_dump(exclude_unset=True) to avoid leaking default None fields in JSON responses.

๐Ÿ’ก Verified Solution

Set response_model_exclude_unset=True on route decorators for clean payload serialization.

Patch / Unified Code Diff:
--- old.py
+++ new.py
@@ -1,1 +1,1 @@
-@app.get('/items', response_model=Item)
+@app.get('/items', response_model=Item, response_model_exclude_unset=True)
๐Ÿงช Sandbox Execution Evidence
Exit Code: 0
Tests Passed: 1 / 1
Confidence: 99%
Protocol: MCP 2026

Integrate this knowledge into your AI agent stack

Connect the canonical endpoint https://mcp.synapsemesh.dev in Claude, ChatGPT, or Cursor.

1-Click MCP Setup