Serialization schema allowing inf and NaN #9409
Unanswered
aeisenbarth
asked this question in
Question
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a marker class (not a subclass of BaseModel) that implements __get_pydantic_core_schema__ to validate/serialize a third-party library's class as a list of numbers (int, float, nan, inf).
Since Pydantic 2.7, it serializes
[inf]
as'[null]'
(apparently it shouldn't have worked before). I found that one has to explicitly enableConfigDict(ser_json_inf_nan="constants")
(#7007 etc.) because the RFC standard does not allow Infinity/NaN. However, I don't have a BaseModel, so I cannot use ConfigDict.Example:
The provided serialization schema returns Python built-in types (list), and Pydantic afterwards serializes them to a string. I need to specify that this step afterward allows NaN.
plain_serializer_function_ser_schema
accepts areturn_schema
argument, andCoreSchema
has a propertyallow_inf_nan
, but I could not get this to work and the documentation for core_schema has not enough usage examples yet.How can I enable
ser_json_inf_nan
/allow_inf_nan
forjson_or_python_schema
?Beta Was this translation helpful? Give feedback.
All reactions