FHIR Resources (Python)
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.
Warnings
- breaking fhir.resources 7.x+ requires Pydantic v2. Projects still on Pydantic v1 must use fhir.resources 6.x.
- breaking Default FHIR version changed from R4B to R5 in fhir.resources 7.x+. Top-level imports now return R5 models.
- breaking Pydantic v2 migration changed validation and serialization methods. .parse_obj() and .json() are replaced.
- 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.
- gotcha FHIR resource validation is strict by default. Missing required fields or invalid value sets will raise ValidationError.
- gotcha resourceType field must match the class name exactly. Passing resourceType='patient' (lowercase) will raise a validation error.
Install
-
pip install fhir.resources -
pip install 'fhir.resources[r4]'
Imports
- Patient
from fhir.resources.patient import Patient
- Patient (R4)
from fhir.resources.R4B.patient import Patient
- Patient (STU3)
from fhir.resources.STU3.patient import Patient
- Bundle
from fhir.resources.bundle import Bundle
Quickstart
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'