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

Validation error with str Enum field and numpy's str_ type #9265

Open
1 task done
maxsch3 opened this issue Apr 17, 2024 · 6 comments · May be fixed by pydantic/pydantic-core#1273
Open
1 task done

Validation error with str Enum field and numpy's str_ type #9265

maxsch3 opened this issue Apr 17, 2024 · 6 comments · May be fixed by pydantic/pydantic-core#1273
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@maxsch3
Copy link

maxsch3 commented Apr 17, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

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:

animal
  Input should be 'dog' or 'cat' [type=enum, input_value='cat', input_type=str_]
    For further information visit https://errors.pydantic.dev/2.7/v/enum

Example Code

from enum import Enum
from pydantic import BaseModel, Field
import numpy as np


class Animal(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 triggers error
ModelEnum(animal=numpy_array[0])

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/max/Code/Drax/dev/role-classification/venv/lib/python3.10/site-packages/pydantic
               python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
                     platform: Linux-5.15.0-102-generic-x86_64-with-glibc2.35
             related packages: fastapi-0.101.1 typing_extensions-4.8.0
                       commit: unknown
@maxsch3 maxsch3 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 17, 2024
@samuelcolvin
Copy link
Member

Can you check if this is new in V2.7?

@maxsch3
Copy link
Author

maxsch3 commented Apr 17, 2024

Yes, the example works fine in 2.6.4

@belinux
Copy link

belinux commented Apr 17, 2024

I'm having exactly the same issue, with the only difference that instead of numpy my enums are evaluated on django models.IntegerChoices.

Code example:

from django.db import models

class Status(models.IntegerChoices):
    UNKNOWN = -1, _("unknown")
    ACCEPTED = 0, _("accepted")
    REJECTED = 1, _("rejected")

Output:

foo_status
  Input should be -1, 0 or 1 [type=enum, input_value=Status.ACCEPTED, input_type=Status]
    For further information visit https://errors.pydantic.dev/2.7/v/enum

@sydney-runkle
Copy link
Member

@maxsch3,

If you inherit from str in your Animal type, all works as expected:

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

@sydney-runkle sydney-runkle added this to the 2.7 fixes milestone Apr 18, 2024
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Apr 18, 2024
@sydney-runkle
Copy link
Member

Interesting that this still works, we must be missing a str conversion attempt somewhere:

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")

@sydney-runkle
Copy link
Member

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)

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

Successfully merging a pull request may close this issue.

4 participants