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
Validation error with str Enum field and numpy's str_ type #9265
Comments
Can you check if this is new in V2.7? |
Yes, the example works fine in 2.6.4 |
I'm having exactly the same issue, with the only difference that instead of numpy my enums are evaluated on django Code example:
Output:
|
If you inherit from from enum import Enum
from pydantic import BaseModel, Field
import numpy as np
class Animal(str, Enum):
dog = "dog"
cat = "cat"
class ModelStr(BaseModel):
animal: str = Field(..., description="A type of animal")
class ModelEnum(BaseModel):
animal: Animal = Field(..., description="A type of animal")
numpy_array = np.array(["cat", "dog"])
# this works
ModelStr(animal=numpy_array[0])
# this works
ModelEnum(animal=numpy_array[0]) This workaround should work for now, but I'll add this to the 2.7 fixes milestone, as I don't think this extra specification should be required |
Interesting that this still works, we must be missing a from enum import Enum
from pydantic import BaseModel, Field
class Animal(Enum):
dog = "dog"
cat = "cat"
class ModelEnum(BaseModel):
animal: Animal = Field(..., description="A type of animal")
# this works
ModelEnum(animal="cat") |
Hi -- still working on a fix for this. Will wait on 2.7.2 most likely, hopefully early next week :). See pydantic/pydantic-core#1273 (comment) |
Initial Checks
Description
I'm having this issue in the API that uses machine learning (numpy+scikit-learn+xgboost) to generate predictions. The predictions are text labels. The ML part returns predictions as numpy-specific strings of type str_.
These values trigger validation error when used in the ENUM fields in pydantic model:
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: