Typed Python models (Pydantic v2) for the Civic Transparency specification
Project description
Civic Transparency – Types
Typed Python models (Pydantic v2) for the Civic Transparency schema.
Maintained by Civic Interconnect.
- Documentation: https://civic-interconnect.github.io/civic-transparency-types/
- Schema Specification: https://civic-interconnect.github.io/civic-transparency-spec/
- Contributing: CONTRIBUTING.md
Overview
This package provides strongly-typed Python models that correspond to the Civic Transparency specification. The types are automatically generated from canonical JSON Schema definitions, ensuring consistency and validation at runtime.
Key Features:
- Type Safety: Full Pydantic v2 validation with IDE support
- Schema Compliance: Generated directly from official JSON schemas
- Privacy-First: Designed for aggregated, non-PII data exchange
- Interoperability: JSON serialization/deserialization with validation
Installation
pip install civic-transparency-types
For development or schema validation:
pip install "civic-transparency-types[dev]"
Note: This package automatically includes the compatible
civic-transparency-specversion as a dependency.
Quick Start
Basic Usage
from ci.transparency.types import Series, ProvenanceTag
# Create a time series for civic data
series = Series(
topic="#LocalElection",
generated_at="2025-01-15T12:00:00Z",
interval="minute",
points=[] # Add your aggregated data points here
)
# Validate and serialize
data = series.model_dump() # JSON-compatible dict
json_str = series.model_dump_json(indent=2) # Pretty JSON string
Loading and Validation
from ci.transparency.types import Series
# Load from existing data with validation
series = Series.model_validate(data_dict)
series = Series.model_validate_json(json_string)
# Handle validation errors
from pydantic import ValidationError
try:
invalid_series = Series.model_validate(bad_data)
except ValidationError as e:
print(f"Validation failed: {e}")
Working with Provenance Tags
from ci.transparency.types import ProvenanceTag
tag = ProvenanceTag(
acct_age_bucket="1-6m",
acct_type="person",
automation_flag="manual",
post_kind="original",
client_family="mobile",
media_provenance="hash_only",
dedup_hash="a1b2c3d4"
)
Available Types
| Type | Description | Schema Source |
|---|---|---|
Series |
Privacy-preserving time series data for civic topics | series.schema.json |
ProvenanceTag |
Per-post metadata tags (bucketed, no PII) | provenance_tag.schema.json |
See the API documentation for complete field definitions and examples.
Validation and Schemas
Pydantic Validation
All models use Pydantic v2 for runtime validation:
- Strict typing: Unknown fields are rejected
- Format validation: ISO 8601 dates, patterns, enums
- Range checking: Min/max values, string lengths
- Nested validation: Complex object hierarchies
JSON Schema Validation (Optional)
For additional validation against the canonical schemas:
import json
from importlib.resources import files
from jsonschema import Draft202012Validator
# Get the official schema
schema_text = files("ci.transparency.spec.schemas").joinpath("series.schema.json").read_text()
schema = json.loads(schema_text)
# Validate your data
validator = Draft202012Validator(schema)
validator.validate(series.model_dump())
Versioning Strategy
This package follows the underlying specification versions:
- Major versions: Breaking schema changes
- Minor versions: Backward-compatible additions
- Patch versions: Documentation, tooling, bug fixes
Best Practice: Pin to compatible major versions:
pip install "civic-transparency-types>=0.2.1,<1.0"
The package automatically manages compatibility with the corresponding civic-transparency-spec version.
Integration Examples
FastAPI Integration
from fastapi import FastAPI
from ci.transparency.types import Series
app = FastAPI()
@app.post("/civic-data")
async def receive_series(series: Series) -> dict:
# Automatic validation and parsing
return {"received": series.topic, "points": len(series.points)}
File I/O
from pathlib import Path
from ci.transparency.types import Series
# Save to file
series_file = Path("data.json")
series_file.write_text(series.model_dump_json(indent=2))
# Load from file
loaded_series = Series.model_validate_json(series_file.read_text())
Development and Contributing
This is a generated types package - the source of truth is the civic-transparency-spec repository.
For Type Users
- Report type-related issues here
- Request documentation improvements
- Share integration examples
For Schema Changes
- Schema modifications should be made in the spec repository
- Types are automatically regenerated when the spec changes
Local Development
git clone https://github.com/civic-interconnect/civic-transparency-types
cd civic-transparency-types
python -m venv .venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
pip install -e ".[dev]"
See CONTRIBUTING.md for detailed guidelines.
Support and Community
- Documentation: https://civic-interconnect.github.io/civic-transparency-types/
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: info@civicinterconnect.org
About Civic Transparency
Civic Transparency is an open standard for privacy-preserving, non-partisan analysis of how information spreads in civic contexts. The specification enables researchers, platforms, and civic organizations to share insights while protecting individual privacy.
Core Principles:
- Privacy by Design: No personally identifiable information
- Aggregation First: Time-bucketed, statistical summaries
- Open Standard: Collaborative, transparent development
- Practical Implementation: Real-world deployment focus
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file civic_transparency_types-0.2.1.tar.gz.
File metadata
- Download URL: civic_transparency_types-0.2.1.tar.gz
- Upload date:
- Size: 55.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cebb77883e9372fa9258951da968973c1b7b54d75a59b8a10f1f840e2fe2925a
|
|
| MD5 |
b1617fc181ae9a8d3b8494e9a757cea6
|
|
| BLAKE2b-256 |
424b74371318b61d4090a9d7a1c1b8d163d5ddb558144d7dc5cb113ae915a5a1
|
Provenance
The following attestation bundles were made for civic_transparency_types-0.2.1.tar.gz:
Publisher:
release.yml on civic-interconnect/civic-transparency-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
civic_transparency_types-0.2.1.tar.gz -
Subject digest:
cebb77883e9372fa9258951da968973c1b7b54d75a59b8a10f1f840e2fe2925a - Sigstore transparency entry: 411909197
- Sigstore integration time:
-
Permalink:
civic-interconnect/civic-transparency-types@9e5059aa74e692f73b46dee5dec0838ad82233a4 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/civic-interconnect
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9e5059aa74e692f73b46dee5dec0838ad82233a4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file civic_transparency_types-0.2.1-py3-none-any.whl.
File metadata
- Download URL: civic_transparency_types-0.2.1-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c1a63afb2d8b7bc88ad151d562f5a32b6154a68bef0d6195a1e4233a2fd1a04
|
|
| MD5 |
314db8e0b2e9b67a60cec9f66bf4fdec
|
|
| BLAKE2b-256 |
fa335034d46774739d36610b0514df6a2658e18537315fb5895078676370ba3b
|
Provenance
The following attestation bundles were made for civic_transparency_types-0.2.1-py3-none-any.whl:
Publisher:
release.yml on civic-interconnect/civic-transparency-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
civic_transparency_types-0.2.1-py3-none-any.whl -
Subject digest:
5c1a63afb2d8b7bc88ad151d562f5a32b6154a68bef0d6195a1e4233a2fd1a04 - Sigstore transparency entry: 411909205
- Sigstore integration time:
-
Permalink:
civic-interconnect/civic-transparency-types@9e5059aa74e692f73b46dee5dec0838ad82233a4 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/civic-interconnect
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9e5059aa74e692f73b46dee5dec0838ad82233a4 -
Trigger Event:
push
-
Statement type: