Skip to content

Field types

TIP

This is reference documentation. To see how these types appear on a model, head to Getting started.

All field types are importable from the top-level sqlmodel_ext package. They are Annotated type aliases (TypeAlias) that satisfy both Pydantic validation and SQLAlchemy column-type mapping.

All string-constrained types also implicitly carry pattern=r'^[^\x00]*$' (rejects NUL bytes, which PostgreSQL text columns refuse).

String constraints

python
from sqlmodel_ext import Str16, Str24, Str32, Str36, Str48, Str64, Str100, Str128, Str255, Str256, Str500, Str512, Str2048
Typemax_lengthEquivalent definition
Str1616Annotated[str, Field(max_length=16), _NO_NULL_BYTE]
Str2424same form
Str3232same form
Str3636same form (UUID standard string length)
Str4848same form
Str6464same form
Str100100same form
Str128128same form
Str255255same form
Str256256same form
Str500500same form
Str512512same form
Str20482048same form

Text constraints

python
from sqlmodel_ext import Text1K, Text1024, Text2K, Text2500, Text3K, Text5K, Text10K, Text32K, Text60K, Text64K, Text100K, Text1M
Typemax_length
Text1K1000
Text10241024
Text2K2000
Text25002500
Text3K3000
Text5K5000
Text10K10000
Text32K32000
Text60K60000
Text64K65536
Text100K100000
Text1M1000000

Numeric constraints

python
from sqlmodel_ext import (
    Port, Percentage,
    PositiveInt, NonNegativeInt,
    PositiveBigInt, NonNegativeBigInt,
    PositiveFloat, NonNegativeFloat,
)
TypeRangeDatabase column
Port1 ~ 65535INTEGER
Percentage0 ~ 100INTEGER
PositiveInt1 ~ INT32_MAXINTEGER
NonNegativeInt0 ~ INT32_MAXINTEGER
PositiveBigInt1 ~ JS_MAX_SAFE_INTEGERBIGINT
NonNegativeBigInt0 ~ JS_MAX_SAFE_INTEGERBIGINT
PositiveFloat> 0.0FLOAT
NonNegativeFloat>= 0.0FLOAT

JS_MAX_SAFE_INTEGER upper bound for BigInt

The upper bound of PositiveBigInt / NonNegativeBigInt is JS_MAX_SAFE_INTEGER = 2⁵³ − 1, not INT64_MAX. The reason is that JavaScript JSON parsers lose precision beyond this value. If your API never serves browsers, you can define a custom alias with INT64_MAX as the bound.

Constants

python
from sqlmodel_ext import INT32_MAX, INT64_MAX, JS_MAX_SAFE_INTEGER
ConstantValue
INT32_MAX2_147_483_647 (2³¹−1)
INT64_MAX9_223_372_036_854_775_807 (2⁶³−1)
JS_MAX_SAFE_INTEGER9_007_199_254_740_991 (2⁵³−1)

URL types

python
from sqlmodel_ext import Url, HttpUrl, WebSocketUrl, SafeHttpUrl, UnsafeURLError, validate_not_private_host

Four URL types, all subclassing str, stored as VARCHAR in the database.

TypeAllowed schemesSSRF protection
Urlany (http, ftp, ws, ...)no
HttpUrlhttp / httpsno
WebSocketUrlws / wssno
SafeHttpUrlhttp / httpsyes

SafeHttpUrl rejects:

  • Loopback (localhost, 127.0.0.1, ::1)
  • Private IPs (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)
  • Link-local (169.254.0.0/16)
  • Reserved addresses

A rejected URL raises UnsafeURLError.

validate_not_private_host(host: str) -> None is the underlying check function and can be called directly.

IP address

python
from sqlmodel_ext import IPAddress

Backed by IPv4Address | IPv6Address, automatically validates IPv4/IPv6 format.

Extra method:

python
def is_private(self) -> bool

Returns whether the address is private (including loopback, link-local, etc.).

Path types

python
from sqlmodel_ext.field_types import FilePathType, DirectoryPathType
TypeValidation
FilePathTypePath must include a file name (with extension)
DirectoryPathTypePath must not include a file extension

Behaves as pathlib.Path and can be used like one directly.

Path & naming mixin

python
from sqlmodel_ext import ModuleNameMixin

Adds a module_name: str field to a model (for dynamic-loading / reflection scenarios). See source field_types/mixins/.

PostgreSQL-only types

PostgreSQL only

The types in this section use native PostgreSQL column types and do not work on SQLite / MySQL. Install via pip install sqlmodel-ext[postgresql] or [pgvector].

Array[T]

python
from sqlmodel_ext.field_types.dialects.postgresql import Array

PostgreSQL ARRAY column.

Python representationDatabase column
list[str]TEXT[]
list[int]INTEGER[]
list[float]FLOAT[]
list[UUID]UUID[]

JSON100K / JSONList100K

python
from sqlmodel_ext.field_types.dialects.postgresql import JSON100K, JSONList100K
TypePython representationDatabase columnLength cap
JSON100KdictJSONB100K characters
JSONList100KlistJSONB100K characters

Requires orjson for serialization speed; bundled with the [postgresql] extra.

NumpyVector[dims, dtype]

python
from sqlmodel_ext.field_types.dialects.postgresql import NumpyVector

pgvector + NumPy integration.

ParameterMeaning
dimsVector dimensionality (e.g. 1536)
dtypeNumPy dtype (e.g. numpy.float32)

Requires numpy + pgvector, bundled with the [pgvector] extra.

Exception types

python
from sqlmodel_ext.field_types.dialects.postgresql import (
    VectorError,
    VectorDimensionError,
    VectorDTypeError,
    VectorDecodeError,
)
  • VectorError — base class
  • VectorDimensionError — dimensionality mismatch
  • VectorDTypeError — dtype mismatch
  • VectorDecodeError — deserialization failure