Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Problem with Python, FastAPI, Pydantic and SQLAlchemy

I'm trying to build a Python FastAPI blog system using SQLAlchemy with SQLite and am having problems using/understanding the response_model parameter of the API decorator. Here's a SQLAlchemy model:

class User(SqlAlchemyBase):
__tablename__ = 'user'
__table_args__ = {"keep_existing": True}

user_uid: int = sa.Column(GUID, primary_key=True, default=GUID_DEFAULT_SQLITE)
first_name: str = sa.Column(sa.String)
last_name: str = sa.Column(sa.String)
email: str = sa.Column(sa.String, index=True, unique=True)
posts: List["Post"] = relationship("Post", backref="user")
created: datetime = sa.Column(sa.DateTime, default=datetime.now(tz=timezone.utc), index=True)
updated: datetime = sa.Column(sa.DateTime, default=datetime.now(
    tz=timezone.utc), onupdate=datetime.now(tz=timezone.utc), index=True)

Here's the Pydantic schema's for a User:

from datetime import datetime
from pydantic import BaseModel

class UserBase(BaseModel):
    first_name: str
    last_name: str
    email: str

class UserInDB(UserBase):
    user_uid: int
    created: datetime
    updated: datetime

    class Config:
        orm_mode = True

class User(UserInDB):
    pass

Here's a ULR endpoint to get a single user that has the response_model parameter included:

from typing import List
import fastapi
from starlette import status
from backend.schema.user import User
from backend.services import user_service

router = fastapi.APIRouter()

@router.get("/api/users/{user_uid}", response_model=User, status_code=status.HTTP_200_OK)
async def get_one_user(user_uid: str = None) -> User:
    return await user_service.get_user(user_uid)

If I execute the above call in the OpenAPI docs created by FastAPI I get an internal server error in the OpenAPI docs and these errors in the console that's running the FastAPI application:

File "/Users/dougfarrell/projects/blog/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 137, in serialize_response
    raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 6 validation errors for User
response -> first_name
  field required (type=value_error.missing)
response -> last_name
  field required (type=value_error.missing)
response -> email
  field required (type=value_error.missing)
response -> user_uid
  field required (type=value_error.missing)
response -> created
  field required (type=value_error.missing)
response -> updated
  field required (type=value_error.missing)

If I take the response_model parameter out of the @router.get(...) decorator I get back the results of the SQLAlchemy query, but no Pydantic serialization. I don't know what the error stack trace is trying to tell me. Can anyone offer some suggestions, advice or resources that might help me figure out what I'm doing wrong?

I'm using Python version 3.10.1, FastAPI version 0.70.1 and SQLAlchemy version 1.4.29.

Thanks!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this:

from typing import List

and change response_model=User to:

response_model=List[User]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda