From b839c4fe97c68365a87ae3bee370121fd5d797bc Mon Sep 17 00:00:00 2001 From: jontmy Date: Tue, 24 Mar 2026 12:01:22 +0800 Subject: [PATCH] fix(idempotency): fix serialization of Pydantic idempotency keys --- .../utilities/idempotency/base.py | 2 +- .../_pydantic/test_idempotency_with_pydantic.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/aws_lambda_powertools/utilities/idempotency/base.py b/aws_lambda_powertools/utilities/idempotency/base.py index f93b9097611..f6a3563c103 100644 --- a/aws_lambda_powertools/utilities/idempotency/base.py +++ b/aws_lambda_powertools/utilities/idempotency/base.py @@ -60,7 +60,7 @@ def _prepare_data(data: Any) -> Any: # Convert from Pydantic model if callable(getattr(data, "model_dump", None)): - return data.model_dump() + return data.model_dump(mode="json") # Convert from event source data class if callable(getattr(data, "dict", None)): diff --git a/tests/functional/idempotency/_pydantic/test_idempotency_with_pydantic.py b/tests/functional/idempotency/_pydantic/test_idempotency_with_pydantic.py index f8e3debbc30..ea50c1e2e69 100644 --- a/tests/functional/idempotency/_pydantic/test_idempotency_with_pydantic.py +++ b/tests/functional/idempotency/_pydantic/test_idempotency_with_pydantic.py @@ -1,4 +1,5 @@ from typing import Optional +from uuid import UUID import pytest from pydantic import BaseModel @@ -194,6 +195,19 @@ class Foo(BaseModel): assert as_dict == expected_result +def test_idempotent_function_pydantic_uuid(): + # Scenario _prepare_data should convert a pydantic model with UUIDs to a dict + # with serialization of UUIDs to strings + class Foo(BaseModel): + uuid: UUID + + uuid = UUID("01234567-0123-0123-0123-0123456789ab") + expected_result = {"uuid": str(uuid)} + data = Foo(uuid=uuid) + as_dict = _prepare_data(data) + assert as_dict == expected_result + + @pytest.mark.parametrize("data", [None, "foo", ["foo"], 1, True, {}]) def test_idempotent_function_other(data): # All other data types should be left as is