Overview
This page documents the complete end-to-end workflow for processing medical cases in MedMitra, from initial file upload through AI analysis to final insights generation.High-Level Architecture
Phase 1: Case Creation & File Upload
Step 1: API Request
A case is created via the/cases endpoint:
Step 2: File Categorization
Files are categorized based on type:backend/agentic.py:37-67
lab: Laboratory reports, blood work, chemical analysesradiology: X-rays, CT scans, MRI images, ultrasounds
Phase 2: Document Processing
Lab Document Processing
Output: Plain text extraction stored inmetadata.text_data
Radiology Document Processing
Output: Structured JSON analysis stored inmetadata.ai_summary
Phase 3: Medical Insights Generation
Step 1: Case Input Preparation
After file processing, the orchestrator prepares input for the Medical Insights Agent:backend/agentic.py:78-109
Step 2: LangGraph Workflow Execution
The Medical Insights Agent processes the case through a LangGraph state machine:backend/agents/medical_ai_agent.py:314-333
LangGraph Workflow Detailed
Complete Graph Structure
Node Execution Order
Process Lab Documents
- Extract lab values from text using LLM
- Generate summaries of findings
- Store structured
LabDocumentobjects
processed_lab_docs, processing_stage = "lab_documents_processed"Process Radiology Documents
- Parse Vision Agent JSON output
- Extract summary text
- Create
RadiologyDocumentobjects
processed_radiology_docs, processing_stage = "radiology_documents_processed"Generate Case Summary
- Synthesize patient info, doctor notes, lab results, and radiology findings
- Identify key findings
- Calculate confidence score
case_summary, processing_stage = "case_summary_generated"Generate SOAP Note
- Create structured clinical note
- Separate Subjective, Objective, Assessment, Plan
- Include confidence assessment
soap_note, processing_stage = "soap_note_generated"Generate Diagnosis
- Determine primary diagnosis
- Assign ICD-10 code
- List supporting evidence
primary_diagnosis, processing_stage = "diagnosis_generated"Compile Insights
- Aggregate all analysis results
- Calculate overall confidence score
- Package into
MedicalInsightsobject
medical_insights, processing_stage = "insights_compiled"State Transitions
TheMedicalAnalysisState evolves through the workflow:
Data Flow Visualization
Confidence Score Calculation
Confidence scores flow through the workflow and are aggregated at the end:Confidence Score Factors
Data Completeness
Data Completeness
- Percentage of expected fields populated
- Availability of supporting documents
- Quality of OCR/extraction results
Model Certainty
Model Certainty
- LLM’s internal confidence signals
- Consistency of outputs across prompts
- Presence of qualifying language
Clinical Consistency
Clinical Consistency
- Agreement between different data sources
- Logical coherence of findings
- Standard diagnostic criteria met
Error Handling Strategy
Node-Level Error Handling
Each node handles errors gracefully:backend/agents/medical_ai_agent.py:288-310
Workflow-Level Error Handling
backend/agentic.py:118-121
Performance Metrics
Typical Processing Times
| Stage | Duration | Notes |
|---|---|---|
| File Upload | 1-3s | Per file, depends on size |
| PDF Extraction | 2-5s | Per document |
| Vision Analysis | 3-6s | Per image |
| Lab Processing | 5-10s | Per document |
| Case Summary | 8-12s | Single LLM call |
| SOAP Note | 6-10s | Single LLM call |
| Diagnosis | 7-11s | Single LLM call |
| Total End-to-End | 30-60s | For typical case |
Optimization Opportunities
Parallel Vision Processing
Process multiple radiology images concurrently using
asyncio.gatherBatch LLM Calls
Combine multiple prompts into single API calls where possible
Caching
Cache common lab value extractions and medical terminology
Streaming
Stream partial results to UI as they become available
Database Schema
Cases Table
Case Files Table
AI Insights Table
Complete Example
Here’s a complete trace of processing a real case:API Response
The final insights are returned via the case retrieval endpoint:Next Steps
Medical Insights Agent
Detailed agent implementation
Vision Agent
Image processing details
API Reference
Integration endpoints
