stride3d/stride · error · YamlException

Unable to load the given stream

Error message

Unable to load the given stream

What it means

DynamicYaml parses YAML text and requires the stream to contain exactly one document whose root is a YAML mapping. If the parse yields zero documents, multiple documents, or a non-mapping root (sequence/scalar), it throws YamlException("Unable to load the given stream").

Solutions

  1. Fix the YAML text so it is a single top-level mapping (key: value at root), like a normal Stride asset file.
  2. Remove extra '---' document separators so only one document remains.
  3. Validate the YAML with a parser/YAML linter before constructing DynamicYaml.
  4. Ensure the input string is the actual asset content, not empty, truncated, or a different file type.

Example fix

// before
var yaml = new DynamicYaml("- a\n- b"); // root is a sequence -> throws
// after
var yaml = new DynamicYaml("Items:\n- a\n- b"); // root is a mapping
Defensive patterns

Strategy: validation

Validate before calling

bool isSingleMapping = text.Trim().Length > 0 && !text.TrimStart().StartsWith("-") && text.CountOccurrences("\n---") == 0;

Type guard

static bool IsYamlMappingStream(YamlStream s) =>
    s.Documents.Count == 1 && s.Documents[0].RootNode is YamlMappingNode;

Try / catch

try { var yaml = new DynamicYaml(text); }
catch (YamlException) { /* invalid or multi-document YAML */ }

Prevention

When it happens

Trigger: Constructing DynamicYaml from a string/stream that: is empty or malformed YAML (parse itself degrades), contains multiple --- separated documents, or has a root node that is a sequence ("- a\n- b") or scalar ("just text") instead of a mapping.

Common situations: Asset YAML edited by hand and broken; concatenated asset files with multiple documents; loading a list-style YAML where a top-level map (with !Name/Id tags) is required; wrong file passed (e.g. a log or binary file).

Related errors


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

Appendix: source

Thrown at sources/assets/Stride.Core.Assets.Yaml/DynamicYaml/DynamicYaml.cs:47

        {
            using var assetStreamReader = new StreamReader(stream, Encoding.UTF8);
            assetAsString = assetStreamReader.ReadToEnd();
        }
        finally
        {
            if (disposeStream)
            {
                stream.Dispose();
            }
        }

        // Load the asset as a YamlNode object
        var input = new StringReader(assetAsString);
        yamlStream = [];
        yamlStream.Load(input);

        if (yamlStream.Documents.Count != 1 || yamlStream.Documents[0].RootNode is not YamlMappingNode)
            throw new YamlException("Unable to load the given stream");
    }

    /// <summary>
    /// Initializes a new instance of <see cref="DynamicYaml"/> from the specified stream.
    /// </summary>
    /// <param name="text">A text that contains a YAML content</param>
    public DynamicYaml(string text) : this(GetSafeStream(text))
    {
    }
    /// <summary>
    /// Gets the root YAML node.
    /// </summary>
    public YamlMappingNode RootNode => (YamlMappingNode)yamlStream.Documents[0].RootNode;

    /// <summary>
    /// Gets a dynamic YAML node around the <see cref="RootNode"/>.
    /// </summary>
    public dynamic DynamicRootNode => dynamicRootNode ??= new DynamicYamlMapping(RootNode);

View on GitHub (pinned to 96fad776d2)