Skip to main content

Fhircraft transforms FHIR (Fast Healthcare Interoperability Resources) specifications into type-safe Python models using Pydantic. Build healthcare applications with automatic validation, intelligent code completion, and seamless integration with Python's ecosystem.

Project description


PyPI - Version PyPI - Python Version Pydantic v2 FHIR Releases


Pythonic healthcare interoperability
A comprehensive Python toolkit for working with FHIR healthcare data standards using Pydantic models from core and profiled FHIR specifications, all without external dependencies or complex server infrastructure.

Explore the Documentation »

Report Bug · Request Feature


[!WARNING]
This package is under active development. Major and/or breaking changes are to be expected in future updates.

Key Features

  • Automatic validation of FHIR resources using Pydantic models generated directly from FHIR structure definitions. Catch schema violations and constraint failures without any dedicated servers.

  • Work with FHIR data as standard Python objects. No XML parsing, no external FHIR servers required. Access and modify healthcare data using familiar Python syntax and patterns.

  • Supports FHIR R4, R4B, and R5 out of the box. Load implementation guides and custom profiles directly from the FHIR package registry to work with specialized healthcare data models.

  • Execute FHIRPath expressions directly on Python objects. Query complex nested healthcare data structures using the standard FHIR query language without additional tooling.

  • Implement healthcare data transformations using the official FHIR Mapping Language. Convert between different data formats while maintaining semantic integrity and validation.

(back to top)

Quick Start

Prerequisites

  • Python 3.10 or higher

Installation

Install Fhircraft using your package manager of choice. To download the latest release using the pip manager:

pip install fhircraft

or install the latest development version:

pip install git+https://github.com/luisfabib/fhircraft.git

To verify your installation:

from fhircraft.fhir.resources.datatypes import get_fhir_resource_type

# This should work without errors
Patient = get_fhir_resource_type("Patient")
print("✓ Fhircraft installed successfully!")

(back to top)

Demo

Built-in FHIR Resources

Work with pre-generated Pydantic models for all standard FHIR resources. Each model includes full validation rules from the FHIR specification:

from fhircraft.fhir.resources.datatypes import get_fhir_resource_type

# Get built-in Patient model for FHIR R5
Patient = get_fhir_resource_type("Patient", "R5")

# Create and validate a patient
patient = Patient(
    name=[{"given": ["Alice"], "family": "Johnson"}],
    gender="female",
    birthDate="1985-03-15"
)

print(f"Created patient: {patient.name[0].given[0]} {patient.name[0].family}")

FHIR Package Integration

Extend base FHIR models with implementation guide profiles loaded directly from the official FHIR package registry:

from fhircraft.fhir.resources.factory import factory

# Load US Core Implementation Guide
factory.load_package("hl7.fhir.us.core", "5.0.1")

# Create US Core Patient model with enhanced validation
USCorePatient = factory.construct_resource_model(
    canonical_url="http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"
)

# Use with US Core constraints
patient = USCorePatient(
    identifier=[{"system": "http://example.org/mrn", "value": "12345"}],
    name=[{"family": "Doe", "given": ["John"]}],
    gender="male"
)

FHIRPath Querying

Execute FHIRPath expressions directly on FHIR resource instances to extract, filter, and validate healthcare data:

# Query patient data with FHIRPath
family_names = patient.fhirpath_values("Patient.name.family")
has_phone = patient.fhirpath_exists("Patient.telecom.where(system='phone')")

# Update data using FHIRPath expressions
patient.fhirpath_update_single("Patient.gender", "female")
patient.fhirpath_update_values("Patient.name.given", ["Jane", "Marie"])

print(f"Updated patient: {family_names[0]}, Phone: {has_phone}")

Data Transformation

Convert external data sources into valid FHIR resources using declarative mapping scripts:

from fhircraft.fhir.mapper import FHIRMapper

# Legacy system data
legacy_patient = {
    "firstName": "Bob",
    "lastName": "Smith", 
    "dob": "1975-06-20",
    "sex": "M"
}

# FHIR Mapping script
mapping_script = """
/// url = "http://example.org/legacy-to-fhir"
/// name = "LegacyPatientToFHIR"

uses "http://hl7.org/fhir/StructureDefinition/Patient" as target

group main(source legacy, target patient: Patient) {
    legacy -> patient.name as name then {
        legacy.firstName -> name.given;
        legacy.lastName -> name.family;
    };
    legacy.dob -> patient.birthDate;
    legacy.sex where($this = 'F') -> patient.gender = "female";
    legacy.sex where($this = 'M') -> patient.gender = "male";
}
"""

# Execute transformation
mapper = FHIRMapper()
targets = mapper.execute_mapping(mapping_script, legacy_patient)
fhir_patient = targets[0]

print(fhir_patient.model_dump(exclude={'meta','resourceType'}))
#> {'name': [{'family': 'Smith', 'given': ['Bob']}], 'birthDate': '1975-06-20'}

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. Checkout the Contributing Guide for more details. Thanks to all our contributors!

(back to top)

License

This project is distributed under the MIT License. See LICENSE for more information.

(back to top)

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

fhircraft-0.6.5.tar.gz (15.1 MB view details)

Uploaded Source

Built Distribution

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

fhircraft-0.6.5-py3-none-any.whl (14.1 MB view details)

Uploaded Python 3

File details

Details for the file fhircraft-0.6.5.tar.gz.

File metadata

  • Download URL: fhircraft-0.6.5.tar.gz
  • Upload date:
  • Size: 15.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fhircraft-0.6.5.tar.gz
Algorithm Hash digest
SHA256 f16899a2bdab4c89f6f56a1c4011ac6ff62fadaaa26fdde526a2b82efc377bbc
MD5 01f8c2f3ddfb84fe9282c47012b56aa0
BLAKE2b-256 c9aa02cc82b7658daf5fc73f4c89b1263d51192af0bfdffce019ce168c9acf48

See more details on using hashes here.

File details

Details for the file fhircraft-0.6.5-py3-none-any.whl.

File metadata

  • Download URL: fhircraft-0.6.5-py3-none-any.whl
  • Upload date:
  • Size: 14.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fhircraft-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f36a95753995345872075060eaef6b4036d3fea5e255ac7b0adf67e4476339f8
MD5 92e64c14e4a31c2febd3e651c9ef6862
BLAKE2b-256 1a6e1911963a1ee06c39c0f83552b50c96de3efc0c5e6d1b57fcfc9b9d5957d1

See more details on using hashes here.

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