Extensions

FHIR has two kinds of extensions. Both are fully supported.

Extensions on elements and resources

Every element has extension and (for most) modifierExtension lists. They work like any other field:

>>> from fhir.resources.patient import Patient
>>> patient = Patient.model_validate({
...     "resourceType": "Patient",
...     "extension": [{
...         "url": "http://hl7.org/fhir/StructureDefinition/patient-birthPlace",
...         "valueAddress": {"city": "Amsterdam", "country": "NL"},
...     }],
... })
>>> patient.extension[0].valueAddress.city
'Amsterdam'

Extensions on primitive values

In FHIR JSON a primitive value cannot carry an extension key, so it is placed in a sibling element whose name starts with an underscore: birthDate → _birthDate. In Python, that sibling is the field <name>__ext:

>>> patient = Patient.model_validate({
...     "resourceType": "Patient",
...     "birthDate": "1970-03-30",
...     "_birthDate": {"extension": [{
...         "url": "http://hl7.org/fhir/StructureDefinition/patient-birthTime",
...         "valueDateTime": "1970-03-30T14:35:45-05:00",
...     }]},
... })
>>> patient.birthDate
datetime.date(1970, 3, 30)
>>> patient.birthDate__ext.extension[0].valueDateTime.isoformat()
'1970-03-30T14:35:45-05:00'

It is written back under its FHIR name:

>>> "_birthDate" in patient.model_dump_json()
True

To set it from Python, use the __ext field or the _name alias:

>>> p = Patient(
...     gender="other",
...     gender__ext={"extension": [{
...         "url": "http://example.org/gender-detail", "valueString": "non-binary",
...     }]},
... )
>>> p.model_dump()["_gender"]["extension"][0]["valueString"]
'non-binary'

Repeating primitives

For a list of primitives, _name is a list of the same length, with null where an item has no extension:

>>> from fhir.resources.humanname import HumanName
>>> name = HumanName.model_validate({
...     "given": ["Anna", "Maria"],
...     "_given": [None, {"extension": [{
...         "url": "http://hl7.org/fhir/StructureDefinition/iso21090-EN-qualifier",
...         "valueCode": "CL",
...     }]}],
... })
>>> name.given__ext[0] is None, name.given__ext[1].extension[0].valueCode
(True, 'CL')

Data absent reason

A primitive extension can stand in for a required value that is missing, see Missing required data.

Tip

Fields ending in __ext are not part of the FHIR StructureDefinition; they only exist to carry primitive extensions. Every generated class docstring has a disclaimer about this.