YAML error: mapping values are not allowed in this context

"mapping values are not allowed in this context" is a common error originated from the Python module pyyaml (https://pyyaml.org/). The library is widely used among Python community, so you may come across this error message while working with another tool, such as Ansible or yamllint.com.

The error usually occurs when there is an unexpected indentation or special character in your YAML file. In this article, we will show you how to debug and fix mapping values are not allowed in this context error in YAML.

Debugging your YAML file

Most of the time, the tool you’re using to parse YAML files will give you mapping values are not allowed in this context along with a few other details. The error output from PyYAML may look like below:

Traceback (most recent call last): File "/home/user/main.py", line 4, in <module> yaml.load(foofile, Loader=yaml.CLoader) File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 114, in load return loader.get_single_data() File "/usr/lib/python3/dist-packages/yaml/constructor.py", line 49, in get_single_data node = self.get_single_node() File "ext/_yaml.pyx", line 707, in _yaml.CParser.get_single_node File "ext/_yaml.pyx", line 725, in _yaml.CParser._compose_document File "ext/_yaml.pyx", line 774, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 851, in _yaml.CParser._compose_sequence_node File "ext/_yaml.pyx", line 776, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 890, in _yaml.CParser._compose_mapping_node File "ext/_yaml.pyx", line 774, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 851, in _yaml.CParser._compose_sequence_node File "ext/_yaml.pyx", line 776, in _yaml.CParser._compose_node File "ext/_yaml.pyx", line 892, in _yaml.CParser._compose_mapping_node File "ext/_yaml.pyx", line 905, in _yaml.CParser._parse_next_event yaml.scanner.ScannerError: mapping values are not allowed in this context in "example.yaml", line 6, column 11
Code language: JavaScript (javascript)

The error message may be something else, but there is one thing you need to pay attention to, which is the line number that causes the problem. In this case, that line is line 6. That’s all we need to further investigate.

Double check indentation

Most of the time the error comes from unexpected indentations in the YAML file. A YAML file use spaces as indentation, you can use 2 or 4 spaces for indentation, but no tab. In other words, tab indentation is forbidden. Plus, the use of spaces have to be unified across the file. For example, you cannot mix 2-space and 4-space lines in the same file.

Let’s take a look at an Ansible playbook file :

--- - name: Foofile test hosts: localhost tasks: - name: Make the foofile copy: content: foobar foobar foobar dest: /tmp/foofile.txt
Code language: JavaScript (javascript)

We can see that from line 6 (copy:), the contents have extended indentations. We can fix it by moving the spaces back an indentation level, which should look like this :

--- - name: Foofile test hosts: localhost tasks: - name: Make the foofile copy: content: foobar foobar foobar dest: /tmp/foofile.txt
Code language: JavaScript (javascript)

All we had to do was move “copy” and all its children back two spaces.

Check for strange Unicode characters

One of the lesser-known things that causes mapping values are not allowed in this context error is Unicode characters.

Most of the time, you can manually remove strange-looking Unicode characters on your own, but pay extra attention to the various Unicode spaces.

Besides a regular space, we have other kinds of spaces such as U+2009(thin space), U+200A (hair space) and especially U+200B (zero width space) which doesn’t even show up in the file. YAML doesn’t recognize them as separation spaces, which is why parsing fails and greets you with mapping values are not allowed in this context error.

If you’re using Visual Studio Code, you can install the Gremlins extension, which reveals invisible whitespace and other annoying characters by showing them in the editor.

In rare cases, you should also look into dashes, too. A copy-paste code snippet from the web can easily bring special ‘EN DASH’ (U+2013) instead of the regular dash (named ‘HYPHEN MINUS’ U+002D) to your file.

Validate YAML syntax

We’ve written about the most common scenarios above, but if you’ve tried them all without any success, you may want to soak up your knowledge about YAML a little bit. YAML for beginners by Red Hat and Ansible’s YAML Basics are great quick reads. For more, consult Official YAML Specifications.

We hope that the short article provide helpful information on how to handle mapping values are not allowed in this context error in YAML files. You may want to check out our other great articles like how to use RegEx in VSCodeHandle merge conflicts and Quickly create HTML Boilerplate in VSCode.

If you have any question, don’t hesitate to let us know in the comment section below.

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment