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
How to ensure_ascii in Pydantic v2 #8825
Comments
Thanks for your question! You could do something like this: from pydantic import BaseModel, ConfigDict
import json
class TextMessage(BaseModel):
text: str
dumped_data = TextMessage.model_validate({"text": "Что"}).model_dump(by_alias=True, exclude_unset=True)
print(dumped_data)
#> {'text': 'Что'}
print(json.dumps(dumped_data, ensure_ascii=True))
#> {"text": "\u0427\u0442\u043e"} Or even: from pydantic import BaseModel, model_serializer
import json
class TextMessage(BaseModel):
text: str
@model_serializer(mode='wrap', when_used='json')
def serialize(self, handler) -> str:
return json.dumps(handler(self), ensure_ascii=True)
print(TextMessage.model_validate({"text": "Что"}).model_dump_json(by_alias=True, exclude_unset=True))
#> "{\"text\": \"\\u0427\\u0442\\u043e\"}" By default, |
@sydney-runkle Thank you for quick answer. Are you planning to return this functionality? Now it produces more boilerplate code and built-in |
At the moment, we're not planning on adding this functionality - the performance isn't slower than it would be in V1, where we just had a catch-all **kwargs that passed those values onto We could consider adding support for flags like this on a case by case basis, though I'm not sure how high the demand is for this specific flag. Thanks for following up! |
This gave me some headache as well! I was using Now I have to use |
Discussed in #8821
Originally posted by FyZzyss February 15, 2024
How to ensure_ascii in Pydantic v2?
SomeModel.model_dump_json()
in V2 not ensure ascii symbols anymore.Example:
The text was updated successfully, but these errors were encountered: