Registry / serialization / hl7

hl7

JSON →
library0.4.5pypypi✓ verified 50d ago

A simple Python library for parsing HL7 v2.x messages. Provides segment, field, and component-level access to HL7 messages, along with MLLP (Minimal Lower Layer Protocol) client support. Does not handle HL7 FHIR (v4); it is strictly for the pipe-delimited v2.x wire format.

serializationcommunication
pip install hl7
Install & Compatibility
Where this runs
tested against v0.4.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.950 runs
installs and imports cleanly · install 0.0s · import 0.029s · 18MB
glibc
py 3.103.950 runs
installs and imports cleanly · install 1.5s · import 0.027s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

hl7
import hl7
import hl7
Message
from hl7 import Message
import hl7
parse
from hl7 import parse
import hl7

Parse an HL7 v2.x message and access segments and fields.

import hl7 raw = 'MSH|^~\\&|SND|SND_FAC|RCV|RCV_FAC|20230101120000||ADT^A01|MSG00001|P|2.3\rPID|||12345^^^MRG||DOE^JOHN||19800101|M\r' message = hl7.parse(raw) # Access fields using segment(field) notation print(message.segment('MSH')) # Full MSH segment print(message['MSH.9']) # Message type: ADT^A01 print(message['PID.5']) # Patient name: DOE^JOHN print(message.segment('PID')[5]) # Same: DOE^JOHN
Debug
Known issues
gotchaHL7 v2.x messages use \r (carriage return) as the segment separator, NOT \n. Messages with \n line endings will not parse correctly.
fix
Ensure raw messages use \r: raw = raw.replace('\n', '\r') before parsing.
affects: all
breakingThe 'mllp' installation extra was removed along with the synchronous MLLPClient class in 0.4.0. MLLP functionality is now async-only and does not require a separate 'mllp' extra. Attempting to install `hl7[mllp]` on versions >= 0.4.0 will result in a warning.
fix
Migrate to async: reader, writer = await open_hl7_connection(host, port). The async MLLP functionality is now part of the base `hl7` package and does not require the `[mllp]` extra.
affects: >= 0.4.0
gotchaThis library only handles HL7 v2.x pipe-delimited messages. It does NOT support HL7 FHIR (v4 JSON/XML). Use fhir.resources or fhirclient for FHIR.
fix
Use the fhir.resources package for FHIR R4/R5 resources.
affects: all
gotchaField indexing is 0-based on the segment object but MSH segment counts the field separator as MSH.1, so MSH[1] returns '|' and MSH[9] returns the message type. This 1-off confusion is common.
fix
Use the string accessor message['MSH.9'] for clarity, or remember segment()[n] is 0-indexed.
affects: all
gotchaAccessing a non-existent segment with message.segment('ZZZ') raises KeyError, not returning None.
fix
Check segments first: if message.segments('ZZZ'): ... or wrap in try/except KeyError.
affects: all
Upgrade
Version history
0.4.5latest on PyPI
Audit
Dependencies
aiorunoptionalRequired for the async MLLP server/client functionality.
Agent activity
60 hits · last 30 days
node
12
seranking-bot
4
ahrefsbot
2
Amazon
1
mj12bot
1
Resources