Skip to content

Implementation Guide for New Language SDKs

This guide provides step-by-step instructions for implementing scribe2fhir SDKs in new programming languages while ensuring output compatibility with the Python reference implementation.

Before implementing a new language SDK:

  1. Review SDK Specification for complete FHIR output requirements
  2. Review Element Requirements for detailed constraints
  3. Set up FHIR R5 library for your target language
  4. Establish JSON serialization capabilities
  5. Implement UUID v4 generation

Create directory structure following repository pattern:

{language}/
├── core/ # Core SDK implementation
├── docs/ # Language-specific documentation
├── tests/ # Comprehensive test suite
├── examples/ # Usage examples
├── {language-config-files} # setup.py, package.json, etc.
└── README.md # Language SDK documentation

Implement foundational classes:

DocumentBuilder Class:

class DocumentBuilder {
// Private collections for each resource type
patient: Patient | null
encounter: Encounter | null
observations: Observation[]
conditions: Condition[]
medicationRequests: MedicationRequest[]
medicationStatements: MedicationStatement[]
serviceRequests: ServiceRequest[]
procedures: Procedure[]
familyMemberHistories: FamilyMemberHistory[]
allergies: AllergyIntolerance[]
immunizations: Immunization[]
appointments: Appointment[]
carePlans: CarePlan[]
communications: Communication[]
constructor(bundleId?: string)
convertToFHIR(): Bundle
}

CodeableConcept Helper:

function createCodeableConcept(
text?: string,
code?: string,
system?: string,
display?: string
): CodeableConcept
function parseCodeInput(input: string | CodeableConcept): CodeableConcept

Implement exact enum values matching Python SDK:

Severity: “mild”, “moderate”, “severe” with SNOMED CT codes Laterality: “left”, “right”, “bilateral” with SNOMED CT codes FindingStatus: “present”, “absent” with SNOMED CT codes Interpretation: “normal”, “high”, “low”, “abnormal” with HL7 codes

Implement patient resource creation:

  • Support name as string or structured object
  • Handle age vs birthDate (mutually exclusive)
  • Process identifier arrays with type mapping
  • Create address and telecom arrays
  • Generate patient age extension when needed

Implement encounter resource creation:

  • Map encounter classes to FHIR codes
  • Create period from start/end times
  • Handle location and service provider references
  • Set default status to “finished”

Implement five observation types with correct categories:

  • Symptom: category “survey”, support components
  • Vital Signs: category “vital-signs”, handle numeric/string values
  • Lab Findings: category “laboratory”, support interpretations
  • Examination: category “exam”, primarily text findings
  • Lifestyle: category “social-history”, status values

Key Requirements:

  • Correct category coding for each type
  • Proper effective time handling (DateTime vs Period)
  • Component support for severity, laterality, finding status
  • Value handling (Quantity vs String)

Implement condition creation:

  • Support history vs encounter diagnosis categories
  • Handle clinical and verification status
  • Store severity in severity field (not extension)
  • Process onset/offset into appropriate time fields
  • Create body site from laterality

Implement two medication types:

  • MedicationRequest: For prescriptions
  • MedicationStatement: For medication history

Dosage Builder Requirements:

  • Create complete Dosage objects with text, timing, route, dose
  • Store timing codes in timing.repeat.when array
  • Support SNOMED CT route codes
  • Handle frequency/period calculations

Implement service ordering:

  • Lab tests with laboratory category
  • Procedures with surgical procedure category
  • Priority handling
  • Occurrence date scheduling

Implement historical information:

  • Family History: Relationship handling, condition arrays
  • Allergy: Category classification, reaction documentation
  • Immunization: Dose tracking, series management
  • Procedure: Past procedure documentation

Implement care planning:

  • Appointment: Participant management, scheduling
  • CarePlan: Activity with progress annotations
  • Communication: Payload with CodeableConcept content

Implement all 20 add_* methods with exact signatures:

addPatient(name, age?, birthDate?, gender?, identifiers?, address?, phone?, email?, id?)
addEncounter(encounterClass?, encounterType?, period_start?, period_end?, facilityName?, department?, status?, id?)
addSymptom(code, onset?, offset?, severity?, status?, notes?, laterality?, findingStatus?, id?)
addMedicalConditionHistory(code, onset?, offset?, clinicalStatus?, verificationStatus?, severity?, laterality?, notes?, id?)
addMedicalConditionEncountered(code, onset?, offset?, clinicalStatus?, verificationStatus?, severity?, laterality?, notes?, id?)
addVitalFinding(code, value?, unit?, date?, interpretation?, notes?, id?)
addLabFinding(code, value?, unit?, date?, interpretation?, notes?, id?)
addExaminationFinding(code, value?, date?, status?, notes?, id?)
addLifestyleHistory(code, statusValue?, notes?, date?, id?)
addMedicationPrescribed(medication, dosage?, status?, intent?, durationValue?, durationUnit?, quantityValue?, quantityUnit?, refills?, notes?, reason?, authoredOn?, id?)
addMedicationHistory(medication, dosage?, status?, effectiveStart?, effectiveEnd?, notes?, reason?, dateAsserted?, id?)
addTestPrescribed(code, date?, notes?, priority?, reason?, id?)
addProcedurePrescribed(code, date?, notes?, priority?, reason?, id?)
addProcedureHistory(code, date?, notes?, status?, outcome?, id?)
addFamilyHistory(condition, relation, onset?, status?, notes?, deceased?, id?)
addAllergyHistory(code, category?, clinicalStatus?, criticality?, reaction?, notes?, id?)
addImmunisationHistory(vaccine, occurrenceDate?, status?, doseNumber?, seriesDoses?, notes?, id?)
addFollowup(date, refDoctor?, refSpecialty?, notes?, id?)
addAdvice(note, category?, id?)
addNotes(note, category?, id?)

Each method MUST:

  • Generate UUID v4 for resource ID
  • Add patient reference if patient exists
  • Add encounter reference if encounter exists and applicable
  • Populate reference display names
  • Store resource in appropriate collection

The convertToFHIR() method MUST:

  1. Create Bundle with proper metadata
  2. Add resources in specified order
  3. Create BundleEntry for each resource
  4. Generate fullUrl with urn:uuid format
  5. Return as JSON/dictionary structure

Create comprehensive tests covering:

  • Each element type with minimal and maximal parameters
  • Code input processing (string vs CodeableConcept)
  • DateTime handling and timezone conversion
  • Reference creation and validation
  • Bundle structure and metadata
  • Edge cases and error conditions

Test complete workflows:

  • Multi-element documents
  • Cross-resource references
  • Bundle serialization
  • Large document performance

Verify output matches Python SDK:

  • Generate identical bundles for identical input
  • Compare JSON output field-by-field
  • Validate against FHIR R5 schemas
  • Test reference integrity

Create language-specific documentation:

  • Installation and setup instructions
  • API reference for all methods
  • Code examples for each element type
  • Best practices and patterns

Adapt element documentation from Python SDK:

  • Update syntax for target language
  • Maintain example consistency
  • Emphasize CodeableConcept usage
  • Include coding system recommendations
  • Strongly typed languages: Create type definitions for all FHIR resources
  • Dynamically typed languages: Provide runtime validation
  • All languages: Support union types for flexible input
  • Use native datetime libraries
  • Ensure timezone handling capability
  • Support ISO 8601 parsing and formatting
  • Handle date-only vs datetime fields appropriately
  • Ensure camelCase field names
  • Exclude null/undefined fields
  • Preserve numeric precision
  • Handle nested object serialization
  • Provide clear error messages
  • Validate input parameters
  • Handle missing required fields gracefully
  • Support partial document creation

Each SDK implementation MUST include tests for:

  • All 20 element types with basic usage
  • All element types with comprehensive parameters
  • Bundle generation and structure
  • Reference creation and validation
  • Error handling and edge cases
  • Integration workflows

Create shared test data and expected outputs:

  • JSON files with input parameters
  • Expected FHIR bundle outputs
  • Automated comparison tools
  • Regression test suites
  • Follow language-specific style guidelines
  • Include comprehensive documentation
  • Use appropriate design patterns
  • Implement proper error handling
  • Validate against FHIR R5 schemas
  • Test with FHIR validation tools
  • Ensure proper resource relationships
  • Verify coding system usage
  • Handle 100+ resources efficiently
  • Bundle generation under 5 seconds
  • Memory usage appropriate for typical documents
  • Support concurrent usage
  • Maintain version parity across languages
  • Coordinate breaking changes
  • Update documentation consistently
  • Test cross-language compatibility
  • Keep element documentation synchronized
  • Update examples across all languages
  • Maintain specification compliance
  • Provide migration guides for updates
  • FHIR field names may differ from intuitive names
  • Some fields are arrays when single values expected
  • Extension usage is limited in scribe2fhir
  • Component structures have specific requirements
  • Patient/encounter references must be auto-generated
  • Display names are required for human readability
  • Reference format must match specification exactly
  • Missing references cause validation failures
  • Each resource type has required categories
  • Status defaults must match specification
  • Coding systems must be exact matches
  • Display values should match code meanings
  • All datetime values must include timezone
  • ISO 8601 format required throughout
  • Period vs DateTime selection based on input
  • String datetime input must be properly parsed

This implementation guide ensures that new language SDKs maintain compatibility and consistency with the scribe2fhir specification while following language-specific best practices.