Core

class hl7types.hl7.HL7Model

Bases: BaseModel

Base class for all HL7 v2 models.

All generated message, segment, group, and datatype classes inherit from HL7Model. It extends Pydantic’s BaseModel with ER7 and XML serialisation methods and enables both positional field names and human-readable aliases on input.

Parameters:

**data (Any) – Field values accepted by name, human-readable alias, or HL7 dot notation (e.g. MSH.3). All three forms are equivalent.

model_config

Pydantic configuration. populate_by_name=True allows field population using the Python attribute name in addition to any alias. validate_by_alias=True ensures alias-keyed input is validated.

Type:

ConfigDict

Examples

>>> from hl7types.hl7.v2_5_1.datatypes import HD
>>> HD(hd_1="WPAS").hd_1
'WPAS'
>>> HD(hierarchic_designator_namespace_id="WPAS").hd_1
'WPAS'
>>> HD(**{"HD.1": "WPAS"}).hd_1
'WPAS'
model_dump_er7(segment_separator='\r', enc=None)

Encode the model to an ER7 (pipe-delimited) wire string.

For segment models, returns a single segment string with no trailing separator. For message and group models, returns all segments joined by segment_separator.

Parameters:
  • segment_separator (str, optional) – Character used to join segments. The HL7 v2 specification mandates a carriage return (\r, ASCII 0x0D). Defaults to "\r".

  • enc (EncodingChars, optional) – Delimiter characters to use for encoding. Defaults to the standard HL7 encoding characters (|, ^, ~, \, &).

Returns:

ER7-encoded wire string.

Return type:

str

Examples

>>> from hl7types.hl7.v2_5_1.segments import MSA
>>> MSA(msa_1="AA", msa_2="MSG001").model_dump_er7()
'MSA|AA|MSG001'
classmethod model_validate_er7(wire, segment_separator='\r', enc=None, *, strict=False)

Decode an ER7 wire string into a typed model instance.

For segment classes, wire should be a single segment string. For message and group classes, wire should be a complete multi-segment message. Use the top-level hl7types.decode_er7() helper instead when the message type should be resolved automatically from MSH.

Parameters:
  • wire (str) – ER7-encoded wire string to decode.

  • segment_separator (str, optional) – Character used to split segments. Defaults to "\r".

  • enc (EncodingChars, optional) – Delimiter characters to use for decoding. When None, the decoder reads the encoding characters from MSH in the wire string.

  • strict (bool, optional) – If True, raises pydantic.ValidationError when required fields or segments are absent. If False, missing required fields are filled with empty placeholder values and a UserWarning is emitted. Defaults to False.

Returns:

A fully validated instance of the calling class.

Return type:

Self

Raises:
  • pydantic.ValidationError – If strict=True and required fields are missing, or if any field value fails format validation.

  • ValueError – If the wire string is empty or the message type cannot be resolved.

Examples

>>> from hl7types.hl7.v2_5_1.segments import MSA
>>> seg = MSA.model_validate_er7("MSA|AA|MSG001")
>>> seg.msa_1
'AA'