# How to Fix YAML Indentation Errors

Fix common YAML indentation errors using practical nested mapping, list and multiline string examples and a reliable validation workflow.

By DevBricks Team
Published: 2026-11-27T03:39:00.000Z
Updated: 2026-11-17T03:39:26.217Z
Canonical: https://tools.devbrickstech.com/blog/how-to-fix-yaml-indentation-errors

YAML is popular for configuration files because it is readable and avoids many braces and commas. That convenience comes with a trade-off: **indentation is part of YAML's structure**. One line shifted a few spaces can move a setting into the wrong object, turn a list entry into a sibling, or cause a parser error.

**Quick answer:** Use spaces rather than tabs for indentation, keep sibling keys aligned, indent child values consistently, and check each list item against the parent key. Validate the result after every structural change. The [DevBricks YAML Validator](/tools/yaml-validator) can help check syntax, while a side-by-side comparison with the intended data model helps catch a valid file with the wrong nesting.

This guide explains the indentation rules, fixes common mistakes in lists and mappings, covers multiline strings and quoted values, and demonstrates a safe troubleshooting workflow for CI, deployment, and application configuration.

## Why YAML indentation matters

In YAML block style, indentation indicates hierarchy. Keys at the same indentation level are generally siblings. A value indented under a key belongs to that parent structure. A list item beginning with `-` belongs to a sequence at a particular indentation level.

Consider this configuration:

```yaml
service:
  name: api
  port: 8080
logging:
  level: info
```

`name` and `port` are both children of `service`. `logging` is a sibling of `service`, and `level` belongs to `logging`.

Now compare:

```yaml
service:
  name: api
  port: 8080
  logging:
    level: info
```

Both examples may parse as YAML, but they describe **different structures**. In the second example, `logging` is nested inside `service`. This is why a file can be syntactically valid yet fail in a program expecting a top-level `logging` section.

## YAML indentation rules to remember

| Rule | What it means | Practical advice |
|---|---|---|
| Use spaces for indentation | Tabs are not portable YAML indentation | Configure your editor to insert spaces |
| Indent children deeper than parents | Nested mappings need visible hierarchy | Use two spaces consistently as a style choice |
| Align sibling keys | Properties at the same level should align | Compare their starting columns |
| Align list items | Each item in one sequence should line up | Watch the position of `-` markers |
| Validate after changes | Correct syntax does not ensure correct meaning | Check parser and intended structure |

The YAML 1.2 specification says indentation in block styles is defined by spaces and that tab characters must not be used in indentation for portability. It does **not** mandate exactly two spaces for every nested level. Two spaces is a common team style that makes files easier to review.

## Error 1: using tabs for indentation

A tab and several spaces can look similar in an editor, but they are not interchangeable in YAML block indentation. Some editors visually expand tabs, making the problem hard to see.

**Problem:** A line is indented with a tab character instead of spaces:

```text
service:
<TAB>port: 8080
```

Here `<TAB>` stands for a literal tab and is only a visual demonstration.

**Safer version:**

```yaml
service:
  port: 8080
```

Turn on whitespace visibility in your editor and set the Tab key to insert spaces for YAML files. Before globally converting tabs, check whether there are tabs intentionally inside quoted string values, which are not the same as indentation.

## Error 2: child key not indented under its parent

**Unintended structure:**

```yaml
database:
host: localhost
port: 5432
```

This may parse as top-level keys, leaving `database` without the nested configuration your application expects.

**Intended structure:**

```yaml
database:
  host: localhost
  port: 5432
```

The two-space indent now makes `host` and `port` children of `database`. A parser-only check may not catch the original as an error, so compare the resulting object shape to the application's documentation or schema.

## Error 3: inconsistent alignment of sibling keys

**Problem:**

```yaml
server:
  host: localhost
    port: 8080
```

The extra indentation before `port` can produce a parser error or an unintended structure, depending on the surrounding syntax.

**Corrected:**

```yaml
server:
  host: localhost
  port: 8080
```

A simple diagnostic is to place the cursor at the beginning of each child property. If the keys are intended to be siblings, their first characters should start in the same column.

## Error 4: list items at the wrong depth

Sequences are introduced with a `-` marker. For example:

```yaml
services:
  - name: web
    port: 8080
  - name: worker
    port: 8081
```

Here `services` contains two objects, and each object has `name` and `port` properties.

A common mistake is to misalign a list item's continuation fields:

```yaml
services:
  - name: web
  port: 8080
  - name: worker
```

The `port` line no longer clearly belongs to the first list item and may cause parsing trouble. To fix the issue, align item properties beneath the first property in the list entry, as in the valid example.

For a list of plain strings, use:

```yaml
allowed_hosts:
  - example.com
  - api.example.com
```

Don't introduce an extra mapping level unless the application expects it.

## Error 5: mixing mapping and sequence structure

These are different data shapes:

**Mapping with named properties:**

```yaml
environment:
  NODE_ENV: production
  DEBUG: 'false'
```

**Sequence of key/value objects:**

```yaml
environment:
  - name: NODE_ENV
    value: production
  - name: DEBUG
    value: 'false'
```

Both can be valid YAML, but a specific framework may accept only one form. For example, a deployment manifest or CI configuration has a defined structure that cannot be inferred from indentation alone. Consult the destination schema before converting between styles.

Quoting `'false'` deliberately preserves it as a string rather than a boolean. Whether the application expects a string or boolean depends on its configuration contract.

## Error 6: misplaced colons and quoting

A colon followed by a space can be structural YAML syntax. Unquoted values containing certain special character patterns may need quotes. For example:

```yaml
message: "Error: connection refused"
```

is easier to interpret reliably than an ambiguous unquoted value. URLs and timestamps can also deserve explicit quoting depending on the parser and expected type. YAML is not just “JSON without braces”; it has its own scalar and typing conventions.

When a configuration field must be a string, quote values such as version numbers, identifiers with leading zeros, and boolean-looking text when appropriate. This avoids accidental type interpretation and improves portability across YAML processors.

## Error 7: badly indented multiline strings

YAML provides block scalar syntax. The pipe `|` generally preserves line breaks; the greater-than sign `>` generally folds eligible line breaks. The content needs to be indented beneath its key.

Example:

```yaml
message: |
  First line of the message.
  Second line of the message.
```

If the second line shifts back to the parent's indentation level, it may leave the block or cause an error. Consistent indentation is essential. Be aware that YAML block scalar rules also involve trailing newline behavior and optional chomping indicators such as `|-`.

For configuration that stores a long command or certificate, test whether the application expects literal line breaks or folded text. A syntactically valid multiline string can still change a command's semantics.

## A step-by-step YAML debugging workflow

### Step 1: Save a copy of the failing file

Don't begin by reformatting a production configuration in place. Save a working copy so you can compare the original and corrected structure.

### Step 2: Find the parser's first error

A parser usually reports a line and column. Start there, then inspect the preceding lines. An incorrectly indented block can cause the parser to complain several lines after the actual mistake.

### Step 3: Make whitespace visible

Enable “show whitespace” or an equivalent editor option. Check for tab indentation, mixed spacing, or list markers that begin in the wrong column.

### Step 4: Draw the intended structure

Mentally or on paper, list which keys should be top-level, which should be nested, and which values should be arrays. Comparing against a simple conceptual tree often exposes mistakes that a syntax check misses.

### Step 5: Fix one structural issue at a time

Align siblings, indent children, and correct sequence items. Do not rewrite the entire file at once. Smaller edits make it easier to understand which change solved the problem.

### Step 6: Validate the file again

Use a suitable YAML parser or the [DevBricks YAML Validator](/tools/yaml-validator) to inspect syntax. For sensitive configuration, prefer local or approved tools. Validation should be followed by application-specific checks.

### Step 7: Run the destination's own validator

A Docker Compose file, Kubernetes manifest, GitHub Actions workflow, or Helm values file can be valid YAML but invalid for the destination schema. Use the corresponding platform validation command or documentation to confirm structure and supported fields.

## Worked example: correcting a deployment-style config

Suppose you intend to define two services with different environment settings:

```yaml
services:
  web:
    image: example/web:1.0
    environment:
      APP_MODE: production
      PORT: '8080'
  worker:
    image: example/worker:1.0
    environment:
      APP_MODE: production
```

The structure is a mapping named `services`, containing `web` and `worker`. Each service has an `image` and an `environment` mapping. The string `'8080'` is deliberately quoted because environment values are typically treated as strings.

If `worker` accidentally receives an extra two spaces of indentation, it might be interpreted as part of the preceding service or trigger a parse error. Comparing the parsed structure to the expected service names quickly reveals the problem.

The YAML syntax is only one layer. The image names and configuration keys are illustrative; a real deployment also needs platform-specific validation and an understanding of the actual application's environment variables.

## YAML vs JSON: which is easier to validate?

JSON uses explicit braces, brackets, commas, and quoted keys, making its structure less dependent on indentation. YAML can be more concise for human-maintained configurations but requires careful attention to whitespace, scalar types, and parser behavior.

If you need to inspect the structure of YAML, converting it into JSON can help reveal where keys and arrays are actually nested. The [DevBricks JSON to YAML Converter](/tools/json-to-yaml-converter) is useful for understanding representations, but don't assume round-trip conversion preserves every YAML comment, anchor, or formatting choice.

Choose the format your application supports and your team can maintain safely. For configuration-as-code, consistent formatting rules and automated validation in CI are often more valuable than debating personal style preferences.

## Frequently asked questions

### Is YAML required to use two spaces?

No. Two-space indentation is a popular convention, but YAML's specification does not require a universal indentation width. Child nodes must be consistently deeper than their parents.

### Can I use tabs in YAML?

Tabs are not permitted for block indentation under the YAML specification's portable rules. Configure your editor to insert spaces. Tabs can exist in contexts where they are not indentation, subject to the syntax rules.

### Why is my YAML valid but the app rejects it?

The YAML may parse into a structure that violates the application's own schema. Check required keys, value types, and whether a list or mapping was expected.

### Do quotes fix indentation errors?

No. Quotes help with scalar values, but they do not make incorrectly aligned mappings or lists valid. Fix the structural indentation separately.

### What should I check after an autoformatter runs?

Ensure that the parsed structure and significant scalar values still match the intended configuration. Review comments and special constructs when your formatter or converter may alter them.

## Final checklist

- [ ] Indentation uses spaces rather than tabs.
- [ ] Parent and child relationships are intentional.
- [ ] Sibling keys begin at matching indentation levels.
- [ ] Sequence markers and continuation fields are aligned.
- [ ] Quoted values retain the intended data types.
- [ ] Multiline strings preserve the intended line breaks.
- [ ] YAML parses without errors.
- [ ] The destination application's schema also accepts the file.
- [ ] A backup and meaningful diff are available.

## Final takeaway

The safest way to fix YAML indentation errors is to separate **syntax** from **meaning**. Correct the whitespace so the file parses, then verify that the resulting maps and lists are nested exactly where your application expects them. Consistent editor settings, small test cases, and platform-specific validation prevent repeat failures.

Check a non-sensitive configuration with the [DevBricks YAML Validator](/tools/yaml-validator).

## Sources and further reading

- [YAML 1.2.2 specification — Indentation spaces](https://yaml.org/spec/1.2/)
- [YAML specification project](https://github.com/yaml/yaml-spec)
