Typed Python models (Pydantic v2) for the Civic Transparency PTag specification
Project description
Civic Transparency – Types
Typed Python models (Pydantic v2) for the Civic Transparency PTag schema.
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
Available Types
| Type | Description | Schema Source |
|---|---|---|
PTagSeries |
Privacy-preserving time series data for civic topics | series.schema.json |
PTag |
Per-post metadata tags (bucketed, no PII) | ptag.schema.json |
See the API documentation for complete field definitions and examples.
Installation
pip install civic-transparency-py-ptag-types
Note: This package automatically includes the compatible
civic-transparency-ptag-specversion as a dependency.
Quick Start
Basic Usage
from ci.transparency.ptag.types import PTagSeries, PTag
# Create a time series for civic data
series = PTagSeries(
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.ptag.types import PTagSeries
# Load from existing data with validation
series = PTagSeries.model_validate(data_dict)
series = PTagSeries.model_validate_json(json_string)
# Handle validation errors
from pydantic import ValidationError
try:
invalid_series = PTagSeries.model_validate(bad_data)
except ValidationError as e:
print(f"Validation failed: {e}")
Working with PTags (Provenance Tags)
from ci.transparency.ptag.types import PTag
tag = PTag(
acct_age_bucket="1-6m",
acct_type="person",
automation_flag="manual",
post_kind="original",
client_family="mobile",
media_provenance="hash_only",
dedup_hash="a1b2c3d4"
)
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.ptag.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
The package automatically manages compatibility with the corresponding civic-transparency-ptag-spec version.
Integration Examples
FastAPI Integration
from fastapi import FastAPI
from ci.transparency.ptag.types import PTagSeries
app = FastAPI()
@app.post("/civic-data")
async def receive_series(series: PTagSeries) -> dict:
# Automatic validation and parsing
return {"received": series.topic, "points": len(series.points)}
File I/O
from pathlib import Path
from ci.transparency.ptag.types import PTagSeries
# Save to file
series_file = Path("data.json")
series_file.write_text(series.model_dump_json(indent=2))
# Load from file
loaded_series = PTagSeries.model_validate_json(series_file.read_text())
Performance Benchmarks
See the latest benchmark results in performance_results.md.
Development and Contributing
This is a generated types package - the source of truth is the civic-transparency-ptag-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
See CONTRIBUTING.md for detailed guidelines.
Versioning
This specification follows semantic versioning. See CHANGELOG.md for version history.
License
MIT © Civic Interconnect
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_ptag_types-0.2.5.tar.gz.
File metadata
- Download URL: civic_transparency_ptag_types-0.2.5.tar.gz
- Upload date:
- Size: 82.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6819181b43961a07e557b8352da6ba432166f6ef7bfffdfa1f4b9f927f23b4b
|
|
| MD5 |
93b3685adbf47e1107a2b0777e998493
|
|
| BLAKE2b-256 |
8d5850a587ab425afe3e0ce2c3da0b95f08480771b1d9420d049c8565ea9bd0e
|
Provenance
The following attestation bundles were made for civic_transparency_ptag_types-0.2.5.tar.gz:
Publisher:
release.yml on civic-interconnect/civic-transparency-py-ptag-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
civic_transparency_ptag_types-0.2.5.tar.gz -
Subject digest:
d6819181b43961a07e557b8352da6ba432166f6ef7bfffdfa1f4b9f927f23b4b - Sigstore transparency entry: 581564482
- Sigstore integration time:
-
Permalink:
civic-interconnect/civic-transparency-py-ptag-types@368a1dedd054d9f33378cf63a12b5586a42b7fc4 -
Branch / Tag:
refs/tags/v0.2.5 - Owner: https://github.com/civic-interconnect
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@368a1dedd054d9f33378cf63a12b5586a42b7fc4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file civic_transparency_ptag_types-0.2.5-py3-none-any.whl.
File metadata
- Download URL: civic_transparency_ptag_types-0.2.5-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
664dd3cb952c55f71596ec3cf165b48227bf8d9b1187d12c930128fc7274f03e
|
|
| MD5 |
fa448cd267ff0577ae05044abe2d761f
|
|
| BLAKE2b-256 |
f8f9f94f50ca875da10f22d3706e5ac34759f2a57e9a43dfcf328c24b519aefb
|
Provenance
The following attestation bundles were made for civic_transparency_ptag_types-0.2.5-py3-none-any.whl:
Publisher:
release.yml on civic-interconnect/civic-transparency-py-ptag-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
civic_transparency_ptag_types-0.2.5-py3-none-any.whl -
Subject digest:
664dd3cb952c55f71596ec3cf165b48227bf8d9b1187d12c930128fc7274f03e - Sigstore transparency entry: 581564488
- Sigstore integration time:
-
Permalink:
civic-interconnect/civic-transparency-py-ptag-types@368a1dedd054d9f33378cf63a12b5586a42b7fc4 -
Branch / Tag:
refs/tags/v0.2.5 - Owner: https://github.com/civic-interconnect
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@368a1dedd054d9f33378cf63a12b5586a42b7fc4 -
Trigger Event:
push
-
Statement type: