crs4 / hl7apy

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

CON Segment is not parsing #134

Open jdabhi-hah opened 2 months ago

jdabhi-hah commented 2 months ago

MSH|^~\&|HOSPITAL|HOSPITAL|APPLICATION|20240810044330||ADT^A01^ADT_A01|1234567890||P|2.3 PID|||1234567890||PATIENT^JOHN^DOE||19700101|M|||123 Main St|||CITY^ST^ZIP||HOME^123-456-7890 PV1|||1|I|SURGERY||SURGEON^DR SMITH||1234567890|| CON|1|CONSENT^TREATMENT^SURGERY|20240810044330||PATIENT CONSENTS TO SURGERY|1234567890||F

Above is the sample message.

I have file which contain CON segment and due to this reason, the file is not parsing. Can we get a solution for this case.

germiBest commented 2 months ago

@jdabhi-hah

The issue you're encountering is due to the HL7 version specified in your message. The CON segment, which you're using for consent information, is not supported in HL7 version 2.3. The minimal supported version that includes the CON segment is HL7 2.5 (or better even 2.6 because this is the first version adding messages regarding consent management).

In your provided message:

MSH|^~&|HOSPITAL|HOSPITAL|APPLICATION|20240810044330||ADT^A01^ADT_A01|1234567890||P|2.3

The version is set to 2.3, which doesn't support the CON segment, and that's why the parser is unable to process it.

To resolve this issue, you would need to update your message to comply with at least HL7 2.5 specification.

Reference: HL7 Version 2.5 Messaging Standard reference for CON header

jdabhi-hah commented 2 months ago

@jdabhi-hah

The issue you're encountering is due to the HL7 version specified in your message. The CON segment, which you're using for consent information, is not supported in HL7 version 2.3. The minimal supported version that includes the CON segment is HL7 2.5 (or better even 2.6 because this is the first version adding messages regarding consent management).

In your provided message:

MSH|^~&|HOSPITAL|HOSPITAL|APPLICATION|20240810044330||ADT^A01^ADT_A01|1234567890||P|2.3

The version is set to 2.3, which doesn't support the CON segment, and that's why the parser is unable to process it.

To resolve this issue, you would need to update your message to comply with at least HL7 2.5 specification.

Reference: HL7 Version 2.5 Messaging Standard reference for CON header @germiBest It makes sense. But I am observing the ADTs from HIE which belongs to 2.3 version and having CON segment in it.

germiBest commented 2 months ago

@jdabhi-hah It seems that the ADT message might not be fully compliant with the HL7 specification. As a workaround, I recommend making a couple of modifications to your string before parsing it with hl7apy.

First, it appears that the MSH.2 field (Encoding Characters) is missing the required fourth symbol. Your current string shows MSH|^~&|, which has only three characters. This can cause issues during parsing, so it’s important to update it to include the correct four characters, like this: MSH|^~\\&.

Second, the version specified in the message (2.3) does not support the CON segment. To resolve this, you could manually change the version to 2.5, which supports the CON segment.

Here’s an example of what the corrected payload might look like:

MSH|^~\\&|HOSPITAL|HOSPITAL|APPLICATION|20240810044330||ADT^A01^ADT_A01|1234567890||P|2.5
PID|||1234567890||PATIENT^JOHN^DOE||19700101|M|||123 Main St|||CITY^ST^ZIP||HOME^123-456-7890
PV1|||1|I|SURGERY||SURGEON^DR SMITH||1234567890||
CON|1|CONSENT^TREATMENT^SURGERY|20240810044330||PATIENT CONSENTS TO SURGERY|1234567890||F

These changes should help ensure that your message is parsed correctly, but cannot be a reliable solution, due to ADT's from HIE is not standardized.

jdabhi-hah commented 2 months ago

@jdabhi-hah It seems that the ADT message might not be fully compliant with the HL7 specification. As a workaround, I recommend making a couple of modifications to your string before parsing it with hl7apy.

First, it appears that the MSH.2 field (Encoding Characters) is missing the required fourth symbol. Your current string shows MSH|^~&|, which has only three characters. This can cause issues during parsing, so it’s important to update it to include the correct four characters, like this: MSH|^~\\&.

Second, the version specified in the message (2.3) does not support the CON segment. To resolve this, you could manually change the version to 2.5, which supports the CON segment.

Here’s an example of what the corrected payload might look like:

MSH|^~\\&|HOSPITAL|HOSPITAL|APPLICATION|20240810044330||ADT^A01^ADT_A01|1234567890||P|2.5
PID|||1234567890||PATIENT^JOHN^DOE||19700101|M|||123 Main St|||CITY^ST^ZIP||HOME^123-456-7890
PV1|||1|I|SURGERY||SURGEON^DR SMITH||1234567890||
CON|1|CONSENT^TREATMENT^SURGERY|20240810044330||PATIENT CONSENTS TO SURGERY|1234567890||F

These changes should help ensure that your message is parsed correctly, but cannot be a reliable solution, due to ADT's from HIE is not standardized.

@germiBest the sample you are seeing is not the original sample ADT msg. It is dummy ADT. But the actual ADT contains correct characters and symbols in MSH.2. and the 2nd approach changing the version of ADT msg manually, it works. But as you said, it is not reliable. I'm too thinking the same.

Actually, even I tried to add CON segment directly to hl7apy library in various file like message, segments and fields. But as soon as I added this new element, it started to give me error like Invalid header Name: MSH

Can you suggest me anything here? So, I can make change to hl7 parser library to my own use case?

germiBest commented 2 months ago

@jdabhi-hah You can try this one: https://github.com/germiBest/hl7apy/tree/con-segment-to-v2.3

It's in branch con-segment-to-v2.3 in my personal fork.

I've just copied the CON segment and all its requirements from v2.5 to v2.3, this workaround can suit you.

jdabhi-hah commented 2 months ago

@jdabhi-hah You can try this one: https://github.com/germiBest/hl7apy/tree/con-segment-to-v2.3

It's in branch con-segment-to-v2.3 in my personal fork.

I've just copied the CON segment and all its requirements from v2.5 to v2.3, this workaround can suit you.

That's great. I'll try it out and let you know. thanks.

svituz commented 1 month ago

@jdabhi-hah this seems a case where message profile should be suitable. What is the ADT from HIE? Do they provide an XML with the structure of the custom message?

jdabhi-hah commented 1 month ago

@jdabhi-hah this seems a case where message profile should be suitable. What is the ADT from HIE? Do they provide an XML with the structure of the custom message?

No @svituz, it is not in XML. It comes in standard hl7v2 format.

svituz commented 1 month ago

@jdabhi-hah I didn't mean that the message is in XML format. I don't know what HIE is but you said they added the CON segment to the ADT message in version 2.3. Usually, these customizations come with a message profile which is a definition of a message structure different than the standard ones. The structures of the message profiles are provided as XML files and hl7apy can parse the XML to use that structure when creating and validating a message, as explained in the documentation I linked. For example, IHE makes extensive use of message profiles. Your use case seems similar so I think it is worth it to check whether they provide an XML with the custom structure instead of using a workaround