An implementation of the CloudEvents spec using Pydantic V2
Project description
cloudevents-pydantic
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 requirementsmake fix: Run code style and lint automatic fixes (where possible)make test: Run test suite against system python versionmake check: Run tests against all available python versions, code style and lint checksmake type,make format,make lint,make bandit: Run the relevant checkmake 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
472a0973e6605057de1b9cc167fe4781cc0670d4169b26094aa6a6363b93373a
|
|
| MD5 |
e3ec61ee094d0f38d4ffcd264257aa9f
|
|
| BLAKE2b-256 |
1dbaf0fe53e7042c07683ca1c958ad3bea1e574b351c195713b4c86d211f0dd5
|
Provenance
The following attestation bundles were made for cloudevents_pydantic-0.2.0.tar.gz:
Publisher:
release.yml on febus982/cloudevents-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cloudevents_pydantic-0.2.0.tar.gz -
Subject digest:
472a0973e6605057de1b9cc167fe4781cc0670d4169b26094aa6a6363b93373a - Sigstore transparency entry: 601202779
- Sigstore integration time:
-
Permalink:
febus982/cloudevents-pydantic@b105647befe11dda3644166920265b62eb0bf15f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/febus982
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b105647befe11dda3644166920265b62eb0bf15f -
Trigger Event:
release
-
Statement type:
File details
Details for the file cloudevents_pydantic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cloudevents_pydantic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 23.1 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 |
43a87f15d156767976d29afd2efe2bf7240d19d8a47d3394b543ab61cd04b822
|
|
| MD5 |
7e0fa3d4f62228f71315cd5f9aebf611
|
|
| BLAKE2b-256 |
1821e23fa589874794e44ffd510493b0e67295521749de5cd912aec5f34d6efc
|
Provenance
The following attestation bundles were made for cloudevents_pydantic-0.2.0-py3-none-any.whl:
Publisher:
release.yml on febus982/cloudevents-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cloudevents_pydantic-0.2.0-py3-none-any.whl -
Subject digest:
43a87f15d156767976d29afd2efe2bf7240d19d8a47d3394b543ab61cd04b822 - Sigstore transparency entry: 601202780
- Sigstore integration time:
-
Permalink:
febus982/cloudevents-pydantic@b105647befe11dda3644166920265b62eb0bf15f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/febus982
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b105647befe11dda3644166920265b62eb0bf15f -
Trigger Event:
release
-
Statement type: