Base FHIR Models
These classes provide base for constructing Fhircraft-compatible Pydantic FHIR models.
FHIRContextMixin
Path: fhircraft.fhir.resources.base.mixins.context.FHIRContextMixin
Mixin that provides hierarchical parent-context tracking.
Every FHIR model instance stores a reference to its direct parent (_parent) and its position within a list (_index). The root resource and nearest enclosing resource are resolved lazily by walking the parent chain, avoiding the need to store and update those references.
FHIRPolymorphicMixin
Path: fhircraft.fhir.resources.base.mixins.polymorphic.FHIRPolymorphicMixin
Mixin providing polymorphic serialization and deserialization helpers.
These are helpers called from the Pydantic validators/serializers that remain on FHIRBaseModel; they are not validators themselves and carry no Pydantic decorator magic.
FHIRXMLMixin
Path: fhircraft.fhir.resources.base.mixins.xml.FHIRXMLMixin
Mixin providing XML serialization and deserialization for FHIR models.
Implements the FHIR XML format as described in the FHIR specification, including correct handling of primitive shadow elements and namespace.
Methods:
| Name | Description |
|---|---|
model_dump_xml |
Serialize the FHIR resource to an XML string. |
model_validate_xml |
Deserialize a FHIR XML string into a model instance. |
model_dump_xml
model_dump_xml(*, indent: int | None = None, ensure_ascii: bool = True, include: IncEx | None = None, exclude: IncEx | None = None, exclude_unset: bool = False, exclude_none: bool = False, exclude_defaults: bool = False) -> str
Serialize the FHIR resource to an XML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
int | None
|
Number of indent levels. |
None
|
ensure_ascii
|
bool
|
When True, non-ASCII characters are escaped. |
True
|
include
|
IncEx | None
|
Fields to include (passed through to model_dump). |
None
|
exclude
|
IncEx | None
|
Fields to exclude (passed through to model_dump). |
None
|
exclude_unset
|
bool
|
Exclude fields that were not explicitly set. |
False
|
exclude_none
|
bool
|
Exclude fields whose value is None. |
False
|
exclude_defaults
|
bool
|
Exclude fields that equal their default value. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
An XML string conforming to the FHIR XML format. |
Source code in fhircraft/fhir/resources/base/mixins/xml.py
model_validate_xml
classmethod
Deserialize a FHIR XML string into a model instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_data
|
str
|
The XML string to deserialize. |
required |
strict
|
bool | None
|
Whether to validate strictly. |
None
|
context
|
Any
|
Additional context forwarded to model_validate. |
None
|
Returns:
| Type | Description |
|---|---|
T
|
A validated model instance populated from the XML data. |
Source code in fhircraft/fhir/resources/base/mixins/xml.py
FHIRBaseModel
Path: fhircraft.fhir.resources.base.models.FHIRBaseModel
Bases: BaseModel, ABC, FHIRContextMixin, FHIRXMLMixin, FHIRPolymorphicMixin, FHIRPathMixin
Abstract base class for all FHIR resource and data-type models.
Extends Pydantic's BaseModel with FHIR-specific behaviour: - Hierarchical parent-context tracking (_parent / _index) - Polymorphic serialization and deserialization - XML serialization and deserialization - Profile-slice construction and introspection - FHIRPath expression evaluation (via FHIRPathMixin)
This class is abstract and may not be instantiated directly. All concrete FHIR types are generated subclasses that define _type.
Methods:
| Name | Description |
|---|---|
model_post_init |
Wire up parent-context tracking after construction. |
model_validate |
Validate obj and return a model instance. |
model_validate_json |
Deserialize json_data and return a validated model instance. |
model_construct |
Construct a model instance without running validation. |
model_copy |
Return a copy of this instance with parent context reset. |
model_post_init
model_post_init(context: Any) -> None
model_validate
classmethod
model_validate(obj: Any, *, strict: bool | None = None, from_attributes: bool | None = None, context: Any = None, extra: ExtraValues | None = None, by_alias: bool | None = None, by_name: bool | None = None) -> Self
Validate obj and return a model instance.
Source code in fhircraft/fhir/resources/base/models.py
model_validate_json
classmethod
model_validate_json(json_data: str | bytes | bytearray, *, strict: bool | None = False, context: Any = None, extra: ExtraValues | None = None, by_alias: bool | None = None, by_name: bool | None = None) -> Self
Deserialize json_data and return a validated model instance.
Source code in fhircraft/fhir/resources/base/models.py
model_construct
classmethod
Construct a model instance without running validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
set_defaults
|
bool
|
When True (default), fields with a default value or factory are pre-populated. |
True
|
Returns:
| Type | Description |
|---|---|
Self
|
An unvalidated instance of the model. |
Source code in fhircraft/fhir/resources/base/models.py
model_copy
Return a copy of this instance with parent context reset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update
|
Mapping[str, Any] | None
|
Optional field updates to apply to the copy. |
None
|
deep
|
bool
|
Whether to perform a deep copy. |
False
|
Returns:
| Type | Description |
|---|---|
Self
|
A copied instance with no parent context. |
Source code in fhircraft/fhir/resources/base/models.py
FHIRList
Path: fhircraft.fhir.resources.base.models.FHIRList
FHIRList(items: Iterable[Any] | None = None, parent: FHIRBaseModel | None = None)
Bases: list
Custom list wrapper that maintains parent context on mutations.
This list automatically propagates _parent, _root_resource, _resource, and _index context to FHIRBaseModel items when they are added via append, extend, insert, or setitem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[Any] | None
|
Initial items for the list. |
None
|
parent
|
FHIRBaseModel | None
|
The parent FHIRBaseModel instance that owns this list. |
None
|
Methods:
| Name | Description |
|---|---|
append |
Append item and propagate context. |
extend |
Extend list and propagate context to new items. |
insert |
Insert item and propagate context. |
Source code in fhircraft/fhir/resources/base/models.py
append
append(item: Any)
Append item and propagate context.
Source code in fhircraft/fhir/resources/base/models.py
extend
Extend list and propagate context to new items.
Source code in fhircraft/fhir/resources/base/models.py
insert
insert(index: SupportsIndex, item: Any)
Insert item and propagate context.
Source code in fhircraft/fhir/resources/base/models.py
FHIRModelKind
Path: fhircraft.fhir.resources.base.models.FHIRModelKind
FHIRSliceModel
Path: fhircraft.fhir.resources.base.models.FHIRSliceModel
Bases: FHIRBaseModel
Base class for representation of FHIR profiled slices as Pydantic objects.
Expands the FHIRBaseModel class with slice-specific methods.
Base64BinaryBase
Path: fhircraft.fhir.resources.base.primitives.Base64BinaryBase
Base64BinaryBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR base64Binary types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
BooleanBase
Path: fhircraft.fhir.resources.base.primitives.BooleanBase
BooleanBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR boolean types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
bool | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
CanonicalBase
Path: fhircraft.fhir.resources.base.primitives.CanonicalBase
CanonicalBase(__value: Any | None = None, **data)
CodeBase
Path: fhircraft.fhir.resources.base.primitives.CodeBase
CodeBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR code types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
DateBase
Path: fhircraft.fhir.resources.base.primitives.DateBase
DateBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR date types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
DateTimeBase
Path: fhircraft.fhir.resources.base.primitives.DateTimeBase
DateTimeBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR dateTime types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
DecimalBase
Path: fhircraft.fhir.resources.base.primitives.DecimalBase
DecimalBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR decimal types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
FHIRPrimitiveModel
Path: fhircraft.fhir.resources.base.primitives.FHIRPrimitiveModel
FHIRPrimitiveModel(__value: Any | None = None, **data)
Bases: FHIRBaseModel
Base class for FHIR primitive types.
FHIR primitives are represented as Pydantic models with a single value field that holds the actual primitive value.
This design allows us to attach extensions to primitive values while still treating them as simple types in most contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any | None
|
The actual value |
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
IdBase
Path: fhircraft.fhir.resources.base.primitives.IdBase
IdBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR id types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
InstantBase
Path: fhircraft.fhir.resources.base.primitives.InstantBase
InstantBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR instant types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
Integer64Base
Path: fhircraft.fhir.resources.base.primitives.Integer64Base
Integer64Base(__value: Any | None = None, **data)
Bases: IntegerBase
A release-independent base metaclass for FHIR integer64 types (R5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
IntegerBase
Path: fhircraft.fhir.resources.base.primitives.IntegerBase
IntegerBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR integer types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
MarkdownBase
Path: fhircraft.fhir.resources.base.primitives.MarkdownBase
MarkdownBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR markdown types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
OidBase
Path: fhircraft.fhir.resources.base.primitives.OidBase
OidBase(__value: Any | None = None, **data)
PositiveIntBase
Path: fhircraft.fhir.resources.base.primitives.PositiveIntBase
PositiveIntBase(__value: Any | None = None, **data)
Bases: IntegerBase
A release-independent base metaclass for FHIR positiveInt types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
StringBase
Path: fhircraft.fhir.resources.base.primitives.StringBase
StringBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR string types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
TimeBase
Path: fhircraft.fhir.resources.base.primitives.TimeBase
TimeBase(__value: Any | None = None, **data)
Bases: FHIRPrimitiveModel
A release-independent base metaclass for FHIR time types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
UnsignedIntBase
Path: fhircraft.fhir.resources.base.primitives.UnsignedIntBase
UnsignedIntBase(__value: Any | None = None, **data)
Bases: IntegerBase
A release-independent base metaclass for FHIR unsignedInt types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
UriBase
Path: fhircraft.fhir.resources.base.primitives.UriBase
UriBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR uri types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|
Source code in fhircraft/fhir/resources/base/primitives.py
UrlBase
Path: fhircraft.fhir.resources.base.primitives.UrlBase
UrlBase(__value: Any | None = None, **data)
UuidBase
Path: fhircraft.fhir.resources.base.primitives.UuidBase
UuidBase(__value: Any | None = None, **data)
XhtmlBase
Path: fhircraft.fhir.resources.base.primitives.XhtmlBase
XhtmlBase(__value: Any | None = None, **data)
Bases: StringBase
A release-independent base metaclass for FHIR xhtml types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
|
None
|