Skip to main content

Description

Subgraphs are reusable components that can be referenced in larger workflows. They allow you to define common patterns once and use them multiple times, improving maintainability and reducing duplication.

Creating Subgraphs

Define subgraphs at the same level as the main graph with their own inputs, outputs, and nodes:
# Define a reusable retrieval subgraph
document_retrieval:
  inputs:
    query: str
    threshold: float
  outputs:
    documents: List[str]
    quality_score: float
  nodes:
    search:
      type: SearchUnstructuredDataStep
      input_mapping:
        query: __inputs__#query
    
    quality_check:
      type: QualityCheckStep
      config:
        threshold: 0.7
      input_mapping:
        results: search#retrievals
    
    __outputs__:
      type: output
      input_mapping:
        documents: search#retrievals
        quality_score: quality_check#score

Using Subgraphs

Reference subgraphs using the subgraph: prefix in the type field:
# Main workflow using the subgraph
inputs:
  user_query: str

outputs:
  final_result: str

nodes:
  constant_threshold:
    type: ConstantStep
    config:
      value: 0.8

  retriever:
    type: subgraph:document_retrieval
    input_mapping:
      query: __inputs__#user_query
      threshold: constant_threshold#output

  __outputs__:
    type: output
    input_mapping:
      final_result: retriever#documents

Subgraph Reference Syntax

node_name:
  type: subgraph:subgraph_name
  input_mapping:
    subgraph_input: source_node#output

Features

FeatureDescription
NestingSubgraphs can contain other subgraphs
ReusabilityOne subgraph can be called multiple times in the same workflow
EncapsulationInternal node names don’t conflict with parent graph
Typed I/OSubgraphs define their own typed inputs and outputs

Example: Multiple Subgraph Calls

# Subgraph definition
search_and_rerank:
  inputs:
    query: str
  outputs:
    retrievals: Retrievals
  nodes:
    search:
      type: SearchUnstructuredDataStep
      input_mapping:
        query: __inputs__#query
    rerank:
      type: RerankRetrievalsStep
      input_mapping:
        query: __inputs__#query
        retrievals: search#retrievals
    __outputs__:
      type: output
      input_mapping:
        retrievals: rerank#retrievals

# Main workflow calling the subgraph twice
inputs:
  query: str

outputs:
  response: str

nodes:
  # First call with original query
  primary_search:
    type: subgraph:search_and_rerank
    input_mapping:
      query: __inputs__#query

  # Second call with expanded query
  expanded_search:
    type: subgraph:search_and_rerank
    input_mapping:
      query: query_expansion#expanded_query

  # Merge results
  merge:
    type: MergeRetrievalsStep
    input_mapping:
      retrievals1: primary_search#retrievals
      retrievals2: expanded_search#retrievals