Skip to main content

Overview

Template Generation is an AI-powered feature that helps you create and improve clinical note templates without manual authoring. Instead of building templates from scratch, you can describe what you need in natural language or provide an existing template for refinement. The feature operates in two modes:
ModePurpose
GenerationCreate a new template from a natural language description
RefinementImprove an existing template based on feedback
This is particularly useful when you need custom note formats for specific specialties or workflows but do not want to manually define every section, prompt, and property.
Alpha Feature: Template generation is currently in alpha. The API may change without notice and should not be used in production environments without careful consideration. See the Alpha API Authentication guide for access details.

Authentication

The Alpha API uses Bearer token authentication:
Authorization: Bearer YOUR_API_KEY
This differs from the stable API which uses the X-API-Key header. Use the same API key you obtained from your Sully.ai dashboard.

Generation Mode

Generation mode creates a template from a natural language description. Describe the template structure, sections, and formatting preferences, and the AI will produce a valid NoteTemplate object.

Endpoint

POST /alpha/note-templates

Request Body

FieldTypeRequiredDescription
modestringYesSet to "generation"
descriptionstringYesNatural language description of the template you want

Example Request

const response = await fetch('https://api.sully.ai/alpha/note-templates', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.SULLY_API_KEY}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    mode: 'generation',
    description: 'Create a template for a cardiology consultation with sections for cardiac history, current symptoms, physical exam findings, EKG interpretation, and recommendations'
  })
});

const { data } = await response.json();
console.log('Job ID:', data.id);
console.log('Status:', data.status);

Response

The endpoint returns a job object with a pending status:
{
  "data": {
    "id": "template_abc123def456",
    "mode": "generation",
    "status": "pending",
    "created_at": "2024-01-15T10:30:00Z",
    "updated_at": "2024-01-15T10:30:00Z"
  }
}

Refinement Mode

Refinement mode improves an existing template based on your feedback. Provide the current template and describe what changes you want, and the AI will produce an updated version.

Endpoint

POST /alpha/note-templates

Request Body

FieldTypeRequiredDescription
modestringYesSet to "refinement"
templateobjectYesThe existing NoteTemplate to refine
feedbackstringYesDescription of improvements you want

Example Request

const existingTemplate = {
  id: 'cardiology-consult-v1',
  title: 'Cardiology Consultation',
  global_prompt: 'Generate a cardiology consultation note.',
  sections: [
    {
      id: 'cardiac-history',
      type: 'heading',
      properties: { level: 1, text: 'Cardiac History' },
      children: [
        {
          id: 'history-text',
          type: 'text',
          prompt: 'Document cardiac history.',
          properties: { detail_level: 'standard', tone: 'formal' }
        }
      ]
    },
    {
      id: 'assessment',
      type: 'heading',
      properties: { level: 1, text: 'Assessment' },
      children: [
        {
          id: 'assessment-text',
          type: 'text',
          prompt: 'Provide clinical assessment.',
          properties: { detail_level: 'standard', tone: 'formal' }
        }
      ]
    }
  ]
};

const response = await fetch('https://api.sully.ai/alpha/note-templates', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.SULLY_API_KEY}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    mode: 'refinement',
    template: existingTemplate,
    feedback: 'Make the assessment section more detailed and add a section for follow-up instructions'
  })
});

const { data } = await response.json();
console.log('Job ID:', data.id);

Polling for Results

Template generation is asynchronous. After creating a job, poll the GET endpoint to check status and retrieve results.

Endpoint

GET /alpha/note-templates/{id}

Status Flow

pending → processing → completed | failed
StatusDescription
pendingJob is queued for processing
processingCurrently generating or refining the template
completedSuccess - result.template contains the generated NoteTemplate
failedError occurred - result.error contains the error message

Polling Pattern

async function waitForTemplate(jobId: string): Promise<any> {
  const maxAttempts = 30;
  const pollInterval = 2000; // 2 seconds

  for (let attempt = 0; attempt < maxAttempts; attempt++) {
    const response = await fetch(
      `https://api.sully.ai/alpha/note-templates/${jobId}`,
      {
        headers: { 'Authorization': `Bearer ${process.env.SULLY_API_KEY}` }
      }
    );

    const { data } = await response.json();

    if (data.status === 'completed') {
      return data.result.template;
    }

    if (data.status === 'failed') {
      throw new Error(`Template generation failed: ${data.result?.error || 'Unknown error'}`);
    }

    // Wait before next poll
    await new Promise(resolve => setTimeout(resolve, pollInterval));
  }

  throw new Error('Template generation timed out');
}

// Usage
const template = await waitForTemplate('template_abc123def456');
console.log('Generated template:', template);

Understanding the Output

When a job completes successfully, result.template contains a valid NoteTemplate object that can be used directly with the POST /v1/notes endpoint.

Template Structure

FieldTypeDescription
idstringUnique identifier for the template
titlestringDisplay title
global_promptstringInstructions that apply to all sections
sectionsarrayOrdered list of HeadingSection, TextSection, or ListSection objects

Example Output

{
  "data": {
    "id": "template_abc123def456",
    "mode": "generation",
    "status": "completed",
    "created_at": "2024-01-15T10:30:00Z",
    "updated_at": "2024-01-15T10:32:00Z",
    "result": {
      "template": {
        "id": "cardiology-consultation-generated",
        "title": "Cardiology Consultation Template",
        "global_prompt": "Generate a comprehensive cardiology consultation note with emphasis on cardiovascular assessment and risk stratification.",
        "sections": [
          {
            "id": "cardiac-history",
            "type": "heading",
            "properties": {
              "level": 1,
              "text": "Cardiac History",
              "bold": true
            },
            "children": [
              {
                "id": "cardiac-history-text",
                "type": "text",
                "prompt": "Document relevant cardiac history including prior diagnoses, procedures, and interventions.",
                "properties": {
                  "detail_level": "detailed",
                  "tone": "technical",
                  "formatting_style": "markdown"
                }
              }
            ]
          },
          {
            "id": "current-symptoms",
            "type": "heading",
            "properties": {
              "level": 1,
              "text": "Current Symptoms",
              "bold": true
            },
            "children": [
              {
                "id": "symptoms-text",
                "type": "text",
                "prompt": "Describe presenting cardiac symptoms including chest pain characteristics, dyspnea, palpitations, syncope, and edema.",
                "properties": {
                  "detail_level": "exhaustive",
                  "tone": "technical"
                }
              }
            ]
          },
          {
            "id": "physical-exam",
            "type": "heading",
            "properties": {
              "level": 1,
              "text": "Physical Examination",
              "bold": true
            },
            "children": [
              {
                "id": "vitals",
                "type": "text",
                "prompt": "Document vital signs including blood pressure (both arms if applicable), heart rate, and rhythm.",
                "properties": {
                  "detail_level": "standard",
                  "tone": "technical",
                  "label": "Vitals:",
                  "bold_label": true
                }
              },
              {
                "id": "cardiac-exam",
                "type": "text",
                "prompt": "Document cardiac examination findings: JVP, carotid examination, PMI, heart sounds, murmurs, and gallops.",
                "properties": {
                  "detail_level": "detailed",
                  "tone": "technical"
                }
              }
            ]
          },
          {
            "id": "ekg-interpretation",
            "type": "heading",
            "properties": {
              "level": 1,
              "text": "EKG Interpretation",
              "bold": true
            },
            "children": [
              {
                "id": "ekg-findings",
                "type": "text",
                "prompt": "Interpret EKG findings including rate, rhythm, intervals, axis, and any abnormalities.",
                "properties": {
                  "detail_level": "detailed",
                  "tone": "technical",
                  "hideIfEmpty": true
                }
              }
            ]
          },
          {
            "id": "recommendations",
            "type": "heading",
            "properties": {
              "level": 1,
              "text": "Recommendations",
              "bold": true
            },
            "children": [
              {
                "id": "recommendations-list",
                "type": "list",
                "prompt": "List specific recommendations including further testing, medications, lifestyle modifications, and follow-up plan.",
                "properties": {
                  "list_type": "numeric",
                  "detail_level": "detailed",
                  "tone": "instructional"
                }
              }
            ]
          }
        ]
      }
    }
  }
}

Using the Generated Template

Once you have the template, use it with the notes endpoint:
const noteResponse = await sully.notes.create({
  transcript: transcription,
  noteType: {
    type: 'note_template',
    template: generatedTemplate
  }
});

Iteration Workflow

The recommended workflow for building production-ready templates:
┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  Generate   │────▶│    Test     │────▶│   Refine    │
│  (describe) │     │ (with real  │     │ (feedback)  │
└─────────────┘     │  transcripts)│     └──────┬──────┘
                    └─────────────┘            │
                           ▲                   │
                           │                   │
                           └───────────────────┘


                            ┌─────────────┐
                            │    Done     │
                            │ (production)│
                            └─────────────┘

Step-by-Step

  1. Generate: Start with a description of the template you need
  2. Test: Use the generated template with actual transcriptions
  3. Review: Evaluate the note output for missing sections or formatting issues
  4. Refine: Use refinement mode with specific feedback
  5. Repeat: Continue testing and refining until satisfied
  6. Deploy: Use the final template in production

Example Iteration

// Step 1: Initial generation
const genResponse = await createTemplateJob({
  mode: 'generation',
  description: 'Orthopedic consultation template with injury history, imaging review, and surgical recommendations'
});
const initialTemplate = await waitForTemplate(genResponse.data.id);

// Step 2: Test with real data
const testNote = await sully.notes.create({
  transcript: sampleTranscript,
  noteType: { type: 'note_template', template: initialTemplate }
});

// Step 3: Identify issues (e.g., missing physical exam detail)
// Step 4: Refine
const refineResponse = await createTemplateJob({
  mode: 'refinement',
  template: initialTemplate,
  feedback: 'Add a detailed physical examination section with range of motion, strength testing, and special tests. Make the surgical recommendations section use a numbered list format.'
});
const refinedTemplate = await waitForTemplate(refineResponse.data.id);

// Step 5: Test again and repeat if needed

Best Practices

Writing Effective Descriptions (Generation Mode)

DoDo Not
Be specific about sectionsUse vague terms like “good template”
Name sections explicitlyAssume AI knows your preferences
Mention detail levelsLeave formatting to chance
Specify formatting preferencesSkip specialty context
Good description:
“Create a dermatology consultation template with sections for: skin lesion description (using ABCDE criteria), location and distribution, relevant history, differential diagnosis with at least 3 possibilities, biopsy recommendations, and follow-up plan. Use bullet points for differentials and numbered items for the plan.”
Poor description:
“Make a skin doctor template”

Writing Effective Feedback (Refinement Mode)

DoDo Not
Reference specific sectionsGive vague feedback
Describe exact changesSay “make it better”
Specify formatting changesAssume context is understood
Explain the reasoningProvide contradictory instructions
Good feedback:
“Change the Assessment section prompt to include severity scoring on a 1-10 scale. Add a ‘Red Flags’ subsection under History that specifically asks about warning symptoms. Convert the Plan section from paragraph format to a numbered list with categories: Diagnostics, Medications, Referrals, Follow-up.”
Poor feedback:
“Make the assessment better and add more sections”

General Guidelines

  1. Review before production: Always test generated templates with real transcriptions before deploying
  2. Iterate incrementally: Make small, focused refinements rather than large changes
  3. Document your templates: Keep notes on what each template is designed for
  4. Version control: Track template versions to revert if needed
  5. Combine with manual edits: You can manually adjust the generated template JSON before use