Skip to main content

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

  1. Bump version in pyproject.toml
  2. Open a PR, get it approved, and merge to main
  3. Create a GitHub Release
  4. The publish workflow automatically builds and uploads to PyPI

Documentation

See docs/mapping/ for detailed field-level mapping documentation covering each resource type, including:

License

MIT

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

ccda_to_fhir-0.2.22.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ccda_to_fhir-0.2.22-py3-none-any.whl (338.3 kB view details)

Uploaded Python 3

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

Hashes for ccda_to_fhir-0.2.22.tar.gz
Algorithm Hash digest
SHA256 af2daf6b29c67ef65598833ab0bf84da74e9f21ac0e53cbf0e64c1c355f1f85d
MD5 c2a9fa5365983b5122888698c4610ac2
BLAKE2b-256 4b04380eced606dc92be7ff53ef1d88d08fe681f997e90405bfe31a643bebcc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for ccda_to_fhir-0.2.22.tar.gz:

Publisher: publish.yml on NurraHealth/ccda-to-fhir

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for ccda_to_fhir-0.2.22-py3-none-any.whl
Algorithm Hash digest
SHA256 d44e3999a7a9b1497859de8ab84ebcafee7f2a8bc1da0fe62010fe2241ecc755
MD5 ab70df21ed2e722bd0ddf6d37bbe1c64
BLAKE2b-256 3e03efc198ad20acaf2a0263f599beb055a3c57bb82eb84147c06b17a19147f1

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page