Core¶
- class hl7types.hl7.HL7Model¶
Bases:
BaseModelBase class for all HL7 v2 models.
All generated message, segment, group, and datatype classes inherit from
HL7Model. It extends Pydantic’sBaseModelwith 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=Trueallows field population using the Python attribute name in addition to any alias.validate_by_alias=Trueensures 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,
wireshould be a single segment string. For message and group classes,wireshould be a complete multi-segment message. Use the top-levelhl7types.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, raisespydantic.ValidationErrorwhen required fields or segments are absent. IfFalse, missing required fields are filled with empty placeholder values and aUserWarningis emitted. Defaults toFalse.
- Returns:
A fully validated instance of the calling class.
- Return type:
Self
- Raises:
pydantic.ValidationError – If
strict=Trueand 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'