What the error means
A YAML document holds exactly one top-level node: a mapping, a sequence, or a single scalar. Once the parser has finished that node, the only things it will accept are the end of the input or a document separator, ---, which starts a new document. end of the stream or a document separator is expected means it finished the top-level node and then found something else.
The example illustrates the most surprising version of this. Line one, version 2, has no colon, so YAML reads it as a plain scalar — the string "version 2" — and that scalar is the entire document. Line two then tries to start a mapping, which cannot follow a completed scalar, and the parser stops there. The real mistake is the missing colon on line one; the error is reported on line two.
Why it happens
- A missing colon on the first line, which turns what should be a key into a whole-document scalar, as above.
- Two top-level values with no separator, typically two config files pasted together.
- Content after a block that was closed by dedenting to column zero, for example a stray line of text at the end of a file.
- A stray
---or...in the wrong place, which ends the document early so that everything after it is unattached. - A copied snippet that starts with a list item (
- name: x) followed by a mapping key at column zero.
How to fix it
- Paste the document into the validator above and note the line in the error card.
- Look at the line before it first. If that line has a key without a colon, add the colon. This resolves the example.
- If there are genuinely two documents, put
---on its own line between them. The validator reports the document count, so you can confirm it now sees two. - If a trailing line does not belong anywhere, delete it or indent it under the key it was meant for.
The corrected example:
version: 2
services:
web: nginx
If it still fails
- Docker Compose and CI files are the usual sources. Paste the fixed file into YAML to JSON; if the JSON shows a top-level string where you expected an object, a colon is still missing somewhere.
- A
%YAMLor%TAGdirective must come before the first---. Placing one after a document produces this error too. - Windows line endings are fine; a stray form-feed or other control character is not, and will be reported at its position.
Related errors
bad indentation of a mapping entry
Every key in the same mapping must start in the same column, indented with spaces only. Align the offending line with its siblings and replace any tab with spaces.
duplicated mapping key
Remove or rename one of the duplicate keys. If both values are needed, they belong under different keys or in a list, not in the same mapping.