parsing-hl7v2-messages
できること
このスキルは、病院が検査結果や患者のイベントに使う、パイプ区切りの形式であるHL7 v2メッセージの読み方をエージェントに教えます。メッセージの中の自由記述のノートを取り出し、確認できるようにします。
使う場面
- ✓インターフェースのフィードで検査や入院などの病院のメッセージを受け取り、その中のノートの本文が必要なとき。
- ✓検査レポートのメッセージに含まれる結果のコメントを、読めるテキストにしたいとき。
- ✓MDMメッセージの書き起こしされたレポートを、そのコードと切り離したいとき。
スキルのリンク
https://github.com/maziyarpanahi/openmed/blob/master/skills/parsing-hl7v2-messages/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“病院のフィードから届いたHL7メッセージから、検査のコメントとノートを取り出してもらえますか?”
毎週5つの新しいスキル。1分で読めます。
仕組み
エージェントは各メッセージをセグメントとフィールドに分け、OBXとNTEというノートのフィールドを読みます。そこに含まれる平文を返します。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Parsing HL7 v2 Messages for OpenMed
HL7 v2.x is the workhorse of hospital interfacing — ADT (admit/discharge/ transfer), ORU (observation results), MDM (document management), and ORM (orders) messages flow continuously between EHR, lab, radiology, and ancillary systems. The clinical narrative you want for NLP is buried in OBX-5 (observation value) and NTE-3 (notes/comments) fields, wrapped in a pipe-and-caret encoding. This skill decodes that envelope and hands the free text to OpenMed.
When to use
- You receive HL7 v2 messages from an interface engine (Mirth/NextGen Connect, Rhapsody, Cloverleaf) and want to mine embedded note/result text.
- A lab feed (ORU^R01) carries impression/comment narrative in OBX/NTE.
- An MDM^T02 transcription message carries a full report in OBX-5.
- You need a de-identified, structured feed into
openmed.analyze_text.
HL7 v2 structure in one minute
A message is segments separated by \r (carriage return). Each segment is
3-letter-named, then fields split by |, components by ^,
repetitions by ~, sub-components by &, with \ as escape. The
encoding characters are declared in MSH-1 (the field separator) and
MSH-2 (^~\&). Field positions are one-based, and MSH is special:
MSH-1 is the separator, so MSH-2 is the first real field.
MSH|^~\&|LAB|HOSP|EHR|HOSP|20240302101500||ORU^R01|MSG0001|P|2.5
PID|1||MRN12345^^^HOSP^MR||DOE^JANE^Q||19700115|F|||1 FAKE ST^^SPRINGFIELD^IL^62704
OBR|1||ORD9|CBC^Complete Blood Count
OBX|1|TX|IMPRESSION||Mild leukocytosis; clinically correlate.||||||F
NTE|1||Patient reports fatigue x1 week. Dr. Smith notified.
Quick start
Parse the envelope and pull narrative from OBX-5 / NTE-3, then hand off:
import openmed
from openmed.interop.hl7v2 import parse_hl7v2
raw = open("results.hl7", encoding="utf-8").read()
msg = parse_hl7v2(raw) # -> HL7Message (segments preserved)
narrative_chunks = []
for seg in msg.segments:
if seg.name == "OBX":
# OBX-2 is the value type; OBX-5 is the observation value.
value_type = seg.get_field(2)
if value_type in {"TX", "FT", "CE", "ST"}:
narrative_chunks.append(seg.get_field(5) or "")
elif seg.name == "NTE":
narrative_chunks.append(seg.get_field(3) or "")
# Decode component delimiters into plain text before NLP.
flat = "\n".join(c.replace("^", " ").replace("&", " ") for c in narrative_chunks if c)
# Hand the narrative to OpenMed.
deid = openmed.deidentify(flat, method="replace", policy="hipaa_safe_harbor")
result = openmed.analyze_text(deid.text, output_format="dict")
HL7Segment.get_field(position) uses one-based HL7 positions and returns
None for absent fields. HL7Message.segment_names() lists segments in order.
Whole-message segment-aware de-identification
When you need to redact the entire message (structured PID/NK1/GT1 fields and OBX/NTE free text) while preserving HL7 framing, use the bundled redactor instead of hand-rolling it:
from openmed.interop.hl7v2 import redact_hl7v2
safe = redact_hl7v2("results.hl7") # path or message text
# PID-3 hashed, PID-5 name surrogated, PID-7 DOB date-shifted, OBX-5/NTE-3
# free text masked via openmed.deidentify — delimiters and segment order kept.
redact_hl7v2 applies DEFAULT_FIELD_MAP (PID, PD1, NK1, GT1, IN1/IN2, OBX,
NTE). Extend or override it with field_map={("ZPS", 4): {"action": "hash"}}
for site-specific Z-segments, and pass date_shift_days= for a fixed,
interval-preserving shift.
Workflow
- Frame-split safely. Real feeds use
\r,\r\n, or MLLP framing (\x0b…\x1c\r).parse_hl7v2auto-detects the segment separator; strip MLLP control bytes before parsing. - Read encoding from MSH — never assume
|^~\&. The adapter derives the delimiter set from MSH-1/MSH-2 (HL7V2Encoding.from_msh_segment). - Locate narrative. OBX-5 (gated by OBX-2 value type), NTE-3, and
report-bearing segments. Concatenate repetitions (
~) and components (^). - De-identify, then analyze with OpenMed.
- Rejoin results to the patient/encounter via PID-3 (patient id) and PV1-19 (visit number) — but redact those identifiers in anything you persist.
Hand-off to / from OpenMed
- To OpenMed: flattened OBX-5/NTE-3 text →
openmed.deidentify→openmed.analyze_text. - Adapter:
openmed.interop.hl7v2providesparse_hl7v2,redact_hl7v2, `HL7
よくある質問
HL7 v2とは何ですか?
病院のシステムが、検査結果、入院、オーダーをやり取りするのに使う、一般的なメッセージの形式です。
フィールドのコードを知る必要がありますか?
いいえ。エージェントは構成を知っていて、テキストのフィールドを平易な言葉で報告します。
どのメッセージの種類に対応していますか?
ADT、ORU、MDM、ORMなどの一般的な種類に対応しています。
詳細
作者による元の説明
Decodes pipe-delimited HL7 v2.x messages (ADT, ORU, MDM, ORM) into structured segments/fields/components and surfaces OBX-5 and NTE-3 free-text narrative for OpenMed. Use before OpenMed processing when ingesting HL7 v2 feeds from an interface engine, lab/results system, or ADT stream and you need the embedded clinical note text de-identified and analyzed. Flatten OBX/NTE text then call openmed.deidentify and openmed.analyze_text; segment-aware redaction is available via openmed.interop.hl7v2. Trigger keywords: HL7, HL7 v2, ADT, ORU, OBX, MSH, PID, pipe-delimited, interface engine, Mirth, lab r
repo: maziyarpanahi/openmed
人気
- GitHubのスター
- 5.8k
- ランキング
- #14716
- ライセンス
- Apache-2.0
- 更新日
- 2026/11/2
コメント
- …
このリポジトリの他のスキル
annotating-variants
5.8kエージェントが、無料の公開アノテーターを使って、遺伝子バリアントの作用を予測し、HGVS名をゲノム上の位置に変換し、集団の頻度を付与できるよう支援します。
ask-openmed
5.8kエージェントが、臨床テキストの依頼に合ったOpenMedのワークフローを選べるよう支援します。患者データが含まれる可能性がある場合は、必ずプライバシーの確認から始めます。
assembling-fhir-bundles
5.8kエージェントが、複数のFHIR健康記録を、内部の参照を正しくつないだ一つのトランザクションBundleにまとめ、EHRに送れる状態にします。
auditing-deid-leakage
5.8kエージェントが、匿名化済みの臨床テキストに残っている識別子(SSN、カード番号、メール、日付など)をスキャンし、残っていれば公開を止めます。
似ているスキル
gitcrawl
394k保存されたGitHubのイシューとプルリクエストのアーカイブを検索し、重複をまとめ、GitHub上の現在の状態を確認するようエージェントを支援します。
slacrawl
394kローカルのSlackアーカイブ内のメッセージを検索して件数を数え、アーカイブの新しさを確認し、必要に応じて更新するようエージェントを支援します。
goplaces
394kGoogle Placesを使って、場所を検索し、詳細や口コミを取得し、場所の名前を解決するようエージェントを支援します。スクリプト用にJSON出力もできます。