-
-
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
Difference between TypeAdapter().json_schema and BaseModel.model_json_schema #9210
Comments
Thanks for reporting this. Definitely a bug - the schemas should match. I think we do actually want to merge the field infos like is done in the Marking as a good first issue and adding to our next milestone! |
@sydney-runkle I can work on this one. |
@sydney-runkle While working on the issue I found something that I want to clarify. from pprint import pprint
from typing import Annotated
from pydantic import TypeAdapter, BaseModel, Field
def _set_key2(s):
s['key2'] = 'value2'
CustomField = Annotated[
str,
Field(json_schema_extra={'key1': 'value1'}),
Field(json_schema_extra=_set_key2),
Field(json_schema_extra={'key3': 'value3'}),
]
class Foo(BaseModel):
field: CustomField
print("-1. BaseModel.model_json_schema")
pprint(Foo.model_json_schema())
print("-2. TypeAdapter().json_schema")
pprint(TypeAdapter(CustomField).json_schema()) Result
Note that key2 is set by a callable
|
Great question. Seems the problem is even worse than I imagined 😂. I'd direct you to this issue to understand some more context behind modifying json schema when merging field info: #7686. Even though I like the approach of the |
Alright, let's go with the We'll want to document this as a breaking change, and illustrate to users how they can delete keys via callable json schemas! |
Initial Checks
Description
When presented with an annotated type with multiple
Field
objects that setjson_schema_extra
,TypeAdapter
seems to combine them whereasBaseModel
does not.I was used to the behavior of
BaseModel
of not combining anything, which I can understand because merging json schema's is non-trivial in the case of conflicting keys. I would expectTypeAdapter
to follow the same rules.Below code example outputs the following
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: