| name | fhir-generator |
| description | FHIR R4 resource generation and validation for healthcare interoperability. Converts medical data into FHIR-compliant JSON resources (Patient, Observation, MedicationRequest, Condition, etc.). Use when creating FHIR resources, validating healthcare data structures, or preparing data for EHR integration. |
| license | MIT |
| allowed-tools | ["python","bash"] |
| metadata | {"version":"1.0.0","category":"healthcare","fhir-version":"R4","standard":"HL7 FHIR"} |
FHIR R4 Resource Generator Skill
Overview
This skill enables Claude to generate valid FHIR R4 (Fast Healthcare Interoperability Resources) resources from extracted medical data. FHIR is the industry standard for healthcare data exchange.
When to Use This Skill
Use this skill when you need to:
- Convert OCR-extracted medical data to FHIR resources
- Create Patient, Observation, MedicationRequest resources
- Validate FHIR resource structure
- Prepare data for EHR (Electronic Health Record) integration
- Generate HL7 FHIR bundles for data exchange
Core FHIR Resources
1. Patient Resource
def create_fhir_patient(patient_data):
"""Generate FHIR R4 Patient resource"""
return {
"resourceType": "Patient",
"id": patient_data.get('id', 'example-patient'),
"identifier": [{
"use": "official",
"system": "http://hospital.example.org",
"value": patient_data.get('mrn', 'MRN12345')
}],
"active": True,
"name": [{
"use": "official",
"family": patient_data.get('last_name', ''),
"given": [patient_data.get('first_name', '')]
}],
"gender": patient_data.get('gender', 'unknown'),
"birthDate": patient_data.get('birth_date', ''),
"address": [{
"use": "home",
"line": [patient_data.get('address_line', '')],
"city": patient_data.get('city', ''),
"state": patient_data.get('state', ''),
"postalCode": patient_data.get('zip', ''),
"country": patient_data.get('country', 'US')
}],
"telecom": [{
"system": "phone",
"value": patient_data.get('phone', ''),
"use": "mobile"
}]
}
2. MedicationRequest Resource
def create_medication_request(medication_data, patient_reference):
"""Generate FHIR R4 MedicationRequest"""
return {
"resourceType": "MedicationRequest",
"id": medication_data.get('id', 'med-request-1'),
"status": "active",
"intent": "order",
"medicationCodeableConcept": {
"coding": [{
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"code": medication_data.get('rxnorm_code', ''),
"display": medication_data.get('name', '')
}],
"text": medication_data.get('name', '')
},
"subject": {
"reference": patient_reference,
"display": "Patient"
},
"authoredOn": medication_data.get('date', ''),
"dosageInstruction": [{
"text": medication_data.get('sig', ''),
"timing": {
"repeat": {
"frequency": medication_data.get('frequency', 1),
"period": 1,
: ,
: [medication_data.get(, )]
}
},
: {
: [{
: ,
: medication_data.get(, ),
: medication_data.get(, )
}]
},
: [{
: {
: medication_data.get(, ),
: medication_data.get(, ),
: ,
: medication_data.get(, )
}
}]
}],
: {
: {
: medication_data.get(, ),
:
},
: {
: medication_data.get(, ),
:
}
}
}
3. Observation Resource (Lab Results, Vitals)
def create_observation(obs_data, patient_reference):
"""Generate FHIR R4 Observation for lab results or vitals"""
return {
"resourceType": "Observation",
"id": obs_data.get('id', 'obs-1'),
"status": "final",
"category": [{
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/observation-category",
"code": obs_data.get('category', 'laboratory'),
"display": obs_data.get('category_display', 'Laboratory')
}]
}],
"code": {
"coding": [{
"system": "http://loinc.org",
"code": obs_data.get('loinc_code', ''),
"display": obs_data.get('test_name', '')
}],
"text": obs_data.get('test_name', '')
},
"subject": {
"reference": patient_reference
},
"effectiveDateTime": obs_data.get('date', ''),
"valueQuantity": {
"value": obs_data.get('value', 0),
"unit": obs_data.get(, ),
: ,
: obs_data.get(, )
},
: [{
: {
: obs_data.get(, ),
: obs_data.get(, )
},
: {
: obs_data.get(, ),
: obs_data.get(, )
}
}]
}
4. Condition/Diagnosis Resource
def create_condition(diagnosis_data, patient_reference):
"""Generate FHIR R4 Condition resource"""
return {
"resourceType": "Condition",
"id": diagnosis_data.get('id', 'condition-1'),
"clinicalStatus": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/condition-clinical",
"code": diagnosis_data.get('clinical_status', 'active')
}]
},
"verificationStatus": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/condition-ver-status",
"code": "confirmed"
}]
},
"category": [{
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/condition-category",
"code": "encounter-diagnosis",
"display": "Encounter Diagnosis"
}]
}],
"severity": {
"coding": [{
"system": "http://snomed.info/sct",
"code": diagnosis_data.get('severity_code', '24484000'),
"display": diagnosis_data.get('severity', 'Severe')
}]
},
"code": {
: [{
: ,
: diagnosis_data.get(, ),
: diagnosis_data.get(, )
}, {
: ,
: diagnosis_data.get(, ),
: diagnosis_data.get(, )
}],
: diagnosis_data.get(, )
},
: {
: patient_reference
},
: diagnosis_data.get(, ),
: diagnosis_data.get(, )
}
5. DiagnosticReport Resource
def create_diagnostic_report(report_data, patient_reference, observations):
"""Generate FHIR R4 DiagnosticReport"""
return {
"resourceType": "DiagnosticReport",
"id": report_data.get('id', 'report-1'),
"status": "final",
"category": [{
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/v2-0074",
"code": "LAB",
"display": "Laboratory"
}]
}],
"code": {
"coding": [{
"system": "http://loinc.org",
"code": report_data.get('loinc_code', ''),
"display": report_data.get('report_name', '')
}],
"text": report_data.get('report_name', '')
},
"subject": {
"reference": patient_reference
},
"effectiveDateTime": report_data.get('date', ''),
"issued": report_data.get('issued_date', ''),
"result": [
{"reference": f"Observation/{obs_id}"}
obs_id observations
],
: report_data.get(, )
}
FHIR Bundle Creation
def create_fhir_bundle(entries, bundle_type="transaction"):
"""Create FHIR Bundle to group multiple resources"""
return {
"resourceType": "Bundle",
"type": bundle_type,
"entry": [
{
"fullUrl": f"urn:uuid:{entry.get('id', '')}",
"resource": entry,
"request": {
"method": "POST",
"url": entry.get('resourceType', '')
}
}
for entry in entries
]
}
Medical Coding Systems
Common Code Systems
CODE_SYSTEMS = {
"rxnorm": {
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"description": "RxNorm - Medication names"
},
"snomed": {
"system": "http://snomed.info/sct",
"description": "SNOMED CT - Clinical terms"
},
"loinc": {
"system": "http://loinc.org",
"description": "LOINC - Lab tests and observations"
},
"icd10": {
"system": "http://hl7.org/fhir/sid/icd-10-cm",
"description": "ICD-10-CM - Diagnoses"
},
"cpt": {
"system": "http://www.ama-assn.org/go/cpt",
"description": "CPT - Procedures"
},
"ucum": {
"system": "http://unitsofmeasure.org",
"description": "UCUM - Units of measure"
}
}
Common LOINC Codes
COMMON_LOINC_CODES = {
"glucose": "2345-7",
"hba1c": "4548-4",
"cholesterol": "2093-3",
"hdl": "2085-9",
"ldl": "2089-1",
"triglycerides": "2571-8",
"creatinine": "2160-0",
"hemoglobin": "718-7",
"wbc": "6690-2",
"platelets": "777-3",
"bp_systolic": "8480-6",
"bp_diastolic": "8462-4",
"heart_rate": "8867-4",
"temperature": "8310-5",
"weight": "29463-7",
"height":
}
FHIR Validation
def validate_fhir_resource(resource):
"""Basic FHIR resource validation"""
errors = []
if 'resourceType' not in resource:
errors.append("Missing required field: resourceType")
resource_type = resource.get('resourceType')
if resource_type == 'Patient':
if 'name' not in resource or not resource['name']:
errors.append("Patient must have at least one name")
elif resource_type == 'MedicationRequest':
if 'status' not in resource:
errors.append("MedicationRequest must have status")
if resource.get('status') not in ['active', 'on-hold', 'cancelled', 'completed']:
errors.append(f"Invalid status: {resource.get('status')}")
elif resource_type == 'Observation':
if 'status' not in resource:
errors.append("Observation must have status")
resource:
errors.append()
{
: (errors) == ,
: errors
}
Complete Medical Document → FHIR Pipeline
def medical_data_to_fhir_bundle(medical_data):
"""Convert complete medical document to FHIR Bundle"""
patient = create_fhir_patient(medical_data.get('patient', {}))
patient_ref = f"Patient/{patient['id']}"
resources = [patient]
for med in medical_data.get('medications', []):
med_request = create_medication_request(med, patient_ref)
resources.append(med_request)
for obs in medical_data.get('observations', []):
observation = create_observation(obs, patient_ref)
resources.append(observation)
for dx in medical_data.get('diagnoses', []):
condition = create_condition(dx, patient_ref)
resources.append(condition)
bundle = create_fhir_bundle(resources, bundle_type="transaction")
validation_results = [
validate_fhir_resource(res) for res in resources
]
return {
"bundle": bundle,
"validation": validation_results,
"resource_count": len(resources)
}
Example Workflows
Example 1: Prescription → FHIR
prescription_data = {
"patient": {
"id": "pat-001",
"first_name": "John",
"last_name": "Doe",
"mrn": "MRN123456",
"birth_date": "1980-01-15"
},
"medications": [{
"id": "med-001",
"name": "Metformin",
"rxnorm_code": "860975",
"dose_value": 500,
"dose_unit": "mg",
"frequency": 2,
"route": "Oral",
"quantity": 60,
"days_supply": 30,
"sig": "Take 500mg by mouth twice daily with meals"
}]
}
result = medical_data_to_fhir_bundle(prescription_data)
print(f"Created {result['resource_count']} FHIR resources")
Example 2: Lab Results → FHIR
lab_data = {
"patient": {
"id": "pat-001",
"mrn": "MRN123456"
},
"observations": [
{
"id": "obs-glucose",
"test_name": "Glucose",
"loinc_code": "2345-7",
"value": 126,
"unit": "mg/dL",
"ucum_code": "mg/dL",
"ref_low": 70,
"ref_high": 99,
"category": "laboratory",
"date": "2024-11-22T10:30:00Z"
},
{
"id": "obs-hba1c",
"test_name": "Hemoglobin A1c",
"loinc_code": "4548-4",
"value": 7.2,
"unit": "%",
"ucum_code": "%",
"ref_low": 4.0,
"ref_high": 5.6,
"category": "laboratory",
"date": "2024-11-22T10:30:00Z"
}
]
}
result = medical_data_to_fhir_bundle(lab_data)
Best Practices
- Always validate FHIR resources before submission
- Use standard code systems (LOINC, SNOMED, RxNorm, ICD-10)
- Include meaningful text alongside coded values
- Provide reference ranges for observations
- Use appropriate status codes (active, final, entered-in-error, etc.)
- Include timestamps (effectiveDateTime, authoredOn, etc.)
- Link resources properly using references
- Document source of data (OCR extraction, manual entry, etc.)
Integration with Agents
This skill integrates with:
- HEALTHCARELINC: Receives structured medical data
- COMPLIANCELINC: Validates HIPAA compliance
- Medical-OCR: Receives OCR-extracted data
Resources
Version: 1.0.0
Last Updated: 2024-11-22
FHIR Version: R4
Maintainer: Doctors-Linc Development Team