Skip to main content

An implementation of the CloudEvents spec using Pydantic V2

Project description

cloudevents-pydantic

Static Badge Stable Version stability-beta

Python tests Maintainability Code Coverage

Checked with mypy Ruff Code style: black security: bandit Pydantic v2

This is an implementation of the CloudEvents spec using Pydantic V2 for high performance during validation and serialization.

It is meant to support natively FastAPI and FastStream (WIP)

How to use

pip install cloudevents-pydantic
from cloudevents_pydantic.bindings.http import HTTPHandler
from cloudevents_pydantic.events import CloudEvent

handler = HTTPHandler()
minimal_attributes = {
    "type": "com.example.string",
    "source": "https://example.com/event-producer",
}

# `CloudEvent` is a Pydantic model to handle validation and serialization
# `event_factory` is a helper method to autogenerate some of the mandatory 
# such as id, time, specversion
event = CloudEvent.event_factory(**minimal_attributes)

# Single event HTTP serialization
headers, single_event_as_json = handler.to_json(event)

# Batch of events HTTP serialization
headers, batch_of_events_as_json = handler.to_json_batch([event])

# Parsing a JSON string for a single event
parsed_event = handler.from_json(single_event_as_json)

# Parsing a JSON string for a single event
parsed_event_list = handler.from_json(batch_of_events_as_json)

Refer to the docs for more advanced use cases and for details on how to create custom events.

Performance

Using pydantic gives a great performance boost if compared to the official SDK. (there's obviously some performance issue in the official serialization using pydantic)

These results come from a Macbook Pro M4 Pro on python 3.13. Feel free to run the benchmark.py script yourself.

==== 1M iterations benchmark ====
Timings for HTTP JSON deserialization:
This package: 2.3955996250006137
Official SDK using pydantic model: 11.389213957998436
Official SDK using http model: 10.174893917006557

Timings for HTTP JSON serialization:
This package: 3.497491959002218
Official SDK using pydantic model: 31.92037604199868
Official SDK using http model: 6.780242209002608

Supported specification features

Core Specification v1.0
CloudEvents Core

Event Formats v1.0
AVRO Event Format
JSON Event Format

Protocol Bindings v1.0
HTTP Protocol Binding
Kafka Protocol Binding

Content Modes v1.0
HTTP Binary
HTTP Structured
HTTP Batch
Kafka Binary
Kafka Structured
Kafka Batch

Commands for development

All the common commands used during development can be run using make targets:

  • make dev-dependencies: Install dev requirements
  • make fix: Run code style and lint automatic fixes (where possible)
  • make test: Run test suite against system python version
  • make check: Run tests against all available python versions, code style and lint checks
  • make type, make format, make lint, make bandit: Run the relevant check
  • make docs: Render the mkdocs website locally

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudevents_pydantic-0.2.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudevents_pydantic-0.2.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file cloudevents_pydantic-0.2.0.tar.gz.

File metadata

  • Download URL: cloudevents_pydantic-0.2.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cloudevents_pydantic-0.2.0.tar.gz
Algorithm Hash digest
SHA256 472a0973e6605057de1b9cc167fe4781cc0670d4169b26094aa6a6363b93373a
MD5 e3ec61ee094d0f38d4ffcd264257aa9f
BLAKE2b-256 1dbaf0fe53e7042c07683ca1c958ad3bea1e574b351c195713b4c86d211f0dd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloudevents_pydantic-0.2.0.tar.gz:

Publisher: release.yml on febus982/cloudevents-pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cloudevents_pydantic-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cloudevents_pydantic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43a87f15d156767976d29afd2efe2bf7240d19d8a47d3394b543ab61cd04b822
MD5 7e0fa3d4f62228f71315cd5f9aebf611
BLAKE2b-256 1821e23fa589874794e44ffd510493b0e67295521749de5cd912aec5f34d6efc

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloudevents_pydantic-0.2.0-py3-none-any.whl:

Publisher: release.yml on febus982/cloudevents-pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page