Skip to content

fix(idempotency): dump Pydantic models with aliases so retries validate - #8937

Open
azmi2409 wants to merge 1 commit into
aws-powertools:developfrom
azmi2409:fix-8936-pydantic-serializer-aliases
Open

azmi2409 wants to merge 1 commit into
aws-powertools:developfrom
azmi2409:fix-8936-pydantic-serializer-aliases

Conversation

@azmi2409

@azmi2409 azmi2409 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Issue number: closes #8936

Summary

Changes

PydanticSerializer.to_dict() now dumps with by_alias=True, so the stored dict is what model_validate() accepts for models with field aliases. No change to from_dict(): records already stored for populate_by_name=True models still validate by field name, and models without aliases dump the same dict as before.

User experience

Before, a function decorated with @idempotent_function(..., output_serializer=PydanticSerializer) worked on the first call but every repeated call with the same idempotency key raised ValidationError when the return model used a field alias (Field(alias="orderId") or alias_generator=to_camel) without populate_by_name=True. After, the repeated call returns the saved model, the same as the first call.

Verification

I reproduced the issue with the snippet from the report and confirmed the second call raised ValidationError before the fix and returns the saved model after it. I added test_pydantic_serializer_round_trips_field_aliases, which round-trips three models (explicit alias, alias_generator=to_camel, and populate_by_name=True as the already-working control) through to_dict plus JSON and back through from_dict. Without the fix the first two cases fail and the control passes; with the fix all three pass.

Focused tests, run by me:

pytest tests/functional/idempotency/_pydantic/test_idempotency_with_pydantic.py -q
21 passed

ruff check and ruff format --check pass on the changed files. No docs change: the fix restores the round-trip the serializer already promises. I used an AI assistant to write this PR, and I ran the verification above myself.


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

@azmi2409
azmi2409 requested a review from a team as a code owner December 1, 2026 13:51
@azmi2409
azmi2409 requested a review from svozza December 1, 2026 13:51
@boring-cyborg boring-cyborg Bot added the tests label Dec 1, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 1, 2026
@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: PydanticSerializer can't replay results from models with field aliases, so idempotent retries raise ValidationError

1 participant