← Back to Cookbook

Parallel Branches

Fan-out: root splits into multiple independent nodes that run concurrently.

ai

Source

/**
 * Demonstrates parallel branches in a graph.
 * Root fans out to multiple independent nodes that run concurrently.
 */

webhook article_submitted {
  label: "Article Submitted"
  schema: @json {
    {
      "type": "object",
      "required": ["title", "body", "author"],
      "properties": {
        "title": { "type": "string" },
        "body": { "type": "string" },
        "author": { "type": "string" }
      }
    }
  }
}

graph process_article {
  label: "Process Article"

  root {
    type: code
    label: "Extract article"
    code: @ts { return context.nodes.root.input }
    outputSchema: @json {
      {
        "type": "object",
        "properties": {
          "title": { "type": "string" },
          "body": { "type": "string" },
          "author": { "type": "string" }
        }
      }
    }
  }

  node extract_keywords {
    type: ai
    label: "Extract keywords"
    kind: object
    model: "google/gemini-2.5-flash"
    prompt: @ts {
      return "Extract 5-10 keywords from this article.\n\nTitle: " + context.nodes.root.output.title + "\n\n" + context.nodes.root.output.body
    }
    schema: @json {
      {
        "type": "object",
        "required": ["keywords"],
        "properties": {
          "keywords": { "type": "array", "items": { "type": "string" } }
        }
      }
    }
  }

  node generate_summary {
    type: ai
    label: "Generate summary"
    kind: text
    model: "google/gemini-2.5-flash"
    prompt: @ts {
      return "Write a 2-sentence summary of this article.\n\nTitle: " + context.nodes.root.output.title + "\n\n" + context.nodes.root.output.body
    }
  }

  node estimate_read_time {
    type: code
    label: "Estimate read time"
    code: @ts {
      const words = context.nodes.root.output.body.split(" ").length
      return { read_time_minutes: Math.ceil(words / 200) }
    }
  }

  node combine {
    type: code
    label: "Combine results"
    code: @ts {
      return {
        title: context.nodes.root.output.title,
        author: context.nodes.root.output.author,
        summary: context.nodes.generate_summary.output,
        keywords: context.nodes.extract_keywords.output.keywords,
        read_time: context.nodes.estimate_read_time.output.read_time_minutes
      }
    }
  }

  flow {
    root -> extract_keywords
    root -> generate_summary
    root -> estimate_read_time
    extract_keywords -> combine
    generate_summary -> combine
    estimate_read_time -> combine
  }
}

trigger on_article {
  webhook:article_submitted -> process_article
  enabled: true
}

Flow

Trigger → graph

Graph nodes