fix: round-trip serialization of mixed-type lists in schema-based serialization - #12202
Open
davidpavlovschi wants to merge 1 commit into
Open
Conversation
…ialization _serialize_value_with_schema derived the array schema from the first element only, so a list/tuple/set holding mixed types either crashed on deserialization with a raw AttributeError or silently came back mis-typed. Mixed-type arrays now record one schema per position under the JSON Schema prefixItems keyword, and _deserialize_value_with_schema prefers prefixItems when present, falling back to items otherwise. Homogeneous arrays keep the exact same output as before, so existing snapshots keep loading.
davidpavlovschi
requested review from
davidsbatista
and removed request for
a team
July 30, 2026 21:06
|
@davidpavlovschi is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
|
Hey @davidsbatista I can take review of this one. I was recently working on this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Proposed Changes:
_serialize_value_with_schemaderived the array schema from the first element only, so any list/tuple/set holding mixed types was described by a schema that lied about every element after the first. Two concrete failures onmain(commit 5add74e):This hits real runtime values: Agent
Statefields and pipeline-breakpoint inputs holding heterogeneous lists crash or corrupt on resume.Changes in
haystack/utils/base_serialization.py:{"type": "array", "items": {...}}envelope unchanged. Only when they differ, emit the JSON-Schema-standard{"type": "array", "prefixItems": [schema_per_item, ...]}. TheuniqueItems/frozen/minItems/maxItemsmarkers are attached exactly as before, so set/frozenset/tuple reconstruction is untouched.prefixItemswhen present (each item is zipped with its own schema) and otherwise fall back toschema["items"]exactly as today. AprefixItemslength that disagrees with the data length raises aDeserializationErrorinstead of a rawziperror.releasenotes/notes/.Backward compatibility: homogeneous arrays serialize to the byte-for-byte same dict as before, and payloads written by older versions (which only ever carry
items) still deserialize through the unchangeditemspath. Both are asserted by dedicated tests.How did you test it?
Unit tests added in
test/utils/test_base_serialization.py(newTestMixedTypeArraysclass): the exact serialized schema for mixed primitives, round-trips for mixed primitives, primitives mixed with ato_dictobject (Document), a mixed tuple, mixed sets and frozensets (with container-type assertions), nested mixed lists, the empty list, plus a backward-compat assertion that a homogeneous list/tuple still serializes to the pre-change dict, that an old-formatitems-only payload still deserializes, and that aprefixItems/data length mismatch raisesDeserializationError.Verified red-before / green-after: with the new tests against the unmodified
base_serialization.py, 5 of them fail (including theAttributeErrorcrash); with the fix all pass.Ran locally (Python 3.12, editable install):
pytest test/utils/test_base_serialization.py test/core/pipeline/test_breakpoint.py test/core/pipeline/test_component_checks.py— the three call sites of these helpers: 160 passed.pytest test/utils test/core test/dataclasses -m "not integration"— 2534 passed, 4 failures all pre-existing and unrelated to this change (missing optionaltorch/ipythonin this environment).ruff checkandruff format --checkon both touched files — clean.mypyonhaystack/utils/base_serialization.py— clean.Notes for the reviewer
The issue offers "implement or document" as alternatives. This PR takes the implement route because the current behaviour is a hard crash (
AttributeErrorleaking out of deserialization), not just a documented gap. The mechanism is deliberately conservative: nothing about homogeneous output changes, so existing snapshots and storedStatepayloads keep loading unchanged, andprefixItemsis the standard JSON Schema keyword for positional element schemas rather than a Haystack-specific marker.One behavioural detail worth a look: a set or frozenset with mixed types now round-trips too. The per-position schemas are produced from the same single iteration that produces the data, so the pairing is always consistent even though set iteration order is arbitrary.
This PR was fully generated with an AI assistant. I have reviewed the changes and run the relevant tests.
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.