-
First Check
Commit to Help
Example Codefrom typing import Any
from fastapi import FastAPI
from pydantic import BaseModel, ConfigDict
# models
class CustomType:
...
@classmethod
def __get_pydantic_json_schema__(cls, *args: Any):
return {'type': 'string', 'format': 'date-time', 'description': 'ISO 8601 date-time string'}
class MyModel(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
custom_field: CustomType
# app
app = FastAPI()
@app.get("/", response_model=MyModel)
def test():
return MyModel(custom_field=CustomType())DescriptionAfter upgrading to 0.119.0, It worked normally in version 0.118.3. Response on 0.118.3Error on 0.119.0Operating SystemLinux Operating System DetailsDebial bullseye FastAPI Version0.119.0 Pydantic Version2.12.1 Python Version3.11.13 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
|
Probably related to #14177. In |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo Another similar example which works with 0.118.0 and fails with 0.119.0 from typing import Annotated
import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel, ConfigDict, WithJsonSchema, TypeAdapter
type MyNumpyArray = Annotated[
np.ndarray, WithJsonSchema(TypeAdapter(list[float]).json_schema(), mode="serialization"),
]
class MyModel(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
custom_field: MyNumpyArray
app = FastAPI()
@app.get("/")
def test() -> MyModel:
raise NotImplementedError()
print(app.openapi())
# {'openapi': '3.1.0', 'info': {'title': 'FastAPI', 'version': '0.1.0'}, 'paths': {'/': {'get': {'summary': 'Test', 'operationId': 'test__get', 'responses': {'200': {'description': 'Successful Response', 'content': {'application/json': {'schema': {'$ref': '#/components/schemas/MyModel'}}}}}}}}, 'components': {'schemas': {'MyModel': {'properties': {'custom_field': {'$ref': '#/components/schemas/MyNumpyArray'}}, 'type': 'object', 'required': ['custom_field'], 'title': 'MyModel'}, 'MyNumpyArray': {'items': {'type': 'number'}, 'type': 'array'}}}} |
Beta Was this translation helpful? Give feedback.
-
|
It still does not work in |
Beta Was this translation helpful? Give feedback.
-
|
Thanks all for the reports and discussion! I created an issue here for completeness: #14483 It should be fixed in #14482, available in FastAPI 0.124.1, just released. 🎉 |
Beta Was this translation helpful? Give feedback.
Thanks all for the reports and discussion! I created an issue here for completeness: #14483
It should be fixed in #14482, available in FastAPI 0.124.1, just released. 🎉