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.get_fhir_model_class(model_name)[source]
Parameters:

model_name (str)

Return type:

type[FHIRAbstractModel]

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.

Parameters:

fhir_comments (str | List[str] | None)

classmethod set_global_model_config(**overrides)[source]

Set model_config overrides (any pydantic.ConfigDict key) applied to every FHIR model defined afterwards. Must be called before importing fhir.resources models; already-defined models keep their config. See Global model config.

classmethod elements_sequence()[source]

returning all element names from Resource according to specification, with preserving the original sequence order.

Return type:

List[str]

classmethod summary_elements_sequence()[source]

returning all element names (those have summary mode are enabled) from Resource according to specification, with preserving the original sequence order.

Return type:

List[str]

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:

str

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:

str

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:

str

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:

Dict[str, Any]

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:
  • yaml_data (str | bytes | bytearray) – The YAML data to validate.

  • strict (bool | None) – Whether to enforce types strictly.

  • context (Any | None) – Extra variables to pass to the validator.

Returns:

The validated Pydantic model.

Raises:

ValueError – If yaml_data is not a YAML string.

Return type:

Self

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:
  • xml_data (str | bytes | bytearray) – The YAML data to validate.

  • strict (bool | None) – Whether to enforce types strictly.

  • context (Any | None) – Extra variables to pass to the validator.

  • xmlparser (Any | None) – Custom XML parser to use. If not provided, the default parser will be used.

Returns:

The validated Pydantic model.

Raises:

ValueError – If xml_data is not valid XML.

Return type:

Self

classmethod get_global_model_config()[source]

Return the model_config currently in effect globally.

Return type:

Dict[str, Any]

classmethod reset_global_model_config()[source]

Restore the default global model_config.

Only affects models defined/imported afterward; models that are already defined keep the config they were created with.

Return type:

None

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_LENGTH

Maximum length of an id. Default 255.

TYPES_STRING_ALLOW_EMPTY_STR

Allow "" as a string value. Default False.

HAS_XML_SUPPORT / HAS_YAML_SUPPORT

Whether lxml / PyYAML are installed.