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.resourcesVerified import paths — ran on the pinned version, not inferred.
Create, validate, and serialize a FHIR Patient resource using Pydantic v2 methods.
Either upgrade your Python environment to 3.10 or later and install 'pydantic>=2.0', or pin fhir.resources<7.0.0 for Pydantic v1 compatibility (which supports Python 3.8+).
For R4B resources, import from fhir.resources.R4B.* subpackage instead of top-level fhir.resources.*.
Use Patient.model_validate(data) instead of Patient.parse_obj(data), and patient.model_dump_json() instead of patient.json().
Avoid naming your own modules or packages 'fhir' to prevent import conflicts.
Catch pydantic.ValidationError and inspect e.errors() for details on which fields failed validation.
Always use the exact CamelCase resourceType, e.g. 'Patient', 'Observation', 'Bundle'.
Ensure that your Pydantic installation is compatible with your `fhir.resources` version. For `fhir.resources` v7.0.0 and above, upgrade Pydantic to version 2.x: `pip install --upgrade pydantic`. For older `fhir.resources` versions, you might need to downgrade Pydantic: `pip install 'pydantic<2'`.
Provide a valid value for the missing required field. For example, when creating a `Patient` resource, ensure all mandatory fields as per the FHIR specification are included:
```python
from fhir.resources.R5.patient import Patient
patient_data = {
"resourceType": "Patient",
"id": "example",
"active": True,
"name": [
{
"use": "official",
"family": "Chalmers",
"given": ["Peter"]
}
],
"gender": "male",
"birthDate": "1974-12-25"
}
patient = Patient(**patient_data)
# Or, if loading from JSON string
# patient = Patient.parse_raw(json_string_data)
```
Note: `Patient` itself does not have a 'status' field, but other resources like `Observation` or `ServiceRequest` do. The example `Patient` data is shown to illustrate providing required fields.Import resources from the correct FHIR version sub-package. For R4B resources, use `from fhir.resources.R4B.patient import Patient`. For STU3 resources, use `from fhir.resources.STU3.patient import Patient`. If you intend to use the default R5, simply use `from fhir.resources.R5.patient import Patient` or `from fhir.resources.patient import Patient` (as R5 is the default).
Ensure that the input data strictly conforms to the FHIR specification for the target resource and version. Remove any extraneous fields that are not part of the standard, or ensure that you are using the correct FHIR version's resource definition. For example, if parsing a resource, inspect the input JSON to remove unexpected keys:
```python
from fhir.resources.R5.patient import Patient
# This will raise 'extra fields not permitted' because 'unexpectedField' is not part of Patient
malformed_data = {
"resourceType": "Patient",
"id": "example",
"active": True,
"gender": "male",
"unexpectedField": "some value"
}
try:
patient = Patient(**malformed_data)
except Exception as e:
print(e)
# Corrected data
correct_data = {
"resourceType": "Patient",
"id": "example",
"active": True,
"gender": "male"
}
patient = Patient(**correct_data)
```