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

Different output of V2 model_dump_json compared to V1 json #9019

Closed
1 task done
pkotnis opened this issue Mar 14, 2024 · 4 comments · Fixed by #9110
Closed
1 task done

Different output of V2 model_dump_json compared to V1 json #9019

pkotnis opened this issue Mar 14, 2024 · 4 comments · Fixed by #9110

Comments

@pkotnis
Copy link

pkotnis commented Mar 14, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When I read V2 migration guide, I did not expect major changes to how model_dump_json will work compared to V1's json. However, it seems like there's a difference in how the new method handles non-ASCII characters.

Is this expected behaviour?

Attached code produces different results for V2 and V1

V1 {"test": "\u00bfC\u00f3mo est\u00e1s?"}
V2 {"test":"¿Cómo estás?"}

Example Code

from pydantic.v1 import BaseModel as BaseModelV1
from pydantic import BaseModel as BaseModelV2


class MessageV1(BaseModelV1):
    test: str


class MessageV2(BaseModelV2):
    test: str


message_v1 = MessageV1(test="¿Cómo estás?")
message_v2 = MessageV2(test="¿Cómo estás?")

print("V1", message_v1.json())
print("V2", message_v2.model_dump_json())

Python, Pydantic & OS Version

pydantic version: 2.5.3
        pydantic-core version: 2.14.6
          pydantic-core build: profile=release pgo=false
                 install path: /Users/xxx/.local/share/virtualenvs/xxx-Fhklx5WJ/lib/python3.8/site-packages/pydantic
               python version: 3.8.17 (default, Nov  6 2023, 20:47:47)  [Clang 15.0.0 (clang-1500.0.40.1)]
                     platform: macOS-14.2.1-arm64-arm-64bit
             related packages: typing_extensions-4.9.0 fastapi-0.109.0
@pkotnis pkotnis added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 14, 2024
@sydney-runkle
Copy link
Member

@pkotnis,

Thanks for reporting this! Indeed, this is a V1 -> V2 change that hasn't yet been well documented. This is due to how serde json serialization works in pydantic-core. I'm not sure how difficult it would be to add support for the older serialization style, but we could certainly consider adding a runtime flag / config setting for that.

This might help you as a workaround in the meantime: #8825.

@sydney-runkle sydney-runkle added feature request documentation and removed bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 15, 2024
@sydney-runkle
Copy link
Member

I've also marked this with a documentation tag, and will open a PR later this week to update the migration guide with a note on this change :).

@pkotnis
Copy link
Author

pkotnis commented Mar 18, 2024

@sydney-runkle , thanks for the update.

Solution suggested in #8825 can indeed help, but won't work if your pydantic objects contains types that json.dumps cannot handle, such us datetime for example.

import json
from datetime import datetime
from pydantic import BaseModel as BaseModel


class Message(BaseModel):
    text: str
    date: datetime


message = Message(text="¿Cómo estás?", date=datetime.now())
print(json.dumps(message.model_dump(), ensure_ascii=True))

# TypeError: Object of type datetime is not JSON serializable

The runtime flag/config setting that you mentioned would be a great addition. Cheers.

@sydney-runkle
Copy link
Member

@pkontis,

You can use model_dump(mode='json') to make this work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants