Registry / serialization / cyclonedx-python-lib

cyclonedx-python-lib

JSON →
library11.12.0pypypi✓ verified 30d ago

The CycloneDX Python Library provides data models, validators, and serialization/deserialization capabilities for creating, rendering, and reading CycloneDX Software Bill of Materials (SBOM) documents. It is an OWASP Flagship Project and is intended as a programmatic library, not a standalone SBOM generation tool. The library maintains a frequent release cadence, often releasing new minor versions multiple times a quarter.

pip install cyclonedx-python-lib
INSTALL
IMPORT
SIG · CYCLONEDX-PYTHON-L
C
cyclonedx-python-lib
serializationpythonv11.12.0
Install
3.3s avg
Import
256ms
Disk
45MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v11.12.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.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.273s · 46.5MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 3.3s · import 0.239s · 47MB
45MB installed
● package 45MB
Code
Verified usage

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

Bom
✓ from cyclonedx.model.bom import Bom
Component
✓ from cyclonedx.model.component import Component
Purl
✓ from packageurl.contrib.url2purl import url2purl
✗ from cyclonedx.model.component import Purl
The Purl class is part of `packageurl.contrib.url2purl` and not directly under `cyclonedx.model.component`.
JsonV15
✓ from cyclonedx.output import JsonV15
✗ from cyclonedx.output import Json
Specific CycloneDX schema versions (e.g., JsonV15, XmlV14) should be imported for serialization, not a generic 'Json' or 'Xml' class.

This quickstart demonstrates how to programmatically create a simple CycloneDX SBOM with two components and a dependency relationship, then serialize it to JSON using Schema Version 1.5. It also includes commented-out code for deserialization as an example.

from cyclonedx.model.bom import Bom from cyclonedx.model.component import Component from cyclonedx.model.dependency import Dependency from packageurl.contrib.url2purl import url2purl from cyclonedx.output import JsonV15 # 1. Create a new BOM bom = Bom() # 2. Define components component_a = Component(name='my-app', version='1.0.0') component_a.bom_ref.value = 'pkg-a-1.0.0' component_b_purl = url2purl('pkg:pypi/requests@2.28.1') component_b = Component(name='requests', version='2.28.1', purl=component_b_purl) component_b.bom_ref.value = 'pkg-b-2.28.1' # 3. Add components to the BOM bom.add_component(component_a) bom.add_component(component_b) # 4. Add a dependency relationship (optional) dep_a_to_b = Dependency(ref=component_a.bom_ref) dep_a_to_b.add_dependency(component_b.bom_ref) bom.add_dependency(dep_a_to_b) # 5. Serialize the BOM to JSON (using CycloneDX Schema Version 1.5) outputter = JsonV15(bom) json_output = outputter.output_as_string(indent=2) print(json_output) # Example of deserialization (requires `validation` extra) # from cyclonedx.validation.schema import SchemaVersion # from cyclonedx.parsers.json.parser import JsonParser # parsed_bom = JsonParser(json_output).parse(SchemaVersion.V1_5) # print(f"Parsed BOM version: {parsed_bom.get_spec_version().to_string()}")
Debug
Known issues
breakingDeserialization behavior changed to ignore unknown properties by default. Previously, unknown properties might have caused errors during deserialization.
fix
If strict deserialization is required, users may need to explicitly configure parsers or validate inputs against a schema prior to deserialization. Review code that relies on implicit erroring for unknown properties.
affects: >=11.0.0
deprecatedCertain exports were deprecated, and non-standard implementations were moved to a `contrib` sub-package. This includes methods like `Bom.get_component_by_purl()` which were previously available directly on the `Bom` object.
fix
Update import paths for deprecated exports to their new locations, typically within the `cyclonedx.contrib` sub-package. Refer to the official documentation and changelog for specific refactor details. For deprecated `Bom` methods, reimplement logic or find alternative API calls.
affects: >=11.6.0
gotchaThis package is a software library for data models and manipulation, not a standalone command-line tool for generating SBOMs from projects. For CLI tools, refer to `cyclonedx-python` or `Jake`.
fix
If seeking a CLI tool to generate SBOMs from project environments (e.g., `requirements.txt`, `Poetry` projects), use `cyclonedx-python` or `Jake`. Use `cyclonedx-python-lib` for programmatic SBOM creation, modification, or validation within your own applications.
affects: All versions
breakingSupport for Python versions older than 3.8 was dropped. Also, significant changes were made to license models and validation behavior (e.g., `Bom.validate()` can now throw `LicenseExpressionAlongWithOthersException`). The `SchemaVersion` and `OutputVersion` enums are no longer string-like.
fix
Ensure your project uses Python 3.8 or newer. Review and update code handling license expressions and schema/output version enums. Update any custom validation logic that relied on previous behavior.
affects: >=5.0.0
gotchaSerialization of unsupported enum values (e.g., component types, external reference types not defined in the target schema version) might result in downgrading, migration, or omission of those values in the output, potentially causing data loss.
fix
Always validate your BOM against the target schema version before serialization to identify and address any potential data loss. Ensure all values used conform to the chosen CycloneDX schema version specification.
affects: All versions
Errors
Common errors & fixes
AttributeError: type object 'datetime.datetime' has no attribute 'fromisoformat'
This error occurs when running `cyclonedx-python-lib` on Python versions older than 3.7, as the `datetime.fromisoformat()` method was introduced in Python 3.7.
fix
Upgrade your Python environment to version 3.7 or newer (the library officially supports Python >=3.9). Alternatively, for older Python 3 versions, you can use the `backports-datetime-fromisoformat` library.
ValueError: Unexpected key <KEY_NAME> in data being serialized to cyclonedx.model.bom.BomMetaData
This error typically arises during serialization (e.g., to JSON or XML) when the data model you've constructed contains an attribute or field that is not expected or recognized by the `cyclonedx-python-lib`'s internal data model for the specific CycloneDX schema version being used.
fix
Review the CycloneDX specification for the target schema version and ensure that all fields in your Python data model (`cyclonedx.model.*` objects) correspond correctly to the specification. Remove or rename unexpected keys, or ensure they are nested correctly within the expected parent elements.
cyclonedx.exception.ValidationError: Validation failed: <detailed error message>
This exception is raised when attempting to validate a CycloneDX Bill of Materials (BOM) object, and the BOM does not conform to the specified CycloneDX schema version or contains invalid data according to the schema rules (e.g., incorrect data types, missing required fields, or malformed structures).
fix
Examine the detailed error message provided in the `ValidationError` to identify the specific part of your BOM that is invalid. Correct the data in your `cyclonedx.model.bom.Bom` object to ensure it strictly adheres to the CycloneDX specification for your chosen schema version. For example, ensuring `metadata.authors` are lists of `OrganizationalContact` objects rather than simple strings.
Upgrade
Version history
11.12.0latest on PyPI · released Aug 13, 2026
Audit
Dependencies
pythonrequiredRequired Python version range.
typing_extensionsrequiredRuntime dependency for older Python versions.
referencingrequiredRuntime dependency for schema validation.
Agent activity
15 hits · last 30 days
node
14
Resources
cyclonedx-python-lib — pip install cyclonedx-python-lib · libregistry