-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
model_json_schema
fails with empty enum
#7896
Comments
Perhaps a more practical example is like below, where you would practically provide an empty dict for from pydantic import BaseModel
from enum import Enum
class MyEnum(Enum):
pass
class MyModel(BaseModel):
e: dict[MyEnum, str]
print(MyModel.model_json_schema()) |
I'm really curious, what's the goal here? Do you have empty enums? Do you actually want the only valid value for |
Yes and yes. In my case the empty enums are there as part of a pattern and just happen to be empty. Perhaps things could be structured "better" to make that not a thing but 🤷 Like uhh here's a contrived example from pydantic import BaseModel
from enum import StrEnum
class GoodJapaneseCars(StrEnum):
...
class GoodSouthAfricanCars(StrEnum):
pass # Well maybe they'll make some in the future :)
class MyModel(BaseModel):
japanese_cars: list[GoodJapaneseCars]
south_african_cars: list[GoodSouthAfricanCars]
print(MyModel.model_json_schema()) |
Seems an intended behavior based on pydantic/pydantic/_internal/_std_types_schema.py Lines 62 to 67 in 4dd40ae
But, let's wait for @dmontagu to make it more clear. |
If I put that into https://www.jsonschemavalidator.net/ I get an error: "Enum array must have at least one value" But this seems to do the trick: |
Seems an empty enum is okay per this spec (which pydantic is compliant with), granted it should have at least one element. Seems a bit of a grey area
|
If the issue is just generating a JSON schema, I'm sure we can make that work for enums with no cases. I'm assuming we don't need to actually implement anything different for validation (given there are no cases that it could be validated into). Since right now it just errors, it seems fine to me to make it not error and generate some best-effort JSON schema. If you know what you want the JSON schema to be let me know otherwise I will try to just make it consistent with the existing JSON schemas and just have zero cases. |
It seems risky to produce a schema that's technically spec-compliant but might be refused by a fussy validator. |
Yeah this is the core issue for me - only reason I noticed this issue was because of fastapi docs failing to load. I'm not fussed about exactly what schema gets generated |
Are you able to control the schema generation? If so you can do this in the meantime: from pydantic.json_schema import GenerateJsonSchema
class MyGen(GenerateJsonSchema):
def is_instance_schema(self, *_):
return {"enum": []}
print(MyModel.model_json_schema(schema_generator=MyGen)) |
Cheers I'll have a play with that |
Initial Checks
Description
When a field is annotated as an enum and that enum has no members then
model_json_schema
fails.MRE and traceback below.
In pydantic v1 this produces
{"title": "MyModel", "type": "object", "properties": {"e": {"title": "E", "enum": []}}, "required": ["e"]}
(usingschema_json
).Example Code
~~ see above
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: