fix(idempotency): serialize Pydantic models with mode='json' for UUID/date support#8075
Conversation
…/date support The `_prepare_data()` function was calling `model_dump()` without specifying `mode="json"`, which defaults to `mode="python"`. This caused Pydantic models containing UUIDs, dates, or datetimes to fail with "Object of type UUID is not JSON serializable" when used with `@idempotent_function`. Fixes aws-powertools#8065
|
leandrodamascena
left a comment
There was a problem hiding this comment.
Hey @abhu85, great fix!
At glance I thought it could be a breaking change, but after inspecting the code I saw that Pydantic models with complex types (UUID, date, datetime, Decimal, Path) would already crash during JSON hashing before this fix. For models using only primitive types (str, int, float, bool), both modes produce identical output, so existing users aren't affected. Thats really nice.
I made a few changes on top of your PR:
- Renamed test file to
test_idempotency_pydantic_json_serialization.pyto better reflect its scope - Removed
_prepare_dataunit tests - functional tests should go through@idempotent_function - Added integration tests for
date,datetime,Decimal, andPurePosixPath(original only covered UUID) - All tests follow the existing pattern intest_idempotency_with_pydantic.py
Thanks a lot, approved!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #8075 +/- ##
========================================
Coverage 96.64% 96.64%
========================================
Files 282 282
Lines 13791 13791
Branches 1102 1102
========================================
Hits 13329 13329
Misses 339 339
Partials 123 123 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |



Issue number: closes #8065
Summary
Changes
The
_prepare_data()function inaws_lambda_powertools/utilities/idempotency/base.pywas callingmodel_dump()without specifyingmode="json", which defaults tomode="python". This caused Pydantic models containing UUIDs, dates, or datetimes to fail with "Object of type UUID is not JSON serializable" error when used with@idempotent_function.Fix: Changed
data.model_dump()todata.model_dump(mode="json")to ensure proper JSON serialization of UUID, date, and datetime fields.User experience
Before:
After:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.