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
- Inspect the message's 'got X at line/character' part and fix the YAML or the Expect call to match the actual event type
- Use Allow<T>() (returns null) instead of Expect<T>() when the event type is optional
- Call reader.Peek()/Allow for a union of acceptable types before narrowing
- 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
- Prefer Allow<T>() when the event type is optional
- Dump the offending YAML around the reported line/column
- Don't call Expect after the stream has ended
- Validate document shape with a schema before strict event-walking
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
- Did not find expected <stream-start>.
- Did not find expected
- While parsing a node, find undefined tag handle.
- While parsing a node, did not find expected node content.
- While parsing a block collection, did not find expected '-'…
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)