YAML Scanner Error: Mapping Values Are Not Allowed Here

Complete guide to fixing YAML scanner errors and mapping value issues

Last updated: November 2024 7 min read

Understanding the Error

yaml.scanner.ScannerError: mapping values are not allowed here
  in "<file>", line X, column Y

This error occurs when the YAML parser encounters a colon (:) in a context where it's not expected. The parser interprets the colon as the start of a mapping (key-value pair), but the surrounding syntax doesn't allow it. Common causes include unquoted strings containing colons, incorrect list syntax, or misplaced colons.

Common Causes and Solutions

1. Unquoted Strings with Colons

Values containing colons must be quoted to prevent the parser from treating them as mappings.

Incorrect:

time: 12:30:45
url: https://example.com
version: 1.0:beta

Correct:

time: "12:30:45"
url: "https://example.com"
version: "1.0:beta"

2. Incorrect List Syntax

Lists with inline syntax cannot contain unquoted values with colons.

Incorrect:

items: [key1: value1, key2: value2]

Correct:

items:
  - key1: value1
  - key2: value2

3. Multiple Colons in a Line

Multiple colons on the same line without proper quoting cause parsing errors.

Incorrect:

description: Time: 12:30, Date: 2024-01-01

Correct:

description: "Time: 12:30, Date: 2024-01-01"

4. Colons in Flow Collections

Inline maps and lists require proper quoting when values contain colons.

Incorrect:

config: {time: 12:30, url: https://example.com}

Correct:

config:
  time: "12:30"
  url: "https://example.com"

Step-by-Step Fix

Step 1: Locate the Error

The error message indicates the exact line and column. Check that location:

# Error message shows:
# in "file.yaml", line 5, column 15
# Check line 5, character position 15

Step 2: Identify the Colon

Find the colon causing the issue and determine if it's part of a value that needs quoting:

  • Is the colon part of a time string (e.g., "12:30:45")?
  • Is it part of a URL (e.g., "https://example.com")?
  • Is it part of a version string (e.g., "1.0:beta")?
  • Is it incorrectly placed in a list or map?

Step 3: Apply the Fix

Quote the value containing the colon or restructure the YAML:

# Quote values with colons
time: "12:30:45"
url: "https://example.com"

# Or use block scalar for multi-line
description: |
  Time: 12:30
  Date: 2024-01-01

Real-World Examples

Example 1: Kubernetes ConfigMap

Problematic:

apiVersion: v1
kind: ConfigMap
data:
  endpoint: https://api.example.com:8080/v1
  timeout: 30:00

Fixed:

apiVersion: v1
kind: ConfigMap
data:
  endpoint: "https://api.example.com:8080/v1"
  timeout: "30:00"

Example 2: Docker Compose

Problematic:

services:
  app:
    environment:
      - DATABASE_URL=postgresql://user:pass@host:5432/db
      - REDIS_URL=redis://localhost:6379

Fixed:

services:
  app:
    environment:
      - "DATABASE_URL=postgresql://user:pass@host:5432/db"
      - "REDIS_URL=redis://localhost:6379"

Prevention Tips

  • Quote values with colons - Always quote strings containing colons, especially URLs, times, and version strings
  • Use block syntax for complex values - For multi-line or complex strings, use block scalars (| or >)
  • Validate before use - Always validate YAML files before deploying or committing
  • Use YAML linters - Configure yamllint or similar tools in your development workflow

Related Articles