Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Different behavior Field in pydantic and SQLModel in OpenAPI spec

To my knowledge, SQLmodel is partially based on pydantic. Nevertheless, I get a different resulting OpenAPI specification when using the Field function from SQLmodel in FastAPI.

For example, look at the following example:

from typing import Optional
from fastapi import FastAPI
from sqlmodel import Field, SQLModel

app = FastAPI()

class Model(SQLModel):
    nullable_param: Optional[int] = Field(None, nullable=True)

@app.get("/test", response_model=Model)
def foo():
    return Model(nullable_param=3)

This code runs and a GET on the test endpoint correctly returns {"nullable_param":3}. However, the resulting OpenAPI specification for Model is the following:

{
    "Model":{
        "title":"Model",
        "type":"object",
        "properties":{
            "nullable_param":{"title":"Nullable Param","type":"integer"}
        }
    }
}

Note that nullable_param lacks the nullable=true attribute!

When I import Field directly from pydantic (from pydantic import Field), it does include this attribute:

{
    "Model":{
        "title":"Model",
        "type":"object",
        "properties":{
             "nullable_param":{ "title":"Nullable Param", "type":"integer", "nullable":true }
        }
    }
}

Am I doing something wrong, or is there perhaps a bug in the Field implementation of SQLModel in a FastAPI context?

pydantic 1.8.2 fastapi 0.74.1 sqlmodel 0.0.6 python 3.8.12

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!