Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

stac-pydantic

GitHub Workflow Status (with event)

Pydantic models for STAC Catalogs, Collections, Items, and the STAC API spec. Initially developed by arturo-ai.

The main purpose of this library is to provide reusable request/response models for tools such as fastapi. For more comprehensive schema validation and robust extension support, use pystac.

Installation

python -m pip install stac-pydantic

# or

python -m pip install stac-pydantic["validation"]
stac-pydantic STAC Version STAC API Version Pydantic Version
1.2.x 1.0.0-beta.1 <1* ^1.6
1.3.x 1.0.0-beta.2 <1* ^1.6
2.0.x 1.0.0 <1* ^1.6
3.0.x 1.0.0 1.0.0 ^2.4
3.1.x 1.0.0 1.0.0 ^2.4

* various beta releases, specs not fully implemented

Usage

Loading Models

Load data into models with standard pydantic:

from stac_pydantic import Catalog

stac_catalog = {
  "type": "Catalog",
  "stac_version": "1.0.0",
  "id": "sample",
  "description": "This is a very basic sample catalog.",
  "links": [
    {
      "href": "item.json",
      "rel": "item"
    }
  ]
}

catalog = Catalog(**stac_catalog)
assert catalog.id == "sample"
assert catalog.links[0].href == "item.json"

Extensions

STAC defines many extensions which let the user customize the data in their catalog. stac-pydantic.extensions.validate_extensions gets the JSON schemas from the URLs provided in the stac_extensions property (caching the last fetched ones), and will validate a dict, Item, Collection or Catalog against those fetched schemas:

from stac_pydantic import Item
from stac_pydantic.extensions import validate_extensions

stac_item = {
    "id": "12345",
    "type": "Feature",
    "stac_extensions": [
        "https://stac-extensions.github.io/eo/v1.0.0/schema.json"
    ],
    "geometry": { "type": "Point", "coordinates": [0, 0] },
    "bbox": [0.0, 0.0, 0.0, 0.0],
    "properties": {
        "datetime": "2020-03-09T14:53:23.262208+00:00",
        "eo:cloud_cover": 25,
    },
    "links": [],
    "assets": {},
}

model = Item(**stac_item)
validate_extensions(model, reraise_exception=True)
assert getattr(model.properties, "eo:cloud_cover") == 25

The complete list of current STAC Extensions can be found here.

Vendor Extensions

The same procedure described above works for any STAC Extension schema as long as it can be loaded from a public url.

STAC API

The STAC API Specs extent the core STAC specification for implementing dynamic catalogs. STAC Objects used in an API context should always import models from the api subpackage. This package extends Catalog, Collection, and Item models with additional fields and validation rules and introduces Collections and ItemCollections models and Pagination/ Search Links. It also implements models for defining ItemSeach queries.

from stac_pydantic.api import Item, ItemCollection

stac_item = Item.model_validate(
    {
        "id": "12345",
        "type": "Feature",
        "stac_extensions": [],
        "geometry": { "type": "Point", "coordinates": [0, 0] },
        "bbox": [0.0, 0.0, 0.0, 0.0],
        "properties": {
            "datetime": "2020-03-09T14:53:23.262208+00:00",
        },
        "collection": "CS3",
        "links": [
            {
                "rel": "self",
                "href": "http://stac.example.com/catalog/collections/CS3-20160503_132130_04/items/CS3-20160503_132130_04.json"
            },
            {
                "rel": "collection",
                "href": "http://stac.example.com/catalog/CS3-20160503_132130_04/catalog.json"
            },
            {
                "rel": "root",
                "href": "http://stac.example.com/catalog"
            }
        ],
        "assets": {},
    }
)

stac_item_collection = ItemCollection(**{
    "type": "FeatureCollection",
    "features": [stac_item],
    "links": [
          {
            "rel": "self",
            "href": "http://stac.example.com/catalog/search?collection=CS3",
            "type": "application/geo+json"
          },
          {
            "rel": "root",
            "href": "http://stac.example.com/catalog",
            "type": "application/json"
          }],
    })

Exporting Models

Most STAC extensions are namespaced with a colon (ex eo:gsd) to keep them distinct from other extensions. Because Python doesn't support the use of colons in variable names, we use Pydantic aliasing to add the namespace upon model export. This requires exporting the model with the by_alias = True parameter. Export methods (model_dump() and model_dump_json()) for models in this library have by_alias and exclude_unset st to True by default:

item_dict = item.model_dump()
assert item_dict['properties']['landsat:row'] == item.properties.row == 250

Required keys

STAC specification requires some keys to be present even if their value is null. When exporting a model to dict or json, stac-pydantic will make sure to keep the keys even if exclude_none is set to True. Users can overwrite this by using exclude={'key'}.

from stac_pydantic.api import Item

stac_item = Item.model_validate(
    {
        "id": "12345",
        "type": "Feature",
        "stac_extensions": [],
        "geometry": None,
        "properties": {
            "datetime": None,
            "start_datetime": "2024-01-01T00:00:00Z",
            "end_datetime": "2024-01-02T00:00:00Z",
        },
        "collection": "collection",
        "links": [
            {
                "rel": "self",
                "href": "http://stac.example.com/catalog/collections/CS3-20160503_132130_04/items/CS3-20160503_132130_04.json"
            },
            {
                "rel": "collection",
                "href": "http://stac.example.com/catalog/CS3-20160503_132130_04/catalog.json"
            },
            {
                "rel": "root",
                "href": "http://stac.example.com/catalog"
            }
        ],
        "assets": {},
    }
)

out = stac_item.model_dump(exclude_none=True)
# `geometry` is required
assert out["geometry"] is None
# `datetime` is a required property
assert out["properties"]["datetime"] is None

# force exclusion of required keys
out = stac_item.model_dump(exclude_none=True, exclude={"properties": {"datetime"}, "geometry": True})
assert "geometry" not in out
assert "datetime" not in out["properties"]

CLI

Usage: stac-pydantic [OPTIONS] COMMAND [ARGS]...

  stac-pydantic cli group

Options:
  --help  Show this message and exit.

Commands:
  validate-item  Validate STAC Item

Contribution & Development

See CONTRIBUTING.md

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stac_pydantic-3.6.0a1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stac_pydantic-3.6.0a1-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file stac_pydantic-3.6.0a1.tar.gz.

File metadata

  • Download URL: stac_pydantic-3.6.0a1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.0 {"ci":true,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.14.3"},"installer":{"name":"hatch","version":"1.17.0"},"openssl_version":"OpenSSL 3.5.5 27 Jan 2026","python":"3.14.3","system":{"name":"Linux","release":"6.17.0-1018-azure"}} HTTPX2/2.4.0

File hashes

Hashes for stac_pydantic-3.6.0a1.tar.gz
Algorithm Hash digest
SHA256 750baccd746a6c92b852c139fedb57aa88833e4ce06728702b8f8e08b5a6a182
MD5 1ca4bff07471207bece89d4b6f97322d
BLAKE2b-256 3ed081a05e38ad8e690502a5678d5383c56b4de8fe28e32969a9ccc1c2f284b2

See more details on using hashes here.

File details

Details for the file stac_pydantic-3.6.0a1-py3-none-any.whl.

File metadata

  • Download URL: stac_pydantic-3.6.0a1-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.0 {"ci":true,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.14.3"},"installer":{"name":"hatch","version":"1.17.0"},"openssl_version":"OpenSSL 3.5.5 27 Jan 2026","python":"3.14.3","system":{"name":"Linux","release":"6.17.0-1018-azure"}} HTTPX2/2.4.0

File hashes

Hashes for stac_pydantic-3.6.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 fbeaa8a4c6562860747cdeb23ab75c1f0146b612f098ec08dd79a9fc19a33cc6
MD5 41e8bd176b2ec1f4420adc5ec55122a9
BLAKE2b-256 47b5715cba33c512e7744030fb637de322f2a7961dc3253ad12208e4fada3f9a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page