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

Fast API example fails with PydanticSchemaGenerationError #160

Open
boaza opened this issue Dec 25, 2024 · 3 comments
Open

Fast API example fails with PydanticSchemaGenerationError #160

boaza opened this issue Dec 25, 2024 · 3 comments

Comments

@boaza
Copy link

boaza commented Dec 25, 2024

Hi and thanks for a great library!

I've tried running the FastAPI example as-is, and I get the following error:
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'sqlalchemy_file.file.File'>. Set arbitrary_types_allowed=True in the model_config to ignore this error or implement __get_pydantic_core_schema__ on your type to fully support it.

  • I did notice some version limitations in the pyproject.toml file. Could this be the issue?
  • Should I set arbitrary_types_allowed=True?
Machine: Windows 11
Python: 3.12.8

Libraries:
annotated-types==0.7.0
anyio==4.7.0
apache-libcloud==3.8.0
certifi==2024.12.14
charset-normalizer==3.4.1
click==8.1.8
colorama==0.4.6
fastapi==0.115.6
fasteners==0.19
greenlet==3.1.1
h11==0.14.0
idna==3.10
pillow==11.0.0
pydantic==2.10.4
pydantic_core==2.27.2
requests==2.32.3
sniffio==1.3.1
SQLAlchemy==2.0.36
sqlmodel==0.0.22
starlette==0.41.3
typing_extensions==4.12.2
urllib3==2.3.0
uvicorn==0.34.0
@boaza
Copy link
Author

boaza commented Dec 26, 2024

Installed with exact versions, and the example runs.
This is probably due to FastAPI switching to Pydantic 2 (the original versions resolve to Pydantic 1).

@boaza
Copy link
Author

boaza commented Dec 29, 2024

Not sure if this is the correct approach, but solved using this:

from pydantic import JsonValue

class Category(CategoryBase, table=True):
    image: Union[JsonValue, UploadFile, None] = Field(
        sa_column=Column(
            ImageField(
                upload_storage="category",
                thumbnail_size=(200, 200),
                validators=[SizeValidator(max_size="1M")],
            )
        )
    )

@supermacro
Copy link

The fastapi example also did not work for me "out of the box". But adding model_config = ConfigDict(arbitrary_types_allowed=True) worked for me:

class PersonBase(SQLModel):
    name: str

class Person(PersonBase, table=True):
    __tablename__ = "person"

    # Allow arbitrary types like File in model
    model_config = ConfigDict(arbitrary_types_allowed=True)

    image: Union[File, UploadFile, None] = Field(
        sa_column=Column(
            ImageField(
                upload_storage="category",
                thumbnail_size=(200, 200),
                validators=[SizeValidator(max_size="1M")],
            )
        )
    )

class PersonOut(PersonBase):
    image: FileInfo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants