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

1.1K
Views
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 answers
Answer question

0

Try this:

from typing import List

and change response_model=User to:

response_model=List[User]
over 4 years ago · Santiago Trujillo Report
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!