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 to Pydantic v2 (pip install 'pydantic>=2.0') or pin fhir.resources<7.0.0 for Pydantic v1 compatibility.
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'.
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).
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.
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()`.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.