Chapter 8 · Case Study 12

Medical Report Generation

Breaking down technical jargon into patient-friendly consultations using DSPy for structured extraction and generation.

~20 min read

Business Challenge

Medical reports are notoriously difficult for patients to understand. Salomatic needed a way to translate complex doctor notes and lab results into clear, actionable advice.

Multi-Stage Extraction Pipeline

They used DSPy to create a rigorous pipeline that extracts, validates, and interprets medical data.

The Pipeline

Python
class MedicalReportPipeline(dspy.Module):
    def __init__(self):
        self.extract_labs = ChainOfThought("notes, results -> panels")
        self.extract_diagnoses = ChainOfThought("notes, history -> diagnoses")
        self.generate_consultation = ChainOfThought(
            "profile, labs, diagnoses -> consultation"
        )
    
    def forward(self, notes, results, patient):
        panels = self.extract_labs(notes, results)
        diagnoses = self.extract_diagnoses(notes, patient)
        return self.generate_consultation(patient, panels, diagnoses)

Observability with Langtrace

Integration with Langtrace provided deep visibility into where data extraction might be failing, allowing them to pinpoint issues and improve accuracy rapidly.

Results

  • 87.5% Reduction in manual corrections
  • 90% Faster report generation
  • 50x Capacity Increase