dotnet/aspnetcore · error · InvalidOperationException

More than one sibling has the same key value, '{key}'. Key v

Error message

More than one sibling has the same key value, '{key}'. Key values must be unique.

What it means

When RenderTreeDiffBuilder encounters duplicate keys among sibling frames that are neither Component nor Element type (falling into the default case of ThrowExceptionForDuplicateKey), it throws a generic InvalidOperationException. This covers edge cases in manual RenderTreeBuilder usage where non-element/non-component frames carry duplicate keys.

Source

Thrown at src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs:386

            newStartIndex = NextSiblingIndex(frame, newStartIndex);
        }

        return result;
    }

    private static void ThrowExceptionForDuplicateKey(object key, in RenderTreeFrame frame)
    {
        switch (frame.FrameTypeField)
        {
            case RenderTreeFrameType.Component:
                throw new InvalidOperationException($"More than one sibling of component '{frame.ComponentTypeField}' has the same key value, '{key}'. Key values must be unique.");

            case RenderTreeFrameType.Element:
                throw new InvalidOperationException($"More than one sibling of element '{frame.ElementNameField}' has the same key value, '{key}'. Key values must be unique.");

            default:
                throw new InvalidOperationException($"More than one sibling has the same key value, '{key}'. Key values must be unique.");
        }
    }

    private static object KeyValue(ref RenderTreeFrame frame)
    {
        switch (frame.FrameTypeField)
        {
            case RenderTreeFrameType.Element:
                return frame.ElementKeyField;
            case RenderTreeFrameType.Component:
                return frame.ComponentKeyField;
            default:
                return null;
        }
    }

    // Handles the diff for attribute nodes only - this invariant is enforced by the caller.
    //

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Review any custom RenderTreeBuilder code for incorrect key assignment on non-element/non-component frames.
  2. Ensure @key is only used on elements and components, which is the normal Razor behavior.
  3. If this occurs in framework code without custom RenderTreeBuilder usage, file a Blazor issue.
  4. Avoid using SetKey outside the normal element/component scope.
Defensive patterns

Strategy: validation

Validate before calling

// For manual RenderTreeBuilder code, validate keys on non-standard frames
static void AssertUniqueKeysInFrames(ArrayBuilder<RenderTreeFrame> entries, int start, int end)
{
    var seen = new HashSet<object>();
    for (int i = start; i < end; i++)
    {
        object? key = entries.Buffer[i].FrameTypeField switch
        {
            RenderTreeFrameType.Element => entries.Buffer[i].ElementKeyField,
            RenderTreeFrameType.Component => entries.Buffer[i].ComponentKeyField,
            _ => null
        };
        if (key != null && !seen.Add(key))
            throw new InvalidOperationException($"Duplicate key: {key}");
    }
}

Prevention

When it happens

Trigger: Using RenderTreeBuilder.SetKey on frames that are not Element or Component (though SetKey itself would throw a different error), or internal framework code that assigns keys to non-standard frame types. This is primarily an internal/edge-case error that most developers will never encounter directly.

Common situations: Custom RenderTreeBuilder code that manipulates frame keys directly; framework-level bugs in rendering; extremely unusual manual tree construction scenarios.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/195f1b62a3a13205. Report an issue: GitHub.