powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Bug: idempotency payloads with set, frozenset or bytes fields cannot be hashed

Open leandrodamascena opened this issue 1 month ago • 7 comments

[!WARNING] For contributors, human or agent. Comment here to claim this issue and wait for the assignment before opening a PR; if two PRs show up, the assignee's stays. AI-assisted work is welcome, say so in the PR and tell us how you verified it: run the tests yourself and paste what you saw, do not describe runs you did not do. Read AGENTS.md first, it has the commands and the rules the CI enforces. This goes into 3.41.0.

Expected Behaviour

A dataclass or Pydantic payload with set, frozenset or bytes fields can be used as the idempotency payload; the key hash is computed deterministically.

Current Behaviour

_IdempotencyKeyEncoder in persistence/base.py handles Enum, date/time, UUID and PurePath only. A set field makes the key computation fail with IdempotencyPersistenceLayerError: Failed to save in progress record to idempotency store - Object of type set is not JSON serializable, which reads like a store outage rather than an unsupported payload.

Code snippet

from dataclasses import dataclass
from aws_lambda_powertools.utilities.idempotency import idempotent_function

@dataclass
class Payload:
    id: int
    tags: set

@idempotent_function(data_keyword_argument="payload", persistence_store=layer)
def process(payload: Payload):
    return "ok"

process(payload=Payload(1, {"a"}))  # IdempotencyPersistenceLayerError ... Object of type set is not JSON serializable

Possible Solution

Extend _IdempotencyKeyEncoder.default: encode set/frozenset as a sorted list (sort by repr when elements are not comparable) and bytes as base64. Add tests asserting that the same payload with the same set in a different insertion order produces the same key, and that bytes works.

Item 8 of the Idempotency audit in #8603. No AWS resources needed.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

No response

leandrodamascena avatar Oct 31 '26 22:10 leandrodamascena

HI @leandrodamascena I would like to claim this issue! I'll work on extending _IdempotencyKeyEncoder to support set, frozenset, and bytes, and add the requested unit tests.

Neeraj111010 avatar Oct 31 '26 23:10 Neeraj111010

@Neeraj111010 it's yours, assigning you now. Go ahead with the PR, and say in the description how you ran the tests.

leandrodamascena avatar Nov 01 '26 09:11 leandrodamascena

Hi @leandrodamascena! Just wanted to drop a quick note that I've got everything wrapped up here. All 33 tests are passing cleanly and lint checks are completely clear.

Thanks again!

Neeraj111010 avatar Nov 01 '26 23:11 Neeraj111010

I would like to work on this issue.

anishsinghhh3-bit avatar Nov 02 '26 18:11 anishsinghhh3-bit

Thanks @anishsinghhh3-bit, this one is already assigned to @Neeraj111010. The Logger audit in #8730 becomes issues this week, keep an eye on it.

leandrodamascena avatar Nov 02 '26 20:11 leandrodamascena

[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

This is now released under 3.41.0 version!

github-actions[bot] avatar Nov 18 '26 12:11 github-actions[bot]