crs4 / hl7apy

Python library to parse, create and handle HL7 v2 messages.
http://crs4.github.io/hl7apy/
MIT License
228 stars 90 forks source link

OBR-44 Procedure Code not parsing correctly #133

Closed Blackglade closed 2 months ago

Blackglade commented 3 months ago

I have this anonymized HL7 message:

MSH|^~\\&|TEST|TEST|||1111||ORM^O01|ABCD|T|2.3
PID|||1111||TEST^PERSON||1231231|M|||SOME RANDOM ADDRESS||(^^^11111@GMAIL.COM|(111)111-111||Other
PV1||O|TPK||||RANDOM INFO|||||||||||||||||||||||||||||||||||||1231231
IN1|1|MEDICAL|MEDICAL|MEDICAL|USA||123-123-123||MEDICAL|||||12312312||TEST^PERSON|Self|237892347|USA|||||||||||||||||U12312345
ORC|XO|123123|||CM||^^^789023234676789345123|||||PROVIDER|ACTT1||||ACT
OBR||123123||CT CHEST|RO||2057895757890||||||Cherst area|||DOCTOR|^^^^^^^^Phone~^^^^^^^^Page~^^^^^^^^Mobile Phone~^^^^^^^^Fax Number||SAMPLE DATA|CLINIC|CT        |||CT^ACTT1^ACTT1|||^^15^2057895757890|||||||||2057895757890||||||||72131

Here is my code to parse the message:

hl7_payload = """MSH|^~\\&|TEST|TEST|||1111||ORM^O01|ABCD|T|2.3\rPID|||1111||TEST^PERSON||1231231|M|||SOME RANDOM ADDRESS||(^^^11111@GMAIL.COM|(111)111-111||Other\rPV1||O|TPK||||RANDOM INFO|||||||||||||||||||||||||||||||||||||1231231\rIN1|1|MEDICAL|MEDICAL|MEDICAL|USA||123-123-123||MEDICAL|||||12312312||TEST^PERSON|Self|237892347|USA|||||||||||||||||U12312345\rORC|XO|123123|||CM||^^^789023234676789345123|||||PROVIDER|ACTT1||||ACT\rOBR||123123||CT CHEST|RO||2057895757890||||||Cherst area|||DOCTOR|^^^^^^^^Phone~^^^^^^^^Page~^^^^^^^^Mobile Phone~^^^^^^^^Fax Number||SAMPLE DATA|CLINIC|CT        |||CT^ACTT1^ACTT1|||^^15^2057895757890|||||||||2057895757890||||||||72131"""
message = parse_message(hl7_payload, find_groups=False)
for segment in message.children:
    if isinstance(segment, Segment):
        for attribute in segment.children:
            print(attribute, attribute.value)
    if isinstance(segment, Group):
        for group in segment.children:
            for group_segment in group.children:
                for attribute in group_segment.children:
                    print(attribute, attribute.value)

The issue is the output I get when I print at the end looks something like this:

// additional output
<Field OBR_4 (UNIVERSAL_SERVICE_IDENTIFIER) of type CE> CT CHEST
<Field OBR_5 (PRIORITY) of type ID> RO
<Field OBR_7 (OBSERVATION_DATE_TIME) of type TS> 2057895757890
<Field OBR_13 (RELEVANT_CLINICAL_INFORMATION) of type ST> Cherst area
<Field OBR_16 (ORDERING_PROVIDER) of type XCN> DOCTOR
<Field OBR_17 (ORDER_CALLBACK_PHONE_NUMBER) of type XTN> ^^^^^^^^Phone
<Field OBR_17 (ORDER_CALLBACK_PHONE_NUMBER) of type XTN> ^^^^^^^^Page
<Field OBR_17 (ORDER_CALLBACK_PHONE_NUMBER) of type XTN> ^^^^^^^^Mobile Phone
<Field OBR_17 (ORDER_CALLBACK_PHONE_NUMBER) of type XTN> ^^^^^^^^Fax Number
<Field OBR_19 (PLACER_FIELD_2) of type ST> SAMPLE DATA
<Field OBR_20 (FILLER_FIELD_1) of type ST> CLINIC
<Field OBR_21 (FILLER_FIELD_2) of type ST> CT        
<Field OBR_24 (DIAGNOSTIC_SERVICE_SECTION_ID) of type None> CT^ACTT1^ACTT1
<Field OBR_27 (QUANTITY_TIMING) of type TQ> ^^15^2057895757890
<Field OBR_36 (SCHEDULED_DATE_TIME) of type TS> 2057895757890
<Field of type None> 72131

OBR 44 keeps getting parsed in the parse_message function as a Field of type None instead of OBR-44. This is confusing because when I run the following code:

message = parse_segment("OBR||123123||CT CHEST|RO||2057895757890||||||Cherst area|||DOCTOR|^^^^^^^^Phone~^^^^^^^^Page~^^^^^^^^Mobile Phone~^^^^^^^^Fax Number||SAMPLE DATA|CLINIC|CT        |||CT^ACTT1^ACTT1|||^^15^2057895757890|||||||||2057895757890||||||||72131")
print(message.PROCEDURE_CODE.value)

It seems to parse and print the procedure code properly. Not sure if this is a bug or am I doing somethign wrong?

germiBest commented 2 months ago

@Blackglade

Based on the HL7 v2.3 specification, the OBR segment has a maximum of 43 fields. The field "OBR-44 Procedure Code (CE)" that you're trying to parse does not exist in version 2.3. It was introduced in version 2.3.1 and is available in later versions.

This is why the parse_message function is returning "Field of type None" for OBR-44. The behavior you're observing is consistent with the HL7 v2.3 standard and is not a defect in the parser.

If you need to parse messages with OBR-44, you should consider using a message or version HL7 v2.3.1 or later.

For more information, you can refer to the HL7 v2.3 specification here: HL7 v2.3 Specification.

svituz commented 2 months ago

@Blackglade I'm closing the issue since @germiBest (thanks :) ) already answered