Pydantic 2.6.4 -> 2.7.0: expected enum but got str serialized value may not be as expected #9270
Replies: 3 comments 6 replies
-
Interestingly, we have very same issue on pydantic 2.6.4 🤔 |
Beta Was this translation helpful? Give feedback.
-
@hhartzer I also encountered this issue, is there a way to overcome that impediment? |
Beta Was this translation helpful? Give feedback.
-
Here's an example that's reproducible, but I suspect it's an operator error. from enum import Enum
from pydantic import BaseModel
class FruitEnum(str, Enum):
pear = 'pear'
banana = 'banana'
class CookingModel(BaseModel):
fruit: FruitEnum | None = None
# no warnings emitted since the enum is used in the model construction
print(CookingModel(fruit='banana').model_dump_json(indent=2))
cm = CookingModel()
cm.fruit = 'pear'
print(cm.model_dump_json(indent=2))
# warning: "Expected `enum` but got `str` - serialized value may not be as expected" After starting a bug report, I realized that it's partially my fault, since I'm assigning the value of Changing to: cm.fruit = FruitEnum('pear') resolves the warning, as it's using the string |
Beta Was this translation helpful? Give feedback.
-
I'd like to open this as an issue, but I don't have a reproducible example yet. Hoping someone else may have seen this.
FastAPI 0.101.1, sqlmodel 0.0.16, Python 3.11.
Going from Pydantic 2.6.4 to 2.7.0, some heavy endpoints, maybe somewhat randomly (and more often than not), will spew
expected enum but got str serialized value may not be as expected
over and over. It seems to be able to lock thing up for minutes at a time, if left alone.I do have some enums. I suspect this is related to these:
#9045
#9064
Which showed up in 2.7.0.
When I reverted (solely) back to 2.6.4, all was well.
This might definitely be my bug in some way, but I feel like this is a breaking change to some degree or another.
Beta Was this translation helpful? Give feedback.
All reactions