stride3d/stride · error · YamlException

Expected ' ', got ' ' (at line , character ).

Error message

Expected '{0}', got '{1}' (at line {2}, character {3}).

What it means

EventReader.Expect<T>() peeks the parser's next event and throws this YamlException (with start/end marks) when the next event is not of the requested type T. It is the typed reader's way of enforcing a strict event sequence while consuming a YAML stream.

Solutions

  1. Inspect the message's 'got X at line/character' part and fix the YAML or the Expect call to match the actual event type
  2. Use Allow<T>() (returns null) instead of Expect<T>() when the event type is optional
  3. Call reader.Peek()/Allow for a union of acceptable types before narrowing
  4. Validate the input document's structure before parsing it with a strict Expect sequence

Example fix

// before
var scalar = reader.Expect<ScalarEvent>();
// after
var scalar = reader.Allow<ScalarEvent>();
if (scalar == null) {
  var peeked = reader.Peek();
  throw new FormatException($"Unexpected event {peeked?.Type} at {peeked?.Start}");
}
Defensive patterns

Strategy: try-catch

Validate before calling

var peeked = reader.Peek();
if (!(peeked is ScalarEvent)) throw new FormatException($"Expected scalar, got {peeked?.GetType().Name} at {peeked?.Start}");

Try / catch

try { var e = reader.Expect<ScalarEvent>(); }
catch (YamlException ex) { var m = ex.Start; Console.WriteLine($"YAML structure mismatch at line {m.Line}, col {m.Column}: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling reader.Expect<ScalarEvent>() (or Expect<DocumentStartEvent>, etc.) when the parser's current event is a different type; calling Expect after the stream already ended; misjudging document structure (e.g. expecting a mapping where the file has a scalar).

Common situations: Custom parsers walking YAML manually with EventReader; config files whose shape differs from what the reader expects (list instead of map); reading past the last document so the next event is StreamEnd.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/EventReader.cs:93

        /// </summary>
        /// <value>The parser.</value>
        public IParser Parser { get { return parser; } }

        public int CurrentDepth { get { return currentDepth; } }

        /// <summary>
        /// Ensures that the current event is of the specified type, returns it and moves to the next event.
        /// </summary>
        /// <typeparam name="T">Type of the <see cref="Event"/>.</typeparam>
        /// <returns>Returns the current event.</returns>
        /// <exception cref="YamlException">If the current event is not of the specified type.</exception>
        public T Expect<T>() where T : Event
        {
            var yamlEvent = Allow<T>();
            if (yamlEvent == null)
            {
                // TODO: Throw a better exception
                throw new YamlException(
                    parser.Current.Start,
                    parser.Current.End,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Expected '{0}', got '{1}' (at line {2}, character {3}).",
                        typeof(T).Name,
                        parser.Current.GetType().Name,
                        parser.Current.Start.Line,
                        parser.Current.Start.Column
                        )
                    );
            }
            return yamlEvent;
        }

        /// <summary>
        /// Moves to the next event.
        /// </summary>

View on GitHub (pinned to 96fad776d2)