SQLModel: Simplifying SQL Databases in Python with Pydantic and SQLAlchemy

This repository profile is provided by osrepos.com, an open source repository discovery platform.

SQLModel: Simplifying SQL Databases in Python with Pydantic and SQLAlchemy

Summary

SQLModel is a Python library designed for intuitive, compatible, and robust interaction with SQL databases. Built on Pydantic and SQLAlchemy, it streamlines database operations, especially within FastAPI applications, by leveraging Python type annotations. It aims to minimize code duplication and enhance developer experience with excellent editor support.

Repository Information

Analyzed by OSRepos on December 8, 2025

Topics

Click on any tag to explore related repositories

Use at your own risk

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.

Introduction

SQLModel is a powerful Python library for interacting with SQL databases, designed for simplicity, compatibility, and robustness. It builds upon the strengths of Pydantic for data validation and SQLAlchemy for database interaction, offering a seamless experience for developers. Created by the author of FastAPI, SQLModel is particularly well-suited for FastAPI applications, aiming to reduce code duplication and enhance developer experience.

Key features of SQLModel include:

  • Intuitive to write: Excellent editor support with autocompletion everywhere, reducing debugging time and making it easy to learn.
  • Easy to use: Sensible defaults simplify the code you write, handling much of the underlying complexity.
  • Compatible: Designed for high compatibility with FastAPI, Pydantic, and SQLAlchemy.
  • Extensible: Provides access to the full power of SQLAlchemy and Pydantic when needed.
  • Short: Minimizes code duplication, allowing a single type annotation to perform extensive work without needing separate SQLAlchemy and Pydantic models.

Installation

To get started with SQLModel, ensure you have a virtual environment set up. You can install it using pip:

pip install sqlmodel

Examples

SQLModel simplifies common database operations. Here are some quick examples to illustrate its usage.

Create a SQLModel Model

Define your database table structure using a Python class that inherits from SQLModel and sets table=True. Each class attribute corresponds to a table column.

from sqlmodel import Field, SQLModel


class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str
    secret_name: str
    age: int | None = None

Create Rows

Create instances of your SQLModel class to represent rows in your table.

hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)

Write to the Database

Combine your model definitions and instances with an engine and session to persist data to a database. This example uses SQLite.

from sqlmodel import Field, Session, SQLModel, create_engine


class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str
    secret_name: str
    age: int | None = None


hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)


engine = create_engine("sqlite:///database.db")


SQLModel.metadata.create_all(engine);

with Session(engine) as session:
    session.add(hero_1)
    session.add(hero_2)
    session.add(hero_3)
    session.commit()

Select from the Database

Query data from your database using the select function and session. SQLModel ensures you retain excellent editor support even after retrieving data.

from sqlmodel import Field, Session, SQLModel, create_engine, select


class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str
    secret_name: str
    age: int | None = None


engine = create_engine("sqlite:///database.db")

with Session(engine) as session:
    statement = select(Hero).where(Hero.name == "Spider-Boy")
    hero = session.exec(statement).first()
    print(hero)

Why Use SQLModel

SQLModel offers several compelling reasons for Python developers, especially those working with FastAPI:

  • Unified Models: It acts as both a SQLAlchemy model and a Pydantic model, eliminating the need to define separate models for database interaction and data validation/serialization. This significantly reduces code duplication.
  • Exceptional Developer Experience: Leveraging Python type annotations, SQLModel provides excellent editor support, including autocompletion and inline error checking, both when defining models and when querying data.
  • FastAPI Integration: Designed by the creator of FastAPI, SQLModel provides a natural and highly compatible way to integrate SQL databases into FastAPI applications.
  • Robust and Extensible: While simplifying common tasks, it retains the full power and extensibility of SQLAlchemy and Pydantic underneath, allowing for complex scenarios when needed.

Links

For more detailed information and comprehensive guides, refer to the official documentation and source code:

Related repositories

Similar repositories that may be relevant next.

TensorRT-LLM: Optimizing Large Language Model Inference on NVIDIA GPUs

TensorRT-LLM: Optimizing Large Language Model Inference on NVIDIA GPUs

July 3, 2026

TensorRT-LLM is an open-source library by NVIDIA designed to optimize inference for Large Language Models (LLMs) and Visual Generation models. It offers a user-friendly Python API, state-of-the-art optimizations, and specialized kernels to ensure efficient performance on NVIDIA GPUs. This powerful tool enables developers to deploy LLMs with high throughput and low latency, from single-GPU setups to multi-node deployments.

PythonLLMInference Optimization
DataDreamer: Streamlining Synthetic Data Generation and LLM Workflows

DataDreamer: Streamlining Synthetic Data Generation and LLM Workflows

July 3, 2026

DataDreamer is an open-source Python library designed for efficient prompting, synthetic data generation, and model training workflows. It simplifies the process of creating complex LLM workflows, generating high-quality synthetic datasets, and aligning or fine-tuning models. Built to be simple, efficient, and research-grade, DataDreamer empowers users to build reproducible and shareable AI solutions.

PythonLLMSynthetic Data
EasyInstruct: An Easy-to-Use Instruction Processing Framework for LLMs

EasyInstruct: An Easy-to-Use Instruction Processing Framework for LLMs

July 2, 2026

EasyInstruct is an open-source Python framework designed to simplify instruction processing for Large Language Models (LLMs). Accepted at ACL 2024, it offers modularized components for instruction generation, selection, and prompting, supporting various LLMs like GPT-4 and LLaMA. This framework is ideal for researchers and developers working on LLM-based experiments and applications.

EasyInstructLLM FrameworkPython
LazyLLM: Low-Code Development for Multi-Agent LLM Applications

LazyLLM: Low-Code Development for Multi-Agent LLM Applications

July 2, 2026

LazyLLM offers a low-code development tool designed for building multi-agent LLM applications with ease. It simplifies the creation of complex AI applications, providing a streamlined workflow for rapid prototyping, data feedback, and iterative optimization. Developers can leverage its extensive features for deployment, cross-platform compatibility, and efficient model fine-tuning.

PythonAI DevelopmentMulti-Agent

Source repository

Open the original repository on GitHub.

View on GitHub
OS
OSRepos

Analysis and discovery of open source repositories. Find interesting projects and follow their updates.

Monitor your website with YourWebsiteScore

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of third-party repository code is at your own risk. Always review source code, dependencies, licenses, and security implications before running anything.

© 2025 OSRepos. Built with Nuxt 3 and lots of ❤️