stride3d/stride · error · YamlException

Unable to deserialize array. Current number of elements

Error message

Unable to deserialize array. Current number of elements [{index}] exceeding array size [{arrayList.Count}]

What it means

When deserializing into a fixed-size .NET array, ArraySerializer reads elements until SequenceEnd. If the YAML sequence contains more entries than the target array's length, it cannot grow the array, so it throws a YamlException reporting the element index and the array size.

Solutions

  1. Change the target type to List<T> or another growable collection if the data is variable-length
  2. Make the YAML sequence match the fixed array size
  3. Catch YamlException during deserialization to surface which index/size mismatched

Example fix

// before
public float[] Coefficients = new float[3]; // yaml has 5 values
// after
public List<float> Coefficients; // accepts any number of elements
Defensive patterns

Strategy: validation

Validate before calling

var expected = targetArray.Length;
var actual = ((YamlSequenceNode)node).Children.Count;
if (actual > expected) throw new InvalidDataException($"YAML sequence has {actual} items but array holds {expected}");

Try / catch

try { return serializer.Deserialize(reader, arrayType); } catch (YamlException ex) { throw new InvalidDataException($"Array overflow: {ex.Message} at {ex.Start}", ex); }

Prevention

When it happens

Trigger: Deserializing a YAML sequence with N elements into a Type that is an array of length M < N (arrays have fixed length once constructed).

Common situations: Schema drift: the array size is determined by a fixed attribute or an earlier field, but the data file was extended; hand-edited YAML adding entries to a fixed-size array like a matrix or vector.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/Serializers/ArraySerializer.cs:79

        public virtual object ReadYaml(ref ObjectContext objectContext)
        {
            var reader = objectContext.Reader;
            var arrayDescriptor = (ArrayDescriptor) objectContext.Descriptor;

            bool isArray = objectContext.Instance != null && objectContext.Instance.GetType().IsArray;
            var arrayList = (IList) objectContext.Instance;

            reader.Expect<SequenceStart>();
            int index = 0;
            if (isArray)
            {
                while (!reader.Accept<SequenceEnd>())
                {
                    var node = reader.Peek<ParsingEvent>();
                    if (index >= arrayList.Count)
                    {
                        throw new YamlException(node.Start, node.End, $"Unable to deserialize array. Current number of elements [{index}] exceeding array size [{arrayList.Count}]");
                    }

                    // Handle aliasing
                    arrayList[index++] = ReadYaml(objectContext.SerializerContext, arrayDescriptor.ElementType);
                }
            }
            else
            {
                var results = new List<object>();
                while (!reader.Accept<SequenceEnd>())
                {
                    results.Add(ReadYaml(objectContext.SerializerContext, arrayDescriptor.ElementType));
                }

                // Handle aliasing
                arrayList = arrayDescriptor.CreateArray(results.Count);
                foreach (var arrayItem in results)
                {

View on GitHub (pinned to 96fad776d2)