Skip to content

Python: fix(checkpoint): preserve dicts whose keys collide after str() - #8272

Open
LI (ktz03) wants to merge 3 commits into
microsoft:mainfrom
ktz03:fix/checkpoint-dict-key-collision
Open

LI (ktz03) wants to merge 3 commits into
microsoft:mainfrom
ktz03:fix/checkpoint-dict-key-collision

Conversation

@ktz03

@ktz03 LI (ktz03) commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Motivation & Context

Checkpoint encoding stringifies dict keys for JSON. Distinct Python keys that collide after str() (e.g. {1: "a", "1": "b"}) previously lost values in the encoded mapping. Stateful __str__ implementations could also disagree across the reserved-key check, collision check, and final encode passes.

Fixes #8200 (checkpoint dict key collision after str).

Description & Review Guide

  • What are the major changes? Detect stringified-key collisions and fall back to pickle encoding; stringify each key once and reuse that string for reserved-key checks, collision detection, and the encoded mapping.
  • What is the impact of these changes? Prevents silent value loss for colliding keys and makes encode behavior consistent under stateful __str__.
  • What do you want reviewers to focus on? _encode dict path in _checkpoint_encoding.py and the regression tests in test_checkpoint_encode.py.

Related Issue

Fixes #8200

No other open PR targets this issue.

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 (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

When dict keys collide after str() (e.g. 1 and "1"), JSON encoding silently overwrote values. Pickle those mappings instead (microsoft#8256).

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.

🟢 Approval recommended

The focused implementation resolves the reported data-loss case and includes appropriate regression coverage.

Pull request overview

Prevents checkpoint data loss when distinct dictionary keys stringify identically.

Changes:

  • Pickles mappings with stringified-key collisions.
  • Adds a restricted-decoding round-trip regression test.
File summaries
File Description
python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py Detects collisions before dictionary encoding.
python/packages/core/tests/workflow/test_checkpoint_encode.py Verifies colliding keys retain both values.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@ktz03

Copy link
Copy Markdown
Contributor Author

Fork CI for this PR may need a maintainer to Approve workflows before Python tests run (same as other community PRs). Happy to address review feedback once checks appear.

@FOWEPJF255

Copy link
Copy Markdown
Contributor

Nice narrow fix for the str(k) key-collision case in checkpoint encoding 鈥?pickle fallback when len({str(k)}) != len(d) is easy to reason about and CI is green.

Should be ready for maintainer review on #8256.

Comment thread python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py Outdated
Reuse one str(key) for reserved-key checks, collision detection, and
the encoded mapping so stateful __str__ cannot disagree across passes.
Reuse one str(key) for reserved-key checks, collision detection, and
the encoded mapping so stateful __str__ cannot disagree across passes.
@ktz03
LI (ktz03) deployed to github-app-auth September 14, 2026 12:35 — with GitHub Actions Active
@eavanvalkenburg

Copy link
Copy Markdown
Member

Thanks for the update. Before this is ready, could you please:

  • replace the PR body with the current repository template, retain its four required sections, provide substantive Motivation and Description answers, and complete the applicable checklist items

Once those are addressed, please re-request review. Thanks!

@ktz03
LI (ktz03) deployed to github-app-auth September 14, 2026 13:52 — with GitHub Actions Active
@ktz03

Copy link
Copy Markdown
Contributor Author

Eduard van Valkenburg (@eavanvalkenburg) Updated PR body to the repository template and addressed the latest review feedback on this head — please re-review when convenient.

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.

5 participants