Skip to content

Python: preserve dict subclasses in workflow checkpoints - #8519

Open
Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
microsoft:mainfrom
ManoharPaturi:fix/checkpoint-dict-subclass-fidelity
Open

Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
microsoft:mainfrom
ManoharPaturi:fix/checkpoint-dict-subclass-fidelity

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

Motivation & Context

Fixes #8517. checkpoint encode/decode reconstructed every mapping as a plain dict, so defaultdict/Counter/OrderedDict in workflow state lost their type across a save/load and resumed workflows broke.

Description & Review Guide

  • What are the major changes? the encoder records the concrete mapping type and the decoder reconstructs it (collections.defaultdict, Counter, OrderedDict added to the allowlist alongside plain dict).
  • What is the impact of these changes? mapping subclasses survive round-trips; plain dicts unchanged.
  • What do you want reviewers to focus on? the allowlist additions in the decode path.

Related Issue

Fixes #8517

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue
  • This is not a breaking change.

encode_checkpoint_value used isinstance checks for dict and list, so dict
subclasses (defaultdict, Counter, OrderedDict) nested in checkpoint state
were silently flattened to plain dicts on save. The save-time validation
passed because decoding still succeeded, but restored workflows got a
plain dict: a defaultdict access pattern like state["missing"].append(x)
then raises KeyError on resume, and Counter/OrderedDict types are lost.

Only plain dict/list values now take the JSON path; subclasses are pickled
like tuples and sets already were, preserving type and behavior through
the round trip. collections.Counter joins OrderedDict, defaultdict, and
deque in the built-in restricted-unpickler allowlist so the common stdlib
dict subclasses restore under restricted decoding too.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: checkpoints flatten dict subclasses (defaultdict/Counter/OrderedDict become plain dict)

2 participants