CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-fhir--resources

FHIR Resources as Model Classes - Python classes for all FHIR resources with validation and serialization

Pending
Overview
Eval results
Files

version-support.mddocs/

Version Support

Multi-version FHIR support with dedicated modules for R5, R4B, STU3, and DSTU2, each containing version-appropriate resources and validation. This enables applications to work with different FHIR specification versions based on implementation requirements.

Capabilities

FHIR Version Overview

Support for multiple FHIR specification versions with complete resource sets and validation for each version.

# Default version (R5)
from fhir.resources.patient import Patient  # R5 version

# 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

# Version information
__version__ = "7.1.0"  # Package version
__fhir_version__ = "5.0.0"  # Default FHIR version (R5)

R5 Support (Default)

FHIR R5 (5.0.0) - Latest FHIR specification with the most comprehensive resource set and advanced features.

# R5 is the default version
from fhir.resources.patient import Patient
from fhir.resources.observation import Observation
from fhir.resources.bundle import Bundle

# Core R5 features
# Administrative resources
from fhir.resources.account import Account
from fhir.resources.activitydefinition import ActivityDefinition
from fhir.resources.actordefinition import ActorDefinition
from fhir.resources.administrableproductdefinition import AdministrableProductDefinition
from fhir.resources.appointment import Appointment
from fhir.resources.appointmentresponse import AppointmentResponse
from fhir.resources.auditevent import AuditEvent
from fhir.resources.basic import Basic
from fhir.resources.binary import Binary
from fhir.resources.bundle import Bundle
from fhir.resources.capabilitystatement import CapabilityStatement
from fhir.resources.chargeitem import ChargeItem
from fhir.resources.chargeitemdefinition import ChargeItemDefinition
from fhir.resources.citation import Citation

# Clinical resources
from fhir.resources.allergyintolerance import AllergyIntolerance
from fhir.resources.bodystructure import BodyStructure
from fhir.resources.careplan import CarePlan
from fhir.resources.careteam import CareTeam
from fhir.resources.clinicalimpression import ClinicalImpression
from fhir.resources.communication import Communication
from fhir.resources.communicationrequest import CommunicationRequest
from fhir.resources.condition import Condition
from fhir.resources.diagnosticreport import DiagnosticReport
from fhir.resources.encounter import Encounter
from fhir.resources.familymemberhistory import FamilyMemberHistory
from fhir.resources.goal import Goal
from fhir.resources.imagingstudy import ImagingStudy
from fhir.resources.immunization import Immunization

# Medication resources
from fhir.resources.medication import Medication
from fhir.resources.medicationadministration import MedicationAdministration
from fhir.resources.medicationdispense import MedicationDispense
from fhir.resources.medicationknowledge import MedicationKnowledge
from fhir.resources.medicationrequest import MedicationRequest
from fhir.resources.medicationstatement import MedicationStatement

# Financial resources
from fhir.resources.claim import Claim
from fhir.resources.claimresponse import ClaimResponse
from fhir.resources.coverage import Coverage
from fhir.resources.explanationofbenefit import ExplanationOfBenefit

# And 150+ other resources...

# R5 version identifier
from fhir.resources import __fhir_version__
print(__fhir_version__)  # "5.0.0"

R5 Features:

  • 150+ resource types
  • Advanced search parameters
  • Enhanced terminology support
  • Improved extension mechanisms
  • Latest data types and constraints

R4B Support

FHIR R4B (4.3.0) - Intermediate version between R4 and R5 with selected R5 features backported to R4.

# R4B version-specific imports
from fhir.resources.R4B import (
    Patient, Observation, Encounter, Condition,
    MedicationRequest, DiagnosticReport, Bundle
)

# R4B core functions
from fhir.resources.R4B import get_fhir_model_class, construct_fhir_element

# Version information
from fhir.resources.R4B import __fhir_version__
print(__fhir_version__)  # "4.3.0"

Usage Example:

from fhir.resources.R4B import Patient, HumanName, Identifier

# Create R4B patient
patient_r4b = Patient(
    id="patient-r4b-001",
    active=True,
    name=[
        HumanName(
            use="official",
            family="Johnson",
            given=["Michael"]
        )
    ],
    identifier=[
        Identifier(
            use="usual",
            system="http://hospital.example.org/patients",
            value="R4B-123456"
        )
    ]
)

# R4B-specific features
json_output = patient_r4b.json()

STU3 Support

FHIR STU3 (3.0.2) - Standard for Trial Use 3, widely adopted version with mature resource definitions.

# STU3 version-specific imports
from fhir.resources.STU3 import (
    Patient, Observation, Encounter, Condition,
    MedicationRequest, DiagnosticReport, Bundle
)

# STU3 core functions
from fhir.resources.STU3 import get_fhir_model_class, construct_fhir_element

# Version information
from fhir.resources.STU3 import __fhir_version__
print(__fhir_version__)  # "3.0.2"

Usage Example:

from fhir.resources.STU3 import Patient, HumanName

# Create STU3 patient
patient_stu3 = Patient(
    id="patient-stu3-001",
    active=True,
    name=[
        HumanName(
            use="official",
            family="Williams",
            given=["Sarah"]
        )
    ]
)

# STU3-specific validation and serialization
xml_output = patient_stu3.xml(pretty=True)

STU3 Characteristics:

  • ~100 resource types
  • Mature and stable specification
  • Wide industry adoption
  • Good balance of features and simplicity

DSTU2 Support

FHIR DSTU2 (1.0.2) - Draft Standard for Trial Use 2, legacy version for backward compatibility.

# DSTU2 version-specific imports
from fhir.resources.DSTU2 import (
    Patient, Observation, Encounter, Condition,
    MedicationOrder, DiagnosticReport, Bundle
)

# DSTU2 core functions
from fhir.resources.DSTU2 import get_fhir_model_class, construct_fhir_element

# Version information
from fhir.resources.DSTU2 import __fhir_version__
print(__fhir_version__)  # "1.0.2"

Usage Example:

from fhir.resources.DSTU2 import Patient, HumanName

# Create DSTU2 patient
patient_dstu2 = Patient(
    id="patient-dstu2-001",
    active=True,
    name=[
        HumanName(
            use="official",
            family=["Brown"],  # Note: DSTU2 uses array for family name
            given=["David"]
        )
    ]
)

# DSTU2-specific serialization
json_output = patient_dstu2.json()

DSTU2 Characteristics:

  • ~80 resource types
  • Legacy specification
  • Different resource structures
  • Limited compared to newer versions

Version Migration

Utilities and patterns for migrating resources between FHIR versions.

def migrate_resource(
    resource: FHIRAbstractModel, 
    target_version: str
) -> FHIRAbstractModel:
    """
    Migrate FHIR resource between versions.
    
    Parameters:
    - resource: Source FHIR resource
    - target_version: Target FHIR version ('R5', 'R4B', 'STU3', 'DSTU2')
    
    Returns:
    FHIRAbstractModel - Migrated resource in target version
    
    Note: This is a conceptual function - actual migration
    requires manual handling of version differences
    """

Migration Examples:

from fhir.resources.patient import Patient as PatientR5
from fhir.resources.STU3 import Patient as PatientSTU3

# Manual migration from R5 to STU3
patient_r5 = PatientR5(
    id="patient-001",
    active=True,
    name=[HumanName(family="Smith", given=["John"])]
)

# Convert via dictionary (handles common fields)
patient_dict = patient_r5.dict()

# Create STU3 version (may need field adjustments)
try:
    patient_stu3 = PatientSTU3.parse_obj(patient_dict)
except ValidationError as e:
    # Handle version-specific differences
    print(f"Migration requires manual field mapping: {e}")

Version Comparison

Key differences between FHIR versions to guide version selection.

# Version feature comparison
VERSION_FEATURES = {
    "R5": {
        "resources": 150+,
        "status": "Current",
        "features": ["Advanced search", "Enhanced terminology", "New data types"],
        "use_case": "New implementations, future-proofing"
    },
    "R4B": {
        "resources": 140+,
        "status": "Maintenance",
        "features": ["Selected R5 features", "R4 compatibility"],
        "use_case": "R4 implementations needing specific R5 features"
    },
    "STU3": {
        "resources": 100+,
        "status": "Mature",
        "features": ["Stable specification", "Wide adoption"],
        "use_case": "Production systems, EHR integration"
    },
    "DSTU2": {
        "resources": 80,
        "status": "Legacy",
        "features": ["Basic FHIR functionality"],
        "use_case": "Legacy system support only"
    }
}

Dynamic Version Selection

Runtime version selection based on application requirements.

def get_version_module(version: str):
    """
    Get FHIR version module dynamically.
    
    Parameters:
    - version: str - FHIR version ('R5', 'R4B', 'STU3', 'DSTU2')
    
    Returns:
    Module - FHIR version module
    """
    
def get_resource_class(resource_name: str, version: str = "R5"):
    """
    Get resource class from specific FHIR version.
    
    Parameters:
    - resource_name: str - FHIR resource name
    - version: str - FHIR version (default: R5)
    
    Returns:
    Type[FHIRAbstractModel] - Resource class from specified version
    """

Usage Example:

# Dynamic version selection
def create_patient(version="R5", **kwargs):
    if version == "R5":
        from fhir.resources.patient import Patient
    elif version == "R4B":
        from fhir.resources.R4B import Patient
    elif version == "STU3":
        from fhir.resources.STU3 import Patient
    elif version == "DSTU2":
        from fhir.resources.DSTU2 import Patient
    else:
        raise ValueError(f"Unsupported version: {version}")
    
    return Patient(**kwargs)

# Create patients for different versions
patient_r5 = create_patient("R5", active=True)
patient_stu3 = create_patient("STU3", active=True)

Types

from typing import Literal, Union
from fhir.resources.core.fhirabstractmodel import FHIRAbstractModel

# Version identifiers
FHIRVersion = Literal["R5", "R4B", "STU3", "DSTU2"]

# Version-specific resource types
R5Resource = Union[
    "Patient", "Observation", "Encounter", "Condition",
    # ... all R5 resources
]

R4BResource = Union[
    "Patient", "Observation", "Encounter", "Condition", 
    # ... all R4B resources
]

STU3Resource = Union[
    "Patient", "Observation", "Encounter", "Condition",
    # ... all STU3 resources  
]

DSTU2Resource = Union[
    "Patient", "Observation", "Encounter", "Condition",
    # ... all DSTU2 resources
]

# Migration result
class MigrationResult:
    success: bool
    target_resource: FHIRAbstractModel
    warnings: List[str]
    errors: List[str]

Install with Tessl CLI

npx tessl i tessl/pypi-fhir--resources

docs

administrative-resources.md

clinical-resources.md

core-functions.md

data-types.md

financial-resources.md

index.md

patient-resources.md

serialization.md

version-support.md

tile.json