{"id":1423,"library":"cloudevents","title":"CloudEvents Python SDK","description":"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.","status":"active","version":"2.0.0","language":"en","source_language":"en","source_url":"https://github.com/cloudevents/sdk-python","tags":["cloudevents","eventing","sdk","cloud"],"install":[{"cmd":"pip install cloudevents","lang":"bash","label":"Install core library"}],"dependencies":[],"imports":[{"note":"Pre-2.0 versions used versioned imports like `cloudevents.sdk.event.v1`. V2.0.0 consolidates to `cloudevents.http` for HTTP binding.","wrong":"from cloudevents.sdk.event.v1 import CloudEvent","symbol":"CloudEvent","correct":"from cloudevents.http import CloudEvent"},{"symbol":"to_structured","correct":"from cloudevents.http import to_structured"},{"symbol":"from_structured","correct":"from cloudevents.http import from_structured"}],"quickstart":{"code":"from cloudevents.http import CloudEvent, to_structured\nimport json\nimport datetime\n\n# Define CloudEvent attributes\nattributes = {\n    \"type\": \"com.example.someevent\",\n    \"source\": \"/mycontext/myapp\",\n    \"id\": \"A234-1234-1234\",\n    \"time\": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z'),\n    \"datacontenttype\": \"application/json\"\n}\n\n# Define event data\ndata = {\"message\": \"Hello, CloudEvents!\", \"value\": 123}\n\n# Create a CloudEvent instance\nevent = CloudEvent(attributes, data)\n\n# Serialize the event to structured HTTP headers and body\nheaders, body = to_structured(event)\n\nprint(\"--- CloudEvent (Structured Mode) ---\")\nprint(\"Headers:\", headers)\nprint(\"Body:\", body.decode('utf-8'))\nprint(\"\\nEvent Data (original):\")\nprint(event.data) # For v2.0.0, this returns bytes or None\nprint(\"Event Data (decoded):\", json.loads(event.data.decode('utf-8')))","lang":"python","description":"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+)."},"warnings":[{"fix":"Update all instances of `contenttype` to `datacontenttype` when creating or accessing CloudEvent attributes (e.g., `event.attributes['datacontenttype']`).","message":"The CloudEvent attribute `contenttype` was renamed to `datacontenttype` in version 2.0.0 to align with the CloudEvents specification. Code using `event.contenttype` will break.","severity":"breaking","affected_versions":"2.0.0+"},{"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.","message":"In 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`).","severity":"breaking","affected_versions":"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`.","message":"Several 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.","severity":"breaking","affected_versions":"2.0.0+"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}