API reference¶
The generated models are thin: almost all behavior comes from
fhir-core, which fhir.resources
depends on. This page documents the public API you use day-to-day.
fhir.resources¶
- fhir.resources.__version__¶
The package version, e.g.
"8.4.0".
- fhir.resources.__fhir_version__¶
The FHIR version of the default (top-level) models:
"5.0.0".
Base model¶
Every resource and data type inherits from
fhir_core.fhirabstractmodel.FHIRAbstractModel, which itself is a
pydantic.BaseModel. Pydantic’s own methods (model_validate,
model_validate_json, model_copy, model_fields …) are available too;
see the Pydantic documentation.
- class fhir_core.fhirabstractmodel.FHIRAbstractModel(*, fhir_comments=None)[source]¶
Abstract base model class for all FHIR elements.
- classmethod set_global_model_config(**overrides)[source]¶
Set
model_configoverrides (anypydantic.ConfigDictkey) applied to every FHIR model defined afterwards. Must be called before importingfhir.resourcesmodels; already-defined models keep their config. See Global model config.
- classmethod elements_sequence()[source]¶
returning all element names from
Resourceaccording to specification, with preserving the original sequence order.
- classmethod summary_elements_sequence()[source]¶
returning all element names (those have summary mode are enabled) from
Resourceaccording to specification, with preserving the original sequence order.
- model_dump_json(*, indent=None, exclude_comments=False, summary_only=False, **pydantic_kwargs)[source]¶
Usage docs: https://docs.pydantic.dev/2.7/concepts/serialization/#modelmodel_dump_json
Generates a JSON representation of the model using Pydantic’s to_json method.
- Parameters:
indent (int | None) – Indentation to use in the JSON output. If None is passed, the output will be compact.
exclude_comments (bool) – If the FHIR comment should be excluded. By default, FHIR comments are included.
pydantic_kwargs (Any) – Original pydantic BaseModel.model_dump() parameters. Normally you don’t need to use other params.
exclude_comments
summary_only (bool)
- Returns:
A JSON string representation of the model.
- Return type:
- model_dump_yaml(*, indent=None, exclude_comments=False, summary_only=False, **pydantic_kwargs)[source]¶
Generates a YAML representation of the model using PyYAML.
- Parameters:
indent (int | None) – Indentation to use in the YAML output. If None is passed, the output will be compact.
exclude_comments (bool) – If the FHIR comment should be excluded. By default, FHIR comments are included.
summary_only (bool) – If only the FHIR summary element should be included.
pydantic_kwargs (Any) – Original pydantic BaseModel.model_dump() parameters. Normally you don’t need to use other params.
- Returns:
A YAML string representation of the model.
- Return type:
- model_dump_xml(*, pretty_print=False, xml_declaration=True, exclude_comments=False, summary_only=False, **pydantic_kwargs)[source]¶
Generates a YAML representation of the model using PyYAML.
- Parameters:
pretty_print (bool) –
.
exclude_comments (bool) – If the FHIR comment should be excluded. By default, FHIR comments are included.
xml_declaration
summary_only (bool) – If only the FHIR summary element should be included.
pydantic_kwargs (Any) – Original pydantic BaseModel.model_dump() parameters. Normally you don’t need to use other params.
- Returns:
A XML string representation of the model.
- Return type:
- model_dump(*, exclude_comments=False, summary_only=False, **pydantic_kwargs)[source]¶
Usage docs: https://docs.pydantic.dev/2.7/concepts/serialization/#modelmodel_dump
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
- Parameters:
exclude_comments (bool) – If the FHIR comment should be excluded. By default, FHIR comments are included.
summary_only (bool) – If only the FHIR summary element should be included.
pydantic_kwargs (Any) – Original pydantic BaseModel.model_dump() parameters. Normally you don’t need to use other params.
- Returns:
A dictionary representation of the model.
- Return type:
- classmethod model_validate_yaml(yaml_data, *, strict=None, context=None)[source]¶
Usage docs: https://pypi.org/project/fhir.resources/#YAML
Validate the given YAML data against the Pydantic model.
- Parameters:
- Returns:
The validated Pydantic model.
- Raises:
ValueError – If yaml_data is not a YAML string.
- Return type:
- classmethod model_validate_xml(xml_data, *, strict=None, context=None, xmlparser=None)[source]¶
Usage docs: https://pypi.org/project/fhir.resources/#XML
Validate the given XML data against the Pydantic model.
- Parameters:
- Returns:
The validated Pydantic model.
- Raises:
ValueError – If xml_data is not valid XML.
- Return type:
Type helpers¶
fhir.resources.fhirtypes (and fhir.resources.R4B.fhirtypes,
fhir.resources.STU3.fhirtypes) defines a <Name>Type annotation for every
resource and data type, e.g. PatientType, HumanNameType, and the
primitive types (DateType, IdType, CodeType …). Use them as field
annotations in your own Pydantic models; see Pydantic and FastAPI.
Global constraints¶
fhir_core.constraints holds process-wide settings, see
Configuration:
TYPES_ID_MAX_LENGTHMaximum length of an
id. Default255.TYPES_STRING_ALLOW_EMPTY_STRAllow
""as astringvalue. DefaultFalse.HAS_XML_SUPPORT/HAS_YAML_SUPPORTWhether
lxml/PyYAMLare installed.