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
Metadata
Release files for civic-transparency-ptag-types 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| civic_transparency_ptag_types-0.2.5.tar.gz | 82.8 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| civic_transparency_ptag_types-0.2.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 95.6 kB
Release files / civic_transparency_ptag_types-0.2.5.tar.gz
| Download URL | civic_transparency_ptag_types-0.2.5.tar.gz |
|---|---|
| Size | 82.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d6819181b43961a07e557b8352da6ba432166f6ef7bfffdfa1f4b9f927f23b4b
|
|
BLAKE2b-256 checksum How to use checksums |
8d5850a587ab425afe3e0ce2c3da0b95f08480771b1d9420d049c8565ea9bd0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 2, 2025.
Transparency logRelease files / civic_transparency_ptag_types-0.2.5-py3-none-any.whl
| Download URL | civic_transparency_ptag_types-0.2.5-py3-none-any.whl |
|---|---|
| Size | 12.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
664dd3cb952c55f71596ec3cf165b48227bf8d9b1187d12c930128fc7274f03e
|
|
BLAKE2b-256 checksum How to use checksums |
f8f9f94f50ca875da10f22d3706e5ac34759f2a57e9a43dfcf328c24b519aefb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 2, 2025.
Transparency logSecurity for civic-transparency-ptag-types 0.2.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for civic_transparency_ptag_types-0.2.5.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 2, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.