{"id":4547,"library":"geojson-pydantic","title":"Pydantic GeoJSON Models","description":"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.","status":"active","version":"2.1.1","language":"en","source_language":"en","source_url":"https://github.com/developmentseed/geojson-pydantic","tags":["geojson","pydantic","gis","geometry","validation","geospatial"],"install":[{"cmd":"pip install geojson-pydantic","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for data validation and modeling; geojson-pydantic v1.0+ requires Pydantic v2.0+.","package":"pydantic","optional":false}],"imports":[{"symbol":"Point","correct":"from geojson_pydantic import Point"},{"symbol":"Feature","correct":"from geojson_pydantic import Feature"},{"symbol":"FeatureCollection","correct":"from geojson_pydantic import FeatureCollection"},{"symbol":"GeometryCollection","correct":"from geojson_pydantic import GeometryCollection"}],"quickstart":{"code":"from geojson_pydantic import Feature, Point\n\n# Define a GeoJSON Point object data\ngeoj_point_data = {\n    \"type\": \"Point\",\n    \"coordinates\": [-105.01621, 39.57422]\n}\n\n# Create a Pydantic Point model instance\npoint_obj = Point(**geoj_point_data)\nprint(f\"Point Object: {point_obj.model_dump_json()}\")\n\n# Define a GeoJSON Feature object data\ngeoj_feature_data = {\n    \"type\": \"Feature\",\n    \"geometry\": geoj_point_data, # Use the previously defined point data\n    \"properties\": {\"name\": \"Example Feature\", \"value\": 123}\n}\n\n# Create a Pydantic Feature model instance\nfeature_obj = Feature(**geoj_feature_data)\nprint(f\"Feature Object: {feature_obj.model_dump_json()}\")","lang":"python","description":"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."},"warnings":[{"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.","message":"Migration 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.","severity":"breaking","affected_versions":">=1.0.0"},{"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]`).","message":"Removed 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.","severity":"breaking","affected_versions":">=2.0.0"},{"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]]`.","message":"Changed 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]]`.","severity":"breaking","affected_versions":">=1.0.0"},{"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=[...])`.","message":"Missing '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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}