stride3d/stride · error · ArgumentException

When constructing a mapping node with a sequence, the…

Error message

When constructing a mapping node with a sequence, the number of elements of the sequence must be even.

What it means

Thrown by YamlMappingNode's sequence constructor when a flat sequence is used to build a mapping: mappings need alternating key/value pairs, so the sequence must contain an even number of elements. The constructor walks the sequence two elements at a time (key, value) and throws when it runs out mid-pair.

Solutions

  1. Check the sequence passed to the constructor and make sure its element count is even
  2. Log or print the sequence before constructing to find the missing key or value
  3. Fix the source YAML/data so each mapping entry has both a key and a value

Example fix

// before
var node = new YamlMappingNode(sequence); // sequence has 3 items
// after
if (sequence.Count % 2 != 0) throw new InvalidOperationException("Mapping sequence must have even item count");
var node = new YamlMappingNode(sequence);
Defensive patterns

Strategy: validation

Validate before calling

if (items == null || items.Count % 2 != 0) throw new InvalidOperationException("Mapping requires an even number of key/value elements");

Type guard

bool IsValidMappingSequence(ICollection items) => items != null && items.Count % 2 == 0;

Try / catch

try { var node = new YamlMappingNode(sequence); } catch (ArgumentException ex) { logger.LogError(ex, "Odd-length sequence for YAML mapping"); }

Prevention

When it happens

Trigger: Calling the YamlMappingNode constructor (or deserializer path that uses it) with a sequence/collection whose element count is odd, e.g. a list of style entries where one entry was dropped or duplicated.

Common situations: Hand-editing YAML asset files so a mapping's key or value line was deleted; constructing mappings programmatically from a flattened key/value array that got out of sync.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/0f1f7c7ca857ef57. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/YamlMappingNode.cs:173

        public YamlMappingNode(params YamlNode[] children)
            : this((IEnumerable<YamlNode>) children)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="YamlMappingNode"/> class.
        /// </summary>
        /// <param name="children">A sequence of <see cref="YamlNode"/> where even elements are keys and odd elements are values.</param>
        public YamlMappingNode(IEnumerable<YamlNode> children)
        {
            using (var enumerator = children.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    var key = enumerator.Current;
                    if (!enumerator.MoveNext())
                    {
                        throw new ArgumentException("When constructing a mapping node with a sequence, the number of elements of the sequence must be even.");
                    }

                    Add(key, enumerator.Current);
                }
            }
        }

        /// <summary>
        /// Adds the specified mapping to the <see cref="Children"/> collection.
        /// </summary>
        /// <param name="key">The key node.</param>
        /// <param name="value">The value node.</param>
        public void Add(YamlNode key, YamlNode value)
        {
            children.Add(key, value);
        }

        /// <summary>

View on GitHub (pinned to 96fad776d2)