Message structure
How a message is built from segments, fields, components, and the delimiters between them.
An HL7v2 message is plain text. Each line of a message is a segment, and each segment carries one kind of information, such as the patient's identity (PID) or the patient's visit (PV1). Within a segment, single characters called delimiters divide the text into smaller and smaller parts:
- A segment is divided into fields, separated by
|. - A field can be divided into components, separated by
^. - A component can be divided into sub-components, separated by
&.
In the segment PID|1||12345||Doe^John, the fifth field holds the patient name, Doe^John. Its first component is the family name, Doe, and its second is the given name, John.
The position of a value within this hierarchy is what gives the value its meaning. A JSON or XML document labels each value with a name, such as "familyName": "Doe", but an HL7v2 message does not. A system that reads the message knows that Doe is a family name only because the standard defines the first component of the fifth field of PID as the family name. HL7v2 is therefore not a self-describing format: a message can be interpreted only with the standard's definitions at hand.
Origins
The format predates XML and JSON. Its delimited syntax derives from the ASTM laboratory-messaging standards of the mid-1980s and resembles other EDI (opens in a new tab) (Electronic Data Interchange) formats of the period.
Segment
A segment is a group of related fields that represents one unit of information, such as a patient, a visit, or an observation. Each segment is identified by a three-character code, such as MSH (Message Header), PID (Patient Identification), PV1 (Patient Visit), or OBX (Observation/Result). A message is an ordered list of segments, and the HL7v2 standard defines which segments each message contains. The segments reference lists every segment that the standard defines.
Local segments
Segments whose code begins with Z are not defined by the standard; they are reserved for local use. Z-segments describes how they are defined and their costs.
Example
The following message represents a patient admission:
MSH|^~\&|SendingApp|SendingFacility|ReceivingApp|ReceivingFacility|20220101080000||ADT^A01|MessageControlID|P|2.8|
EVN|A01|20220101080000||||
PID|1||RTH9876^^^MRN^MR~12345^^^SSN^SS||LastName^FirstName||
PV1|1|I|AssignedLocation||||||||||||The message consists of four segments, each on its own line. Segments are separated by a carriage return (\r, hex 0x0D).
Carriage returns
Many editors do not display a carriage return as a line break, so a raw message often appears as a single line or with ^M markers at the end of each segment. Line endings describes how systems differ in the segment delimiters they accept.
The segments in this example are:
| Segment | Title | Description |
|---|---|---|
| MSH | Message Header | The first segment in every HL7v2 message. Carries information about the message itself: type, control ID, sending and receiving applications, version, and the timestamp of message creation. |
| EVN | Event Type | Information about the event that triggered the message: event type code, event date and time, and other relevant details. |
| PID | Patient Identification | Demographic information about a patient: identifiers, name, date of birth, sex, address, contact details. |
| PV1 | Patient Visit | Information about a patient's visit or encounter: patient class, assigned location, attending physician, admission and discharge dates. |
Optionality
Not every segment appears in every message. For each message, the HL7v2 standard specifies which segments are required, which are optional, and which can repeat, and the order in which they appear. The MSH segment is required in every message and is always the first segment.
The standard writes these rules in a notation called abstract message syntax. A segment code on its own is required, square brackets mark an optional segment, and braces mark a segment that can repeat. In simplified form, the standard defines an admission message as follows:
MSH
EVN
PID
[ PD1 ]
[{ NK1 }]
PV1
[ PV2 ]
[{ DG1 }]
...MSH, EVN, PID, and PV1 are required, so every admission message contains them, as the example message does. PD1 (additional demographics) and PV2 (additional visit information) are optional. NK1 (next of kin) and DG1 (diagnosis) are optional and can repeat, because a patient can have several next of kin and several diagnoses. The ADT A01 reference lists the complete definition.
Profiles
The standard's definition is the widest that any interface can use. A profile narrows it for one interface: it can make an optional segment required, exclude a segment that the interface does not use, or limit how many times a segment can repeat.
Group
A group is a set of segments that belong together, such as an order and the observations that report on it. The standard treats a group as a unit: like a single segment, a group can be required or optional, and it can repeat. A results message repeats its order group once for each order, and each order group repeats its observation group once for each result.
In abstract message syntax, uppercase names identify groups. In simplified form, the standard defines a results message as follows:
MSH
{ PATIENT_RESULT
[ PATIENT
PID
[ PV1 ]
]
{ ORDER_OBSERVATION
[ ORC ]
OBR
[{ NTE }]
[{ OBSERVATION
OBX
[{ NTE }]
}]
}
}This definition contains one or more patient results. Each patient result has an optional patient group and one or more order groups, and each order group contains an OBR segment and zero or more observations.
Purpose
Groups record how segments relate to one another, and the same segment can mean different things depending on the group it belongs to. In the following segments, both NTE segments are notes:
OBR|1||1001|CBC^Complete blood count
NTE|1||Specimen slightly hemolyzed
OBX|1|NM|HGB^Hemoglobin||13.5|g/dL
NTE|1||Repeated to confirmThe first NTE segment follows the OBR segment, so it belongs to the order group and comments on the whole order. The second follows an OBX segment, so it belongs to that observation group and comments on the hemoglobin result alone. Results, notes, and other segments are attached to the right order and the right observation only through their groups, and a system that assigns them to the wrong group reports clinical data against the wrong test.
Structure
Groups do not appear in the transmitted message. A message is a flat sequence of segments: its delimiters mark segments, fields, components, and sub-components, but no delimiter marks where a group begins or ends, and group names such as ORDER_OBSERVATION are not written. Groups exist only in the standard's definition of each message.
The groups are therefore determined by the order of the segments, read against the definition of the message. In a results message, an OBR segment begins a new order group, and the OBX and NTE segments that follow it belong to that group until the next OBR segment. Two consequences follow:
- Segment order carries meaning. An
OBXsegment placed before itsOBRsegment belongs to a different order, or to none. - Groups depend on the definition. The same sequence of segments can form different groups under different definitions. The definition that applies is identified in
MSH-9, by the message type, the trigger event, and the message structure.
Field
A field is a single data element within a segment. Fields are numbered from 1, and the meaning of each field is determined by its position: in the PID segment, field 5 is the patient name and field 7 is the date of birth. Fields are separated by the field delimiter, the pipe character |.
The notation PID-5 means "field 5 of the PID segment". A component number is appended with a dot: PID-5.1 is the first component of the patient name, the family name. The standard uses this notation throughout.
The number of fields varies by segment: the PID segment defines 40 fields in version 2.8, although most messages populate far fewer. The PID segment reference lists each field with its position, data type, and optionality.
MSH numbering
The MSH segment is numbered differently from every other segment. Its first field, MSH-1, contains the field delimiter itself, and its second field, MSH-2, contains the encoding characters. In MSH|^~\&|SendingApp|…, the value SendingApp is therefore MSH-3, not MSH-2. Counting field delimiters from the segment code gives the correct field number for every segment except MSH, where the count is one lower than the field number. The sending application is MSH-3, the message type is MSH-9, the control ID is MSH-10, and the version is MSH-12.
Empty field
Many fields are optional. Because a field is identified by its position, an empty field must still be present, so that the fields after it keep their positions. An empty field is represented by two adjacent delimiters. In the following segment, the mother's maiden name (PID-6) is empty, and the date of birth remains in PID-7:
PID|1||12345^^^MRN^MR||Doe^John||19700101|MTrailing empty fields can be omitted. PID|1||12345 and PID|1||12345|||||| carry the same information, and most source systems omit them.
Null value
HL7v2 distinguishes between a field that is absent and a field that is null. An empty field, with nothing between the delimiters, means that the message provides no information about the field, and a destination that updates its records leaves the stored value unchanged. A field that contains exactly two double quotes, "", is a null value, and the destination is expected to delete the stored value.
PID|1||12345||Doe^John||19700101|M|||""|In this example, the patient's address (PID-11) is explicitly cleared. The distinction is most significant in update messages such as ADT^A08: a source that sends "" in error deletes stored data, and a source that never sends "" cannot clear a stored value.
Repetition
Some fields can hold several values, because a patient can have more than one identifier, address, or telephone number. Repetitions are separated by the repetition delimiter, the tilde ~. In the example message, PID-3 carries two identifiers, a medical record number and a social security number, separated by ~. The field definition specifies whether a field can repeat and how many times.
Component
A field can be divided into components, separated by the component delimiter ^. The patient name LastName^FirstName is one field (PID-5) with two components. Patient identifiers, addresses, physician names, and coded values are structured in the same way: one field with several parts, each with its own position and meaning.
A component can in turn be divided into sub-components separated by &. Sub-components are the lowest level of the hierarchy.
The components of a field are defined by the field's data type, not by the segment. PID-5 has components because its data type is XPN (extended person name), and every XPN field in the standard has the same components in the same order. Data types describes how data types define components.
Delimiters
Delimiters are the characters that separate segments, fields, components, repetitions, and sub-components, and they define the structure of a message. JSON and XML enclose values between opening and closing marks; HL7v2 instead uses a small set of single-character delimiters, one for each level of the hierarchy.
The default HL7v2 delimiters are:
| Delimiter | Character | hex | Description |
|---|---|---|---|
| Segment | \r | 0x0D | The carriage return separates segments within a message. |
| Field | | | 0x7C | The pipe separates fields within a segment. |
| Component | ^ | 0x5E | The caret separates components within a field: last name, first name, middle name. |
| Repetition | ~ | 0x7E | The tilde separates repetitions of a field: one identifier, then another. |
| Escape | \ | 0x5C | The backslash introduces an escape sequence, so a value can contain a delimiter character without breaking the structure. |
| Sub-component | & | 0x26 | The ampersand separates sub-components within a component. |
The segment delimiter is fixed by the standard. The other five are declared at the start of every message: the character after MSH is the field delimiter, and the four characters after it, the encoding characters in MSH-2, are the component, repetition, escape, and sub-component delimiters, in that order. The first eight characters of every message, MSH|^~\&, therefore declare the delimiters for the rest of the message. Version 2.7 added an optional fifth encoding character, #, the truncation character, which marks a value that the source has shortened.
Custom delimiters
Because each message declares its own delimiters, a source can use characters other than the defaults. In practice, almost all systems use the default delimiters, and many destination systems do not accept others.
Line endings
The standard specifies the carriage return as the only segment delimiter. Messages written by hand, or by tools that treat messages as text files, often use a line feed \n or the pair \r\n instead. Many destination systems accept all three, but a destination that follows the standard strictly does not, so a message that is accepted by one destination can be rejected by another. Source systems should terminate every segment with \r alone.
Escape sequences
Because the delimiters are ordinary printable characters, a value that contains a delimiter would alter the structure of the message. A laboratory named "Smith & Jones" cannot be sent unchanged, because & is the sub-component delimiter. The escape character introduces a sequence that represents the delimiter instead:
| Sequence | Stands for |
|---|---|
\F\ | Field delimiter (|) |
\S\ | Component delimiter (^) |
\T\ | Sub-component delimiter (&) |
\R\ | Repetition delimiter (~) |
\E\ | Escape character (\) |
\Xdd…\ | One or more bytes given in hexadecimal. |
\.br\ | A line break inside formatted text (FT data type). |
\H\, \N\ | Start and end of highlighted text (FT data type). |
\Zdd…\ | A locally defined escape. |
The laboratory name is therefore sent as Smith \T\ Jones, and the destination decodes it to Smith & Jones after splitting the message into fields. The order of these steps is significant: if the destination decoded escape sequences before splitting, the restored & would be treated as a delimiter.
Escape sequences are meaningful only in text data types such as ST, TX, and FT. Numeric and date fields do not contain them.
Character encoding
The delimiters are ASCII characters, and ASCII is the default character set for the rest of the message. A message that carries characters outside ASCII, such as accented letters in names and addresses, declares its character set in field MSH-18; UNICODE UTF-8 is the most common value. The source and the destination must use the same encoding. A mismatch corrupts non-ASCII characters, and because test data often contains none, the error frequently appears only after the interface is in production.
Batch
The standard also defines an envelope for sending several messages together, usually as a file. A batch consists of a BHS (batch header) segment, the messages, and a BTS (batch trailer) segment, and a file of batches is enclosed in FHS and FTS segments. Batches are used for file-based exchange, such as scheduled transfers in billing and reporting interfaces. Real-time interfaces over MLLP send one message at a time and do not use batches.
Trade-offs
HL7v2 predates the data formats that current software platforms support natively, so implementations depend on specialized libraries. Several properties of the format, however, account for its continued use. Messages are compact: a complete admission message is a few hundred bytes. Messages are plain text and can be read with the segment reference at hand. New fields are added at the end of segments, so a system built for one version can read messages written for another, as described under Versioning. In addition, replacing an interface that is in production carries cost and risk, which favors keeping existing HL7v2 interfaces in service.