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
Literal
annotation does not render type to json-schema
#8905
Comments
Related to this, it would be nice if As a workaround, I'm using a dynamically generated string enum. |
@WillDaSilva does the following not work for you? from typing import Literal
from pydantic import BaseModel
class A(BaseModel):
x: Literal['a', 'b', 'c']
print(A.model_json_schema())
#> {'properties': {'x': {'enum': ['a', 'b', 'c'], 'title': 'X', 'type': 'string'}}, 'required': ['x'], 'title': 'A', 'type': 'object'} More generally though, I think we should do a better job of handling single-item literals as I have discovered some JSON-schema-consuming tools that misbehave if you use just a plain Hoping to have some improvements here shortly. |
@dmontagu That works for when the list of possible values is static (i.e. hard-coded), but when you have the list of strings as a variable it does not work. |
Ah, now I understand. Not sure how the other maintainers feel but I wouldn't be opposed to adding that option to @WillDaSilva I'll just note though in case it is useful that I think it is still technically possible to using from typing import Literal, TYPE_CHECKING
from pydantic import BaseModel
choices = ['a', 'b', 'c']
if TYPE_CHECKING:
# make type-checkers just see `str` so that they don't complain about the dynamic literal
MyStringChoices = str
else:
MyStringChoices = Literal[*choices]
class MyModel(BaseModel):
x: MyStringChoices
print(MyModel.model_json_schema())
#> {'properties': {'x': {'enum': ['a', 'b', 'c'], 'title': 'X', 'type': 'string'}}, 'required': ['x'], 'title': 'MyModel', 'type': 'object'} but I would agree that having |
@bruno-f-cruz does the change proposed in #8944 address your issue, or do you otherwise have any problems with it? It goes a bit farther than just adding the type in order to also address other issues at the same time, but if you (or anyone) have a problem with that change then it might be more reasonable to scale it back. |
I don't have an informed opinion on the full scope of your PR. That being said, it would solve this issue. Thanks! |
@dmontagu Sorry if i am too late to the party but I failed to noticed something in the previous test. While the string type is indeed added to the schema, the Literal[None] does not give rise to a typing in the schema. Can you think of a reason why it would be a bad idea to add this typing to the PR too? Thanks! |
@sydney-runkle Would it be possible to open this issue given my last comment or should I open a new one? |
Ah, thanks for the ping. Let's see what @dmontagu thinks! |
I think it was just an oversight. Would you like to open a PR adding better handling for |
@dmontagu @sydney-runkle Check #9135. Would this be ok? |
Looks great, please add a test! |
Initial Checks
Description
When defining a property as
Literal
, eg:The output json-schema model loses the typing of the variable used for the literal. I.e.:
It would be nice to infer this type and correctly add it to the schema. This could be achieved by simply extending the
literal_schema
methodpydantic/pydantic/json_schema.py
Line 722 in 9811727
with:
Example Code
No response
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: