Schedule a demo
Message type

Dispense history response

Returns a query response containing the requested data. The QAK segment reports the query status, QPD echoes the original query parameters, and the payload segments carry the result data. Query-response workflows consume this.

45segments
18required
6groups
v2.9HL7 version
rsp-k31.py
# Zato
from zato.hl7v2 import HL7Message

# An incoming message ..
raw = 'MSH|^~\&|SENDER|FAC|RCV|DEST|20260701||' \
      'RSP^K31^RSP_K31|MSG001|P|2.9\r' \
      'QAK|QRY001|OK|Q22\r' \
      'QPD|Q22^FindCandidates|QRY001|SMITH^JOHN'

# .. parsed into a Python object ..
msg = HL7Message.parse(raw)

# .. whose fields we can now access.
query_tag = msg.qak.query_tag
status = msg.qak.query_response_status
query_name = msg.qpd.message_query_name
query_tag = msg.qpd.query_tag

Build and parse RSP^K31 messages in Python

How to construct, send, receive, and extract fields from real-world RSP^K31 messages.

1

Parse an incoming RSP^K31 message

Demonstrates receiving and extracting key fields from a dispense history response transaction

# Zato
from zato.hl7v2 import HL7Message

# An incoming message ..
raw = 'MSH|^~\&|SENDER|FAC|RCV|DEST|20260701||' \
      'RSP^K31^RSP_K31|MSG001|P|2.9\r' \
      'QAK|QRY001|OK|Q22\r' \
      'QPD|Q22^FindCandidates|QRY001|SMITH^JOHN'

# .. parsed into a Python object ..
msg = HL7Message.parse(raw)

# .. whose fields we can now access.
query_tag = msg.qak.query_tag
status = msg.qak.query_response_status
query_name = msg.qpd.message_query_name
query_tag = msg.qpd.query_tag

Learn by building

Step-by-step guides for working with HL7 v2 in Zato.

Frequently asked questions

Use the HL7v2 message parser to extract the typed structure. The parsed message gives you direct access to every segment and field by name.

from zato.hl7v2 import HL7Message

raw = 'MSH|^~\&|SENDER|FACILITY|RECEIVER|DEST|20260401120000||RSP^K31^RSP_K31|MSG00001|P|2.9\rMSA|AA|MSG00001\rERR|||0|I|\rQAK|QRY-2001|OK\rQPD|QRY-1001|QRY-2001|\rRCP|I|10^RD\rDSC|CONT-001'
msg = HL7Message.parse(raw)

query_tag = msg.qak.query_tag
status = msg.qak.query_response_status

Beyond the mandatory MSH header, the required segments are: MSA, QAK, QPD, RCP, PID, PV1, ORC, TQ1. Optional segments provide additional detail when available.

Zato connects to any system that speaks HL7v2 over MLLP or FHIR over REST. This includes Epic, Cerner, Meditech, Allscripts, and other EHR platforms.

Yes, Zato has built-in MLLP support for sending and receiving HL7v2 messages. MLLP channels handle the framing protocol automatically.

Zato supports HL7 v2.9, which is backward compatible with earlier versions including v2.3, v2.5, and v2.7. Standard segments and datatypes are available as typed Python classes.

Yes. Zato handles both HL7v2 and FHIR natively, so you can parse v2 messages and build FHIR resources with IG-specific extensions in the same service. Typed Python classes are available for both protocols, including extensions from US Core, UK Core, Da Vinci, and other Implementation Guides.

RSP^K31 segment composition

Segments that make up the RSP^K31 message, in order. Groups are shown with their child segments.

#SegmentUsageRepeatsDescriptionGroup
1MSHRequiredNoMessage header-
2ARVOptionalYesAccess restriction-
3MSARequiredNoMessage acknowledgment-
4ERROptionalYesError-
5SFTOptionalYesSoftware segment-
6UACOptionalNoUser authentication credential-
7QAKRequiredNoQuery acknowledgment-
8QPDRequiredNoQuery parameter definition-
9RCPRequiredNoResponse control parameter-
10PIDRequiredNoPatient identificationRESPONSE
11PD1OptionalNoPatient additional demographicRESPONSE
12PRTOptionalYesParticipation informationRESPONSE
13NTEOptionalYesNotes and commentsRESPONSE
14ARVOptionalYesAccess restrictionRESPONSE
15AL1OptionalYesPatient allergy informationRESPONSE
16PV1RequiredNoPatient visitPATIENT_VISIT
17PV2OptionalNoPatient visit - additional infoPATIENT_VISIT
18PRTOptionalYesParticipation informationPATIENT_VISIT
19ARVOptionalYesAccess restrictionPATIENT_VISIT
20ORCRequiredNoCommon orderRESPONSE
21PRTOptionalYesParticipation informationRESPONSE
22TQ1RequiredNoTiming / quantityTIMING
23TQ2OptionalYesTiming / quantity relationshipTIMING
24RXORequiredNoPharmacy / treatment orderORDER_DETAIL
25PRTOptionalYesParticipation informationORDER_DETAIL
26NTEOptionalYesNotes and commentsORDER_DETAIL
27RXRRequiredYesPharmacy / treatment routeORDER_DETAIL
28RXCRequiredNoPharmacy / treatment component orderORDER_DETAIL
29NTEOptionalYesNotes and commentsORDER_DETAIL
30RXERequiredNoPharmacy / treatment encoded orderENCODING
31PRTOptionalYesParticipation informationENCODING
32NTEOptionalYesNotes and commentsENCODING
33TQ1RequiredNoTiming / quantityENCODING
34TQ2OptionalYesTiming / quantity relationshipENCODING
35RXRRequiredYesPharmacy / treatment routeENCODING
36RXCOptionalYesPharmacy / treatment component orderENCODING
37RXDRequiredNoPharmacy / treatment dispenseRESPONSE
38PRTOptionalYesParticipation informationRESPONSE
39RXRRequiredYesPharmacy / treatment routeRESPONSE
40RXCOptionalYesPharmacy / treatment component orderRESPONSE
41CDOOptionalYesClinical study data objectRESPONSE
42OBXRequiredNoObservation resultOBSERVATION
43PRTOptionalYesParticipation informationOBSERVATION
44NTEOptionalYesNotes and commentsOBSERVATION
45DSCOptionalNoContinuation pointer-

Ready to build integrations?

Get started with Zato and connect your systems in minutes.

Open source In Python