Registry / serialization / geojson-pydantic

geojson-pydantic

JSON →
library2.1.2pypypi✓ verified 23d ago

geojson-pydantic provides a suite of Pydantic models that strictly adhere to the GeoJSON specification (RFC 7946). These models are invaluable for creating, validating, and working with GeoJSON data in a type-safe manner. The library is actively maintained, supporting Python 3.9 and above, with a consistent release cadence that includes performance improvements and Pydantic V2 compatibility.

pip install geojson-pydantic
INSTALL
IMPORT
SIG · GEOJSON-PYDANTIC
G
geojson-pydantic
serializationpythonv2.1.2
Install
3.3s avg
Import
430ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.444s · 27.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.3s · import 0.416s · 27MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Point
from geojson_pydantic import Point
Feature
from geojson_pydantic import Feature
FeatureCollection
from geojson_pydantic import FeatureCollection
GeometryCollection
from geojson_pydantic import GeometryCollection

Demonstrates how to create a simple GeoJSON Point and Feature object using geojson-pydantic, validating and serializing them to JSON. Note the use of `model_dump_json()` for serialization with Pydantic V2.

from geojson_pydantic import Feature, Point # Define a GeoJSON Point object data geoj_point_data = { "type": "Point", "coordinates": [-105.01621, 39.57422] } # Create a Pydantic Point model instance point_obj = Point(**geoj_point_data) print(f"Point Object: {point_obj.model_dump_json()}") # Define a GeoJSON Feature object data geoj_feature_data = { "type": "Feature", "geometry": geoj_point_data, # Use the previously defined point data "properties": {"name": "Example Feature", "value": 123} } # Create a Pydantic Feature model instance feature_obj = Feature(**geoj_feature_data) print(f"Feature Object: {feature_obj.model_dump_json()}")
Debug
Known issues
breakingMigration to Pydantic V2.0: geojson-pydantic versions 1.0 and above require Pydantic V2.0+. This involves significant breaking changes from Pydantic V1, including method renames like `dict()` to `model_dump()`, `json()` to `model_dump_json()`, and `parse_obj()` to `model_validate()`. Older geojson-pydantic versions (e.g., <=0.6.3) are incompatible with Pydantic V2.
fix
Upgrade Pydantic to V2 and update your code to use the new `model_` prefixed methods. For instance, `my_model.json()` becomes `my_model.model_dump_json()`. Review Pydantic's official migration guide for V1 to V2 changes.
affects: >=1.0.0
breakingRemoved custom iteration, length, and item access for `GeometryCollection` and `FeatureCollection`: Version 2.0.0 removed custom `__iter__`, `__getitem__`, and `__len__` methods. Direct iteration or `len()` calls on these objects will now fail.
fix
For iteration, use the `.iter()` method (e.g., `for geom in collection.iter()`). For length, use the `.length` property (e.g., `collection.length`). For item access, access the underlying lists directly (e.g., `collection.geometries[0]` or `collection.features[0]`).
affects: >=2.0.0
breakingChanged generic typing for `FeatureCollection`: From version 1.0, the generic `FeatureCollection` model now expects a generic `Feature` model. The type hint has changed from `FeatureCollection[Geometry, Properties]` to `FeatureCollection[Feature[Geometry, Properties]]`.
fix
Update generic type hints for `FeatureCollection` to nest the `Feature` type. For example, if you had `MyFc = FeatureCollection[Polygon, CustomProperties]`, it should now be `MyFc = FeatureCollection[Feature[Polygon, CustomProperties]]`.
affects: >=1.0.0
gotchaMissing 'type' attribute when creating GeoJSON objects: All GeoJSON objects require a 'type' attribute (e.g., 'Point', 'Feature', 'Polygon') as part of their data structure, which is then validated by geojson-pydantic. Omitting this will lead to validation errors.
fix
Always include the mandatory 'type' field with the correct GeoJSON object type string when constructing data for geojson-pydantic models. For example: `Point(type="Point", coordinates=[...])`.
affects: All versions
Errors
Common errors & fixes
ValidationError: ... Input should be less than or equal to 180 [type=less_than_equal, input_value=200, input_type=int]
The input GeoJSON data violates the GeoJSON specification (RFC 7946) or the Pydantic model's constraints, such as invalid coordinate ranges, incorrect type values, or missing required fields.
fix
Correct the GeoJSON data to adhere to the specification; for instance, ensure longitude values are within [-180, 180] and latitude values within [-90, 90], and all mandatory fields (like 'type' and 'coordinates' for Point) are present and correctly typed.
AttributeError: 'Point' object has no attribute 'dict'
This error occurs when attempting to use Pydantic V1 methods (e.g., `dict()`, `json()`, `parse_obj()`) on a `geojson-pydantic` model, which is built on Pydantic V2 and uses renamed methods.
fix
Migrate the method calls to their Pydantic V2 equivalents: use `model_dump()` instead of `dict()`, `model_dump_json()` instead of `json()`, and `model_validate()` instead of `parse_obj()` or `parse_raw()`.
AttributeError: 'dict' object has no attribute 'type'
You are attempting to access an attribute using dot notation (e.g., `my_data.type`) on a standard Python dictionary (`dict`) that you expected to be a `geojson-pydantic` model instance. Raw dictionaries do not support attribute-style access.
fix
Instantiate the `geojson-pydantic` model with your dictionary (e.g., `point_model = PointModel(**my_data)`) before using dot notation, or use dictionary-style key access (e.g., `my_data['type']`) if you intend to work with a dictionary.
TypeError: unhashable type: 'Point'
This error arises when a mutable `geojson-pydantic` model instance (like `Point` or other geometries) is used in a context that requires hashable objects, such as dictionary keys, set elements, or certain internal operations during OpenAPI/Swagger schema generation.
fix
To make model instances hashable, configure `frozen=True` within the model's `model_config` (e.g., `class Config: frozen = True` in Pydantic V1, or `model_config = ConfigDict(frozen=True)` in Pydantic V2). Alternatively, convert the model to an immutable, hashable representation if appropriate for the use case.
AttributeError: 'Point' object has no attribute 'json'
In Pydantic V2 (which geojson-pydantic v2.x.x uses), the `.json()` method for serializing a model to a JSON string was replaced by `.model_dump_json()`.
fix
Use `model_instance.model_dump_json()` to serialize a geojson-pydantic model to a JSON string, or `model_instance.model_dump()` to get a dictionary representation.
Upgrade
Version history
2.1.2latest on PyPI · released Aug 25, 2026
Audit
Dependencies
pydanticrequiredCore dependency for data validation and modeling; geojson-pydantic v1.0+ requires Pydantic v2.0+.
Agent activity
5 hits · last 30 days
node
4
Resources
geojson-pydantic — pip install geojson-pydantic · libregistry