Repository navigation
keep aliases and inf/nan when ToJsonOptions is set - #2039
Merged
tconley1428 merged 1 commit intoOct 29, 2026
Merged
tconley1428 merged 1 commit into
tconley1428 merged 1 commit into
Conversation
PydanticJSONPlainPayloadConverter serialized through SchemaSerializer(any_schema()).to_json() when to_json_options was set. Since pydantic 2.11 that path defaults to by_alias=False, and ser_json_inf_nan defaults to 'null', unlike pydantic_core.to_json (by_alias=True, inf/nan as constants). Aliases were dropped and inf/nan became null whenever e.g. exclude_unset=True was used. Pass by_alias=True and configure the serializer with ser_json_inf_nan='constants' so the options only change what they control.
tconley1428
approved these changes
Oct 29, 2026
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.
What was changed
PydanticJSONPlainPayloadConverternow builds its serializer withconfig={"ser_json_inf_nan": "constants"}and passesby_alias=TruetoSchemaSerializer.to_json, so theto_json_optionspath produces the same bytes as the no-options path (pydantic_core.to_json).ToJsonOptionskeeps its only effect,exclude_unset. Adds achangelog/fixedfragment.Why?
Since pydantic 2.11,
SchemaSerializer.to_jsondefaults toby_alias=Falseand writesinf/nanasnull, unlikepydantic_core.to_json. WithToJsonOptionsset, aliased fields were therefore encoded under their field name, so a receiver that accepts only the alias silently used the field default, and top-level/list/dictinf/nanbecamenull.Pydantic-core versions that do not know
ser_json_inf_nanignore the key, so no version guard is needed (checked on pydantic 2.0.3: default and options paths still match). Inf/nan inside model fields stay governed by the model config on both paths, as before.Checklist
Closes [Bug] Pydantic converter with ToJsonOptions drops field aliases and serializes inf/nan as null #2038
How was this tested:
New tests in
tests/contrib/pydantic/test_pydantic.py:test_to_json_options_wire_format_matches_default(parametrized): byte-for-byte parity between the converter withToJsonOptions()/ToJsonOptions(exclude_unset=False)and the default converter for an alias model (alias, float, timedelta, bytes),inf,-inf,nan,[inf, 1.5, nan],{"ratio": inf}and a dict with timedelta and bytes.test_to_json_options_exclude_unset_keeps_aliases:exclude_unset=Trueemits{"orderId":7}and round-trips.On pydantic 2.14.1 the new tests fail without the change (7 failed, 32 passed) and
tests/contrib/pydanticpasses with it (39 passed). Also checked on pydantic 2.11.0 (new tests fail before, pass after) and 2.10.6 (only the inf/nan cases fail before). The reproduction from the issue now sendsorderId=7and the workflow sees7. Not run: the other contrib suites and CI.No. Changelog fragment added under
changelog/fixed.