microsoft/semantic-kernel · error · InvalidOperationException

The maximum indentation level must be at least 1.

Error message

The maximum indentation level must be at least 1.

What it means

Thrown by the ToMermaid extension when the maxLevel argument is less than 1. maxLevel controls how deep nested (sub)processes are expanded in the generated flowchart; 1 means no nesting is rendered, and 0 or negatives are invalid.

Source

Thrown at dotnet/src/Experimental/Process.Core/Tools/ProcessVisualizationExtensions.cs:37

    /// <returns></returns>
    public static string ToMermaid(this ProcessBuilder processBuilder, int maxLevel = 2)
    {
        var process = processBuilder.Build();
        return process.ToMermaid(maxLevel);
    }

    /// <summary>
    /// Generates a Mermaid diagram from a kernel process.
    /// </summary>
    /// <param name="process"></param>
    /// <param name="maxLevel">The maximum indentation level to reach for nested processes, 1 is basically no nesting</param>
    /// <returns></returns>
    public static string ToMermaid(this KernelProcess process, int maxLevel = 2)
    {
        // Check that the maximum level is at least 1
        if (maxLevel < 1)
        {
            throw new InvalidOperationException("The maximum indentation level must be at least 1.");
        }

        StringBuilder sb = new();
        sb.AppendLine("flowchart LR");

        // Generate the Mermaid flowchart content with indentation
        string flowchartContent = RenderProcess(process, 1, isSubProcess: false, maxLevel);

        // Append the formatted content to the main StringBuilder
        sb.Append(flowchartContent);

        return sb.ToString();
    }

    /// <summary>
    /// Renders a process and its nested processes recursively as a Mermaid flowchart.
    /// </summary>
    /// <param name="process">The process to render.</param>

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pass maxLevel >= 1; use 1 to render only the top process with no nested expansion.
  2. Clamp computed values with Math.Max(1, computedLevel) before calling ToMermaid.
  3. Review configuration defaults for the visualization depth to ensure a positive value.

Example fix

// before
var diagram = process.ToMermaid(maxLevel: depth - 1); // depth == 1 -> throws

// after
var diagram = process.ToMermaid(Math.Max(1, depth - 1));
Defensive patterns

Strategy: validation

Validate before calling

int safeLevel = Math.Max(1, requestedLevel);
var diagram = process.ToMermaid(safeLevel);

Type guard

static bool IsValidMaxLevel(int level) => level >= 1;

Try / catch

try { var d = process.ToMermaid(level); }
catch (InvalidOperationException ex) when (ex.Message.Contains("indentation level"))
{ /* default to 1 or 2 and retry */ }

Prevention

When it happens

Trigger: Calling kernelProcess.ToMermaid(maxLevel) with 0, a negative number, or a value computed from a subtraction that underflows to <=0.

Common situations: Computing maxLevel from a depth that can be 0 (e.g. level - 1); passing a config default that was mis-set to 0; defensive code that passes 0 intending 'no limit'.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/7fdddaa7862f1619. Report an issue: GitHub.