Modeling Decimal
types as json-schema results in ambiguous typing
#739
bruno-f-cruz
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
I agree that this is counterintuitive. I haven't thought too hard about it, but I wonder if we should just pivot and go with |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the bug
The json model builder for
Decimal
types results in the following signature:This was a change from pydantic v1 that allows decimal numbers to be encoded as strings in order to preserve their numeric precision. (see details here).
Unfortunately, Pydantic goes a bit rogue and comes up with their own standard (at least I couldn't find anything in json-schema nor OpenApi??) of what a Decimal means by choosing a AnyOf[str | number] and solving the deserialization with custom logic (see source here).
This is unfortunate since the union is not discriminated, and thus makes it impossible to know what the type should resolve as.
Other users have also raised this issue. See this discussion for more details
Describe the solution you'd like
It would be nice to change the model class and force decimals to either always be strings or numbers. This can be done in two ways:
Serialize
instead of the defaultValidate
decimal_schema
method in the implementation mentioned above and create our own custom model builder.Beta Was this translation helpful? Give feedback.
All reactions