A Python library to convert C-CDA documents to FHIR R4B resources
Project description
ccda-to-fhir
A Python library for converting C-CDA documents to FHIR R4B resources. Built for precision: the library implements the HL7 C-CDA on FHIR Implementation Guide and fails loudly on unexpected data rather than silently dropping information.
Installation
pip install ccda-to-fhir
Or with uv:
uv add ccda-to-fhir
Quick Start
from ccda_to_fhir import convert_document
with open("patient_record.xml") as f:
result = convert_document(f.read())
bundle = result["bundle"]
print(f"Converted {len(bundle['entry'])} resources")
Features
- Type Safety: Strict typing with Pydantic models and mypy validation
- Extensibility: Modular converter architecture for custom mappings
- Standards Compliance: FHIR R4B with US Core profile support, HL7 C-CDA on FHIR IG v2.0.0
Supported Conversions
| C-CDA Section | FHIR Resource |
|---|---|
| Patient (recordTarget) | Patient |
| Problems | Condition |
| Allergies | AllergyIntolerance |
| Medications | MedicationRequest, MedicationStatement, MedicationDispense |
| Immunizations | Immunization |
| Procedures | Procedure |
| Lab Results | DiagnosticReport, Observation |
| Vital Signs | Observation (vital-signs profile) |
| Social History | Observation |
| Encounters | Encounter |
| Care Plans | CarePlan, Goal |
| Care Teams | CareTeam |
| Payers | Coverage, Organization |
| Plan of Treatment | ServiceRequest |
| Notes (Note Activities, narrative-only sections, encounter diagnosis notes) | DocumentReference |
| Devices | Device |
| Authors/Performers | Practitioner, PractitionerRole, Device, Organization, Provenance |
Clinical Data Handling
- Complex structures: blood pressure components, pregnancy observations
- Provenance tracking with multi-author support (Practitioner, Device, Organization)
- Narrative-only section extraction (HPI, Physical Exam, ROS, Assessment, Reason for Visit)
- Encounter diagnosis notes from narrative tables
- Negation patterns: no-known-allergies, refuted conditions, not-done procedures
- Body site qualifiers with laterality
- Reference ranges for vital signs and lab results
- NullFlavor to data-absent-reason mapping
- US Core extensions (race, ethnicity, birth sex, tribal affiliation)
Tested EHR Integrations
The library has been validated against C-CDA documents from:
- Epic
- Cerner
- Athena
- Practice Fusion
- NIST reference documents
Error Handling
The library raises specific exceptions when it encounters data it cannot handle:
from ccda_to_fhir import convert_document
from ccda_to_fhir.exceptions import UnknownCodeSystemError, UnmappedValueError
try:
result = convert_document(xml_content)
except UnknownCodeSystemError as e:
print(f"Unknown code system OID: {e.oid}")
except UnmappedValueError as e:
print(f"Unmapped value '{e.value}' for {e.field}")
Conversion Metadata
Track what was processed during conversion:
result = convert_document(xml_content)
metadata = result["metadata"]
print(f"Processed: {len(metadata['processed_templates'])} templates")
print(f"Skipped: {len(metadata['skipped_templates'])} templates")
print(f"Errors: {len(metadata['errors'])}")
Development
git clone https://github.com/NurraHealth/ccda-to-fhir.git
cd ccda-to-fhir
uv sync --dev
uv run pytest
uv run ruff check .
uv run mypy ccda_to_fhir/
Releasing
- Bump
versioninpyproject.toml - Open a PR, get it approved, and merge to main
- Create a GitHub Release
- The publish workflow automatically builds and uploads to PyPI
Documentation
See docs/mapping/ for detailed field-level mapping documentation covering each resource type, including:
- Mapping Overview - Core data type mappings and conversion rules
- Terminology Maps - Value set translations between C-CDA and FHIR
License
MIT
Project details
Release history Release notifications | RSS feed
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 ccda_to_fhir-0.2.22.tar.gz.
File metadata
- Download URL: ccda_to_fhir-0.2.22.tar.gz
- Upload date:
- Size: 1.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af2daf6b29c67ef65598833ab0bf84da74e9f21ac0e53cbf0e64c1c355f1f85d
|
|
| MD5 |
c2a9fa5365983b5122888698c4610ac2
|
|
| BLAKE2b-256 |
4b04380eced606dc92be7ff53ef1d88d08fe681f997e90405bfe31a643bebcc9
|
Provenance
The following attestation bundles were made for ccda_to_fhir-0.2.22.tar.gz:
Publisher:
publish.yml on NurraHealth/ccda-to-fhir
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ccda_to_fhir-0.2.22.tar.gz -
Subject digest:
af2daf6b29c67ef65598833ab0bf84da74e9f21ac0e53cbf0e64c1c355f1f85d - Sigstore transparency entry: 1143283868
- Sigstore integration time:
-
Permalink:
NurraHealth/ccda-to-fhir@8f009dcb5223a5c9c9f7fb176f4b12c2583089a5 -
Branch / Tag:
refs/tags/v0.2.22 - Owner: https://github.com/NurraHealth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f009dcb5223a5c9c9f7fb176f4b12c2583089a5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ccda_to_fhir-0.2.22-py3-none-any.whl.
File metadata
- Download URL: ccda_to_fhir-0.2.22-py3-none-any.whl
- Upload date:
- Size: 338.3 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 |
d44e3999a7a9b1497859de8ab84ebcafee7f2a8bc1da0fe62010fe2241ecc755
|
|
| MD5 |
ab70df21ed2e722bd0ddf6d37bbe1c64
|
|
| BLAKE2b-256 |
3e03efc198ad20acaf2a0263f599beb055a3c57bb82eb84147c06b17a19147f1
|
Provenance
The following attestation bundles were made for ccda_to_fhir-0.2.22-py3-none-any.whl:
Publisher:
publish.yml on NurraHealth/ccda-to-fhir
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ccda_to_fhir-0.2.22-py3-none-any.whl -
Subject digest:
d44e3999a7a9b1497859de8ab84ebcafee7f2a8bc1da0fe62010fe2241ecc755 - Sigstore transparency entry: 1143283922
- Sigstore integration time:
-
Permalink:
NurraHealth/ccda-to-fhir@8f009dcb5223a5c9c9f7fb176f4b12c2583089a5 -
Branch / Tag:
refs/tags/v0.2.22 - Owner: https://github.com/NurraHealth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8f009dcb5223a5c9c9f7fb176f4b12c2583089a5 -
Trigger Event:
release
-
Statement type: