Turn Medical Notes into HL7 FHIR with GPT-4o

Résumé

Pixel shares a tutorial on converting unstructured medical notes into HL7 FHIR resources with GPT-4o, few-shot prompting, and Pydantic validation. It outlines a pipeline with JSON Schema checks and retry or human review for invalid results, helping healthcare data engineers improve interoperability and data quality.

DEV Community

From Scribbles to Standards: Converting Unstructured Medical Notes to HL7 FHIR with GPT-4o

Let’s be honest: doctors are famous for two things—saving lives and having handwriting that looks like a caffeinated spider crawled across a page. For Data Engineers in the healthcare space, this "unstructured data" is a nightmare. Traditional OCR and Regex-based parsers often fail because medical notes aren't just messy; they are context-heavy. To achieve true healthcare interoperability, we need to transform these scribbles into the HL7 FHIR (Fast Healthcare Interoperability Resources) standard. In this tutorial, we are diving into the "Dark Arts" of medical data cleaning. We'll leverage GPT-4o, Few-shot prompting, and JSON Schema to build a robust pipeline that turns raw clinical text into valid FHIR resources. The Architecture of a Medical Data Pipeline Before we write any code, we need a solid strategy. We aren't just "asking" the AI to format text; we are building a validation engine. graph TD
A[Raw Unstructured Medical Note] --> B{GPT-4o Engine}
B -->|Few-Shot Context| C[Extraction Logic]
C --> D[JSON Schema Validation]
D -->|Valid| E[FHIR Patient/Observation Resource]
D -->|Invalid| F[Retry/Human-in-the-loop]
E --> G[(Clinical Data Repository)]

style B fill:#f9f,stroke:#333,stroke-width:2px
style E fill:#00ff00,stroke:#333

Prerequisites To follow along, you'll need: Python 3.10+ OpenAI API Key (using GPT-4o for best reasoning) Tech Stack: openai, pydantic (for schema validation), and a basic understanding of HL7 FHIR structures. Step 1: Defining the Target (The FHIR Schema) FHIR is strict. If your JSON doesn't match the resource definition exactly, the receiving server will reject it. We’ll use Pydantic to define a simplified FHIR Observation resource to ensure our LLM output is production-ready. from pydantic import BaseModel, Field
from typing import List, Optional

class FHIRCoding(BaseModel):
system: str
code: str
display: str

class FHIRQuantity(BaseModel):
value: float
unit: str
system: str = "http://unitsofmeasure.org"
code: str

class FHIRObservation(BaseModel):
resourceType: str = "Observation"
status: str
category: List[dict]
code: dict # Should contain a LOINC code
subject: dict # Reference to Patient
valueQuantity: Optional[FHIRQuantity] = None
note: Optional[List[dict]] = None
Step 2: The Prompt Engineering Magic 🪄 The secret to converting "Dark Data" is Few-shot prompting. By providing the LLM with 2-3 examples of "Messy Input -> Valid FHIR," we drastically reduce hallucinations. SYSTEM_PROMPT = """
You are a specialized Medical Informatics Agent.
Your task is to parse unstructured clinical notes into valid HL7 FHIR R4 JSON.
Focus on extracting Vital Signs into 'Observation' resources.
Use LOINC codes for observations (e.g., 85354-9 for Blood Pressure).
"""

FEW_SHOT_EXAMPLES = """
Input: "Pt seen today. BP 120/80. HR 72. Complaining of mild headache."
Output: {
"resourceType": "Observation",
"status": "final",
"code": { "coding": [{"system": "http://loinc.org", "code": "85354-9", "display": "Blood Pressure"}] },
"component": [
{"code": {"text": "Systolic"}, "valueQuantity": {"value": 120, "unit": "mmHg"}},
{"code": {"text": "Diastolic"}, "valueQuantity": {"value": 80, "unit": "mmHg"}}
]
}
"""
Step 3: Implementation with OpenAI Structured Outputs OpenAI recently released Structured Outputs, which forces the model to adhere strictly to a provided JSON Schema. This is a game-changer for Data Engineering pipelines. import openai
import json

client = openai.OpenAI()

def extract_medical_data(raw_text: str):
response = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"Extract from this note: {raw_text}"}
],
response_format=FHIRObservation, # Enforcing the Pydantic schema!
)

return response.choices[0].message.parsed

Example Us...

Turn Medical Notes into HL7 FHIR with GPT-4o
Powered by Readybot.io

Les dernières actus de Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …