Skip to content

fix: preserve non-string dict keys in pipeline snapshot serialization - #13732

Merged
davidsbatista merged 4 commits into
deepset-ai:mainfrom
Bensonluo:fix/snapshot-non-string-dict-keys
Nov 17, 2026
Merged

davidsbatista merged 4 commits into
deepset-ai:mainfrom
Bensonluo:fix/snapshot-non-string-dict-keys

Conversation

@Bensonluo

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

_serialize_value_with_schema wrote dicts through the properties/serialized_data mappings, whose keys json.dump stringifies. Resuming a pipeline from a snapshot file therefore turned int keys into strings ({1: "a"} came back as {"1": "a"}) and merged keys that stringify alike ({1: "a", "1": "b"} came back as {"1": "b"}), while the in-memory snapshot in the BreakpointException kept the original keys.

Dicts with any non-string key are now serialized as an explicit list of key/value entries, each key with its own schema, and _deserialize_value_with_schema rebuilds the dict from those entries (with a length check like the existing prefixItems one). Specifically, in haystack/utils/base_serialization.py:

  • _serialize_value_with_schema emits the entries encoding when a dict has any non-string key, and keeps the historical properties envelope for dicts with only string keys.
  • _serialize_with_field_fallback's field-by-field path uses the same encoding, and a dict key that cannot be serialized now degrades like a failing value (field omitted with a warning) instead of breaking the JSON write.
  • The same helpers also serialize Agent State data, so keys are preserved there too.

The entries encoding follows the approach proposed in #13497 by @rupak-eng (closed because the CLA was not signed, not for design reasons) — credit for the design belongs there.

How did you test it?

  • New TestNonStringKeys in test/utils/test_base_serialization.py: JSON round-trips for int/float/bool/None/mixed/nested/tuple keys and the {1: "a", "1": "b"} collision; the unchanged string-keyed envelope (including a field literally named entries); the entries length-mismatch DeserializationError; and the field-fallback path with an unserializable value.
  • New end-to-end test in test/core/pipeline/test_breakpoint.py: the issue's repro — run with a breakpoint on sink, save the snapshot file, load it back and resume; the result keeps {1: "a", 2: "b"}.
  • RED→GREEN: the 11 new defect-catching tests fail on current main and pass with this change.
  • pytest test/utils/test_base_serialization.py test/core/pipeline/test_breakpoint.py test/components/agents/test_state_class.py → 152 passed; pytest test/core/pipeline/ test/utils/ → 1204 passed with 11 pre-existing failures reproduced identically on pristine main (missing optional dependencies in my environment); ruff check, ruff format --check, mypy on the changed module and pre-commit hooks all pass.

Notes for the reviewer

  • Snapshots saved by this version that contain non-string dict keys cannot be read by older Haystack versions (they fail loudly instead of silently corrupting); snapshots saved by older versions still load.
  • Deliberately out of scope: the _serialize_with_field_fallback degraded path still drops a field it cannot serialize, as before.

Checklist

From GPT6 and reviewed by person

Dicts with non-string keys serialized by _serialize_value_with_schema were
written through the properties/data mappings, whose keys json.dump
stringifies: resuming a pipeline from a snapshot file turned int keys into
strings and merged keys that stringify alike (e.g. {1: "a", "1": "b"}
came back as {"1": "b"}).

Dicts with any non-string key are now serialized as an explicit list of
key/value entries, each key with its own schema, and the deserializer
rebuilds the dict from those entries. Dicts with only string keys keep the
historical properties envelope, and snapshots created by earlier Haystack
versions still load. The field-by-field fallback path of
_serialize_with_field_fallback uses the same encoding, and a dict key that
cannot be serialized now degrades like a failing value instead of breaking
the JSON write.

Fixes deepset-ai#13492
@Bensonluo
Bensonluo requested a review from a team as a code owner November 13, 2026 07:49
@Bensonluo
Bensonluo requested review from davidsbatista and removed request for a team November 13, 2026 07:49
@CLAassistant

CLAassistant commented Nov 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added P2 Medium priority, add to the next sprint if no P1 available topic:tests and removed P2 Medium priority, add to the next sprint if no P1 available labels Nov 13, 2026
@vercel

vercel Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

@Bensonluo is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the P2 Medium priority, add to the next sprint if no P1 available label Nov 14, 2026
@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Deployment failed for project haystack-docs with the following error:

The provided GitHub repository does not contain the requested branch or commit reference. Please ensure the repository is not empty.

@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
haystack-docs Ready Ready Preview Nov 17, 2026 1:09pm UTC

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/utils
  base_serialization.py
Project Total  

This report was generated by python-coverage-comment-action

@davidsbatista davidsbatista 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.

Hi @Bensonluo, thanks for the fix. I did a few changes

  • Shorter release note, it's meant to be for users and you should not name internal functions.
  • Less duplicated code in _serialize_with_field_fallback: it now only picks which fields to keep, and leaves building the output to _serialize_value_with_schema, so the dict format is defined in one place. The output is the same as before.
  • Two new tests + return type hints on the test components

@davidsbatista
davidsbatista merged commit 2b85450 into deepset-ai:main Nov 17, 2026
26 checks passed

This branch was successfully deployed

1 active deployment
Preview — 7dd4c026 Deployed Nov 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Medium priority, add to the next sprint if no P1 available topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resuming a pipeline from a snapshot file turns int dict keys into strings

3 participants