Skip to content

fix(idempotency): serialize Pydantic models with mode='json' for UUID/date support#8075

Open
abhu85 wants to merge 3 commits intoaws-powertools:developfrom
abhu85:fix/8065-idempotency-uuid-serialization
Open

fix(idempotency): serialize Pydantic models with mode='json' for UUID/date support#8075
abhu85 wants to merge 3 commits intoaws-powertools:developfrom
abhu85:fix/8065-idempotency-uuid-serialization

Conversation

@abhu85
Copy link
Copy Markdown

@abhu85 abhu85 commented Mar 30, 2026

Issue number: closes #8065

Summary

Changes

The _prepare_data() function in aws_lambda_powertools/utilities/idempotency/base.py 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" error when used with @idempotent_function.

Fix: Changed data.model_dump() to data.model_dump(mode="json") to ensure proper JSON serialization of UUID, date, and datetime fields.

User experience

Before:

from uuid import UUID
from pydantic import BaseModel

class PaymentInput(BaseModel):
    payment_id: UUID
    customer_id: str

@idempotent_function(data_keyword_argument="payment", persistence_store=persistence_layer)
def process_payment(payment: PaymentInput):
    return {"status": "processed"}

# Raises: Object of type UUID is not JSON serializable

After:

# Same code now works correctly - UUID is serialized as string for idempotency key generation
process_payment(payment=PaymentInput(payment_id=uuid4(), customer_id="123"))
# Returns: {"status": "processed"}

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…/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
@abhu85 abhu85 requested a review from a team as a code owner March 30, 2026 16:42
@abhu85 abhu85 requested a review from sdangol March 30, 2026 16:42
@boring-cyborg boring-cyborg bot added the tests label Mar 30, 2026
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 30, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 30, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 30, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 2, 2026
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 2, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Idempotency key in idempotent_function fails to serialize dates/UUIDs

2 participants