Pydantic GeoJSON
raw JSON → 0.3.2 verified Fri May 01 auth: no python
Provides Pydantic models for validating GeoJSON objects (Point, LineString, Polygon, Feature, FeatureCollection, etc.) with strict adherence to RFC 7946. Current version 0.3.2, requires Python >=3.9, release cadence irregular.
pip install pydantic-geojson Common errors
error pydantic_core._pydantic_core.ValidationError: 1 validation error for Point coordinates Input should be a valid list [type=list_type, input_value='...', input_type=str] ↓
cause Passing a string instead of a list for coordinates.
fix
Ensure coordinates is a list of numbers: Point(coordinates=[0, 0])
error pydantic_core._pydantic_core.ValidationError: 1 validation error for Feature type Input should be 'Feature' [type=literal_error, input_value='...', input_type=str] ↓
cause The 'type' field must be exactly 'Feature' for Feature objects.
fix
Set type='Feature' when creating a Feature, or let the model default (if any) handle it.
error ModuleNotFoundError: No module named 'pydantic_geojson.models' ↓
cause Trying to import from internal submodules instead of public top-level package.
fix
Use from pydantic_geojson import Point, Feature, etc. Instead of from pydantic_geojson.models.geometry import Point.
Warnings
gotcha Do not include 'type' fields manually for geometry sub-models; they are automatically set by Pydantic validators. ↓
fix Omit 'type' when instantiating model classes like Point, LineString, etc. For example: Point(coordinates=[0, 0]) is valid; Point(type='Point', coordinates=[0, 0]) also works but is redundant.
breaking Version 0.3.0 introduced strict RFC 7946 compliance: Feature objects reject 'coordinates', 'geometries', and 'features' members. Previously such extra fields were silently ignored. ↓
fix If your code previously included extraneous fields in Feature/FeatureCollection dicts, remove them before validation or ensure they are not passed.
gotcha Coordinates for Point must be [longitude, latitude] order (GeoJSON spec), not [latitude, longitude]. ↓
fix Always provide coordinates in [lon, lat] order. e.g., Point(coordinates=[13.405, 52.52]) for Berlin.
Imports
- Point wrong
from pydantic_geojson.models.geometry import Pointcorrectfrom pydantic_geojson import Point - Feature wrong
from pydantic_geojson.models.feature import Featurecorrectfrom pydantic_geojson import Feature - FeatureCollection wrong
from pydantic_geojson.models.feature_collection import FeatureCollectioncorrectfrom pydantic_geojson import FeatureCollection
Quickstart
from pydantic_geojson import Point, Feature, FeatureCollection
# Create a Point
point = Point(type='Point', coordinates=[13.405, 52.52])
print(point)
# Create a Feature
feature = Feature(type='Feature', geometry=point, properties={'name': 'Berlin'})
print(feature)
# Create a FeatureCollection
collection = FeatureCollection(type='FeatureCollection', features=[feature])
print(collection.model_dump_json(indent=2))