FHIR Resources as Model Classes - Python classes for all FHIR resources with validation and serialization
npx @tessl/cli install tessl/pypi-fhir--resources@7.1.0A comprehensive Python library providing classes for all FHIR (Fast Healthcare Interoperability Resources) resources across multiple specification versions. Built on Pydantic for high-performance data validation and serialization, this library enables developers to create, manipulate, and validate FHIR resources programmatically with full compliance to healthcare interoperability standards.
pip install fhir.resourcesimport fhir.resources
from fhir.resources import get_fhir_model_class, construct_fhir_elementFor specific FHIR resources (R5 - default version):
from fhir.resources.patient import Patient
from fhir.resources.observation import Observation
from fhir.resources.bundle import BundleImport Notes: The main fhir.resources package only exports the core functions get_fhir_model_class and construct_fhir_element. Individual FHIR resources must be imported from their specific modules (e.g., from fhir.resources.patient import Patient) or accessed dynamically using get_fhir_model_class("Patient").
For version-specific resources:
# R4B
from fhir.resources.R4B import Patient as PatientR4B
# STU3
from fhir.resources.STU3 import Patient as PatientSTU3
# DSTU2
from fhir.resources.DSTU2 import Patient as PatientDSTU2from fhir.resources.patient import Patient
from fhir.resources.humanname import HumanName
from fhir.resources.identifier import Identifier
from fhir.resources import get_fhir_model_class, construct_fhir_element
# Create a patient with demographic information
patient = Patient(
id="patient-123",
active=True,
name=[
HumanName(
use="official",
family="Doe",
given=["John", "F"]
)
],
identifier=[
Identifier(
use="usual",
system="urn:oid:1.2.36.146.595.217.0.1",
value="12345"
)
]
)
# Serialize to JSON
patient_json = patient.json()
print(patient_json)
# Parse from JSON string
patient_data = '{"resourceType": "Patient", "id": "123", "active": true}'
patient_from_json = Patient.parse_raw(patient_data)
# Dynamic resource creation
PatientClass = get_fhir_model_class("Patient")
patient_dynamic = PatientClass.parse_obj({"resourceType": "Patient", "active": True})
# Construct from various data formats
patient_from_dict = construct_fhir_element("Patient", {"active": True})
patient_from_file = construct_fhir_element("Patient", "patient.json")FHIR resources follow a hierarchical structure based on the FHIR specification:
The library supports multiple FHIR versions (R5, R4B, STU3, DSTU2) with version-specific modules, each containing the complete set of resources and data types defined in that FHIR specification version.
Essential utilities for dynamic FHIR resource creation and manipulation, enabling flexible resource handling without hardcoded resource types.
def get_fhir_model_class(model_name: str) -> Type[FHIRAbstractModel]:
"""Get FHIR model class by resource/element name."""
def construct_fhir_element(
element_type: str,
data: Union[Dict[str, Any], str, bytes, Path]
) -> FHIRAbstractModel:
"""Construct FHIR element from various data formats."""Comprehensive set of FHIR primitive and complex data types including strings, dates, quantities, addresses, and coded concepts with full validation.
# Primitive types
class Boolean: ...
class String: ...
class DateTime: ...
class Decimal: ...
class Integer: ...
# Complex types
class HumanName: ...
class Address: ...
class CodeableConcept: ...
class Identifier: ...
class Quantity: ...Comprehensive patient demographics and identity management including names, addresses, contacts, communication preferences, and relationships.
class Patient(DomainResource):
def __init__(
self,
active: Optional[bool] = None,
name: Optional[List[HumanName]] = None,
identifier: Optional[List[Identifier]] = None,
gender: Optional[str] = None,
birthDate: Optional[str] = None,
address: Optional[List[Address]] = None,
**kwargs
): ...Clinical data including observations, conditions, procedures, medications, and diagnostic reports for comprehensive healthcare documentation.
class Observation(DomainResource): ...
class Condition(DomainResource): ...
class Procedure(DomainResource): ...
class MedicationRequest(DomainResource): ...
class DiagnosticReport(DomainResource): ...Healthcare administration including appointments, encounters, organizations, practitioners, and schedules for operational management.
class Appointment(DomainResource): ...
class Encounter(DomainResource): ...
class Organization(DomainResource): ...
class Practitioner(DomainResource): ...
class Schedule(DomainResource): ...Healthcare financial transactions including claims, coverage, payments, and billing for insurance and payment processing.
class Claim(DomainResource): ...
class Coverage(DomainResource): ...
class ExplanationOfBenefit(DomainResource): ...
class PaymentReconciliation(DomainResource): ...Multi-format serialization supporting JSON, XML, and YAML with flexible parsing from strings, bytes, or files.
# Instance methods available on all FHIR resources
def json(self, **kwargs) -> str: ...
def xml(self, **kwargs) -> str: ...
def yaml(self, **kwargs) -> str: ...
def dict(self, **kwargs) -> Dict[str, Any]: ...
# Class methods for parsing
@classmethod
def parse_raw(cls, data: Union[str, bytes], content_type: str = None): ...
@classmethod
def parse_file(cls, path: Union[str, Path]): ...
@classmethod
def parse_obj(cls, data: Dict[str, Any]): ...Multi-version FHIR support with dedicated modules for R5, R4B, STU3, and DSTU2, each containing version-appropriate resources and validation.
# Version-specific imports
from fhir.resources.R4B import Patient as PatientR4B
from fhir.resources.STU3 import Patient as PatientSTU3
from fhir.resources.DSTU2 import Patient as PatientDSTU2