Registry / serialization / cloudevents

cloudevents

JSON →
library2.2.0pypypi✓ verified 25d ago

The CloudEvents Python SDK provides a simple and efficient way to create, parse, and send CloudEvents in Python applications. It supports all CloudEvents versions and various transport bindings. The current version is 2.0.0, with active development and regular releases.

pip install cloudevents
INSTALL
IMPORT
SIG · CLOUDEVENTS
C
cloudevents
serializationpythonv2.2.0
Install
1.8s avg
Import
50ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 20.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.010s · 21MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CloudEvent
from cloudevents.http import CloudEvent
from cloudevents.http import CloudEvent

This example demonstrates how to create a CloudEvent with specified attributes and JSON data, then serialize it into HTTP structured mode headers and body. It also shows how to access the event's data (which is bytes in v2.0.0+).

from cloudevents.http import CloudEvent, to_structured import json import datetime # Define CloudEvent attributes attributes = { "type": "com.example.someevent", "source": "/mycontext/myapp", "id": "A234-1234-1234", "time": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z'), "datacontenttype": "application/json" } # Define event data data = {"message": "Hello, CloudEvents!", "value": 123} # Create a CloudEvent instance event = CloudEvent(attributes, data) # Serialize the event to structured HTTP headers and body headers, body = to_structured(event) print("--- CloudEvent (Structured Mode) ---") print("Headers:", headers) print("Body:", body.decode('utf-8')) print("\nEvent Data (original):") print(event.data) # For v2.0.0, this returns bytes or None print("Event Data (decoded):", json.loads(event.data.decode('utf-8')))
Debug
Known issues
breakingThe CloudEvent attribute `contenttype` was renamed to `datacontenttype` in version 2.0.0 to align with the CloudEvents specification. Code using `event.contenttype` will break.
fix
Update all instances of `contenttype` to `datacontenttype` when creating or accessing CloudEvent attributes (e.g., `event.attributes['datacontenttype']`).
affects: 2.0.0+
breakingIn version 2.0.0+, `CloudEvent.data` now consistently returns `bytes` or `None`. In earlier versions (1.x), it could return the original Python object type (e.g., `dict`, `str`).
fix
If you expect JSON or text data, you must decode `event.data`. For example, `json.loads(event.data.decode('utf-8'))` for JSON, or `event.data.decode('utf-8')` for plain text.
affects: 2.0.0+
breakingSeveral utility functions and methods, such as `CloudEvent.FromInstance`, `CloudEvent.ToJsonString`, and direct `json.dumps` methods on CloudEvent objects, were removed in version 2.0.0.
fix
Use the provided binding functions like `from_structured`, `to_structured` for HTTP operations, or manually serialize/deserialize using `event.attributes` and `event.data`.
affects: 2.0.0+
gotchaThe `time` attribute of a CloudEvent must conform to RFC 3339 (e.g., `2018-04-05T17:31:00Z`). Incorrectly formatted timestamps might be accepted by the SDK but could cause issues with downstream consumers or spec validation.
fix
Ensure `time` is a string formatted as `YYYY-MM-DDTHH:MM:SSZ` or with milliseconds (`YYYY-MM-DDTHH:MM:SS.ffffffZ`). Use `datetime.datetime.now(datetime.timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z')` for reliable generation.
affects: All versions
breakingThe 'cloudevents' library could not be imported. This typically indicates that the library is not installed in the current Python environment.
fix
Install the library using `pip install cloudevents`.
affects: All versions
Upgrade
Version history
2.2.0latest on PyPI · released Jun 11, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
39 hits · last 30 days
node
32
OpenAI (training)
1
Resources
cloudevents — pip install cloudevents · libregistry