FHIR Resources (Python)

raw JSON →
8.2.0 verified Tue May 12 auth: no python install: verified quickstart: verified

Python library for FHIR (Fast Healthcare Interoperability Resources) providing Pydantic-based models for all FHIR resource types. Supports FHIR R4, R4B, R5, STU3, and DSTU2. Built on Pydantic v2 for validation, serialization, and deserialization of FHIR JSON. Current version targets FHIR R5 by default with backwards-compatible imports for older FHIR versions.

pip install fhir.resources
error pydantic.ValidationError: X validation error for Y
cause Input data provided to a FHIR resource constructor does not conform to the FHIR specification's data model, such as incorrect data types, missing required fields, or invalid formats, which Pydantic then flags.
fix
Review the FHIR specification for the specific resource and its fields, ensuring that all required fields are present and data types/formats (e.g., dates, codes, quantities) match the expected schema for the FHIR version being used.
error AttributeError: 'NoneType' object has no attribute 'serialize'
cause A preceding operation, such as a search or retrieval, failed to find a FHIR resource and returned 'None', on which a subsequent method (like '.serialize()' or any other attribute access) was then attempted.
fix
Add a check to verify that the returned object is not 'None' before attempting to access its attributes or methods. For example, resource = Patient.get('some_id') should be followed by if resource: resource.serialize().
error ModuleNotFoundError: No module named 'fhir.resources.R4.patient'
cause Attempting to import a FHIR resource from an incorrect or non-existent version-specific submodule. While `fhir-resources` version 8.2.0 defaults to FHIR R5, older versions (like R4, R4B, STU3) are available in specific submodules, and incorrect paths will lead to this error.
fix
For FHIR R5 resources, import directly from fhir.resources, e.g., from fhir.resources.patient import Patient. For older FHIR versions, ensure you are importing from the correct versioned submodule, e.g., from fhir.resources.R4.patient import Patient for R4, or from fhir.resources.STU3.patient import Patient for STU3.
breaking fhir.resources 7.x+ requires Pydantic v2. Projects still on Pydantic v1 must use fhir.resources 6.x.
fix Either upgrade to Pydantic v2 (pip install 'pydantic>=2.0') or pin fhir.resources<7.0.0 for Pydantic v1 compatibility.
breaking Default FHIR version changed from R4B to R5 in fhir.resources 7.x+. Top-level imports now return R5 models.
fix For R4B resources, import from fhir.resources.R4B.* subpackage instead of top-level fhir.resources.*.
breaking Pydantic v2 migration changed validation and serialization methods. .parse_obj() and .json() are replaced.
fix Use Patient.model_validate(data) instead of Patient.parse_obj(data), and patient.model_dump_json() instead of patient.json().
gotcha The package installs as a namespace package (fhir.resources). Do not create your own 'fhir' package in your project or it will shadow the library.
fix Avoid naming your own modules or packages 'fhir' to prevent import conflicts.
gotcha FHIR resource validation is strict by default. Missing required fields or invalid value sets will raise ValidationError.
fix Catch pydantic.ValidationError and inspect e.errors() for details on which fields failed validation.
gotcha resourceType field must match the class name exactly. Passing resourceType='patient' (lowercase) will raise a validation error.
fix Always use the exact CamelCase resourceType, e.g. 'Patient', 'Observation', 'Bundle'.
breaking fhir.resources 7.x+ relies on Pydantic v2, which may utilize type annotations incompatible with Python 3.9 or earlier (e.g., the `|` union operator syntax introduced in Python 3.10). This can lead to a `TypeError` during model import or evaluation.
fix Upgrade your Python environment to 3.10 or newer to ensure compatibility with Pydantic v2's type hint evaluation. If unable to upgrade Python, consider pinning `fhir.resources<7.0.0` for Pydantic v1 compatibility (as per warning #0).
pip install 'fhir.resources[r4]'
python os / libc variant status wheel install import disk
3.10 alpine (musl) r4 - - 0.56s 52.2M
3.10 alpine (musl) fhir.resources - - 0.57s 52.2M
3.10 slim (glibc) r4 - - 0.39s 52M
3.10 slim (glibc) fhir.resources - - 0.40s 52M
3.11 alpine (musl) r4 - - 0.81s 57.2M
3.11 alpine (musl) fhir.resources - - 0.83s 57.2M
3.11 slim (glibc) r4 - - 0.67s 57M
3.11 slim (glibc) fhir.resources - - 0.69s 57M
3.12 alpine (musl) r4 - - 0.95s 47.8M
3.12 alpine (musl) fhir.resources - - 0.95s 47.8M
3.12 slim (glibc) r4 - - 0.92s 48M
3.12 slim (glibc) fhir.resources - - 0.92s 48M
3.13 alpine (musl) r4 - - 0.55s 47.3M
3.13 alpine (musl) fhir.resources - - 0.52s 47.3M
3.13 slim (glibc) r4 - - 0.54s 47M
3.13 slim (glibc) fhir.resources - - 0.55s 47M
3.9 alpine (musl) r4 - - - -
3.9 alpine (musl) fhir.resources - - - -
3.9 slim (glibc) r4 - - - -
3.9 slim (glibc) fhir.resources - - - -

Create, validate, and serialize a FHIR Patient resource using Pydantic v2 methods.

from fhir.resources.patient import Patient

# Create from dict
patient_data = {
    "resourceType": "Patient",
    "id": "example",
    "active": True,
    "name": [
        {
            "use": "official",
            "family": "Doe",
            "given": ["John"]
        }
    ],
    "gender": "male",
    "birthDate": "1990-01-01"
}

patient = Patient.model_validate(patient_data)
print(patient.name[0].family)  # 'Doe'

# Serialize back to FHIR JSON
print(patient.model_dump_json(indent=2))

# Parse from JSON string
json_str = patient.model_dump_json()
patient2 = Patient.model_validate_json(json_str)
print(patient2.id)  # 'example'