Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model_dump should serialise bools as bools for json #7294

Closed
1 task done
Booplicate opened this issue Aug 30, 2023 · 8 comments
Closed
1 task done

model_dump should serialise bools as bools for json #7294

Booplicate opened this issue Aug 30, 2023 · 8 comments
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@Booplicate
Copy link

Booplicate commented Aug 30, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Currently when dumping model to json, objects of type bool are being serialised as integers in json. I think this is a bug because json natively supports booleans (true/false).

Example Code

class SomeModel(BaseModel):
    value: StrictFloat | StrictInt | StrictBool

m = SomeModel(value=True)

# Passes
assert type(m.model_dump(mode="python")["value"]) is bool
# This will fail
assert type(m.model_dump(mode="json")["value"]) is bool, "didn't serialise as bool"

Python, Pydantic & OS Version

Python 3.11.4
Pydantic 2.1.1
Pydantic Core 2.4.0
Windows 10

Selected Assignee: @davidhewitt

@Booplicate Booplicate added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Aug 30, 2023
@Kludex Kludex removed the unconfirmed Bug not yet confirmed as valid/applicable label Aug 30, 2023
@fatelei
Copy link

fatelei commented Aug 31, 2023

image

recommend to use model_dump_json

@Booplicate
Copy link
Author

model_dump_json dumps to a str, I need to serialise to json data types (object/array/int//float/bool/string).

@sydney-runkle
Copy link
Member

Hi @Booplicate,

Thanks very much for reporting this. I can confirm that this is a bug. This issue is a duplicate of the #6830 bug. We're working on fixing this issue in pydantic-core.

The issue is actually with the wrong type being selected when serializing a Union.

See the example below:

from pydantic import BaseModel, StrictBool, StrictFloat, StrictInt


class SomeModel(BaseModel):
    value: StrictFloat | StrictInt | StrictBool


class BoolModel(BaseModel):
    value: StrictBool


m1 = SomeModel(value=True)
m2 = BoolModel(value=True)

# Passes
assert type(m1.model_dump(mode="python")["value"]) is bool
# This will fail
assert type(m1.model_dump(mode="json")["value"]) is bool, "didn't serialise as bool"


# Passes
assert type(m2.model_dump(mode="python")["value"]) is bool
# Passes
assert type(m2.model_dump(mode="json")["value"]) is bool

I'm going to close this issue given that it's a duplicate of #6830, but we will include a test case like this one in our test suite once we fix this bug.

@Booplicate
Copy link
Author

Good to know, thank you.

@gustelle
Copy link

gustelle commented Jul 15, 2024

Hello, is that fixed ? I'm having the issue with pydantic==2.8.2

See below:

class MyModel(BaseModel):
    value: str | float | int | bool

m1 = MyModel(value=True)

# fails
assert type(m1.model_dump(mode="json")["value"]) is bool 

NB : the ordering of types seems to be a fix for the issue

see below

class MyModel(BaseModel):
    value: str | bool | float | int

m1 = MyModel(value=True)

# passes
assert type(m1.model_dump(mode="json")["value"]) is bool 

@sydney-runkle
Copy link
Member

You're right, this is still an issue

@sydney-runkle sydney-runkle reopened this Jul 17, 2024
@sydney-runkle sydney-runkle added this to the Union Issues milestone Jul 17, 2024
@dexterionut
Copy link

dexterionut commented Aug 14, 2024

You're right, this is still an issue

@sydney-runkle Was this fixed with pydantic/pydantic-core#1398 ? I can't figure it out from the test case in that PR as it doesn't test to_json and it only uses ints.

@sydney-runkle
Copy link
Member

Ah @dexterionut, yep! This is fixed by that PR, great call!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

No branches or pull requests

7 participants