Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda