Skip to tool
FeuTex · free tools runs in-browser no bloat built by LiMiT

YAML Formatter (Python)

Paste YAML to normalize indentation, remove messy whitespace, and generate a small Python script (ruamel.yaml / PyYAML) you can run in your project.

Category: Developer · URL: /tools/yaml-formatter-python.html
Tip: if your YAML contains tabs, enable “Convert tabs” or replace tabs with spaces before formatting.

ruamel.yaml preserves more YAML details (comments/quotes) than PyYAML; the script includes both options.

Privacy: runs locally in your browser. No uploads, no tracking scripts.

How to use

Use this tool to quickly clean up indentation and whitespace, then (optionally) export a Python formatter script.

  1. Paste your YAML into the input box.
  2. Select output indentation (2 or 4 spaces) and options like trailing-space cleanup.
  3. Click Format YAML and copy the result.
  4. Optional: click Generate Python script to format YAML files in your repo.
Keywords this page targets (natural cluster): yaml formatter python, python yaml formatter, format yaml in python, python pretty print yaml, pyyaml safe_dump format, ruamel yaml format file, yaml reindent online, fix yaml indentation, yaml tabs to spaces, normalize yaml spacing, yaml pretty formatter, yaml lint indentation, yaml beautifier python script, yaml formatter 2 spaces, yaml formatter 4 spaces, python yaml dump indent, convert messy yaml to clean yaml, kubernetes yaml formatter, ansible yaml formatter, yaml comment safe formatter, python script to format yaml, yaml formatting tool
Secondary intents covered: Reindent YAML to 2 or 4 spaces for consistent diffs, Detect and block tabs in YAML (or convert them to spaces), Fix misaligned indentation that breaks YAML parsing, Strip trailing spaces and normalize newlines, Copy formatted YAML back into a Python project or CI pipeline, Generate a runnable Python script to format YAML files locally, Choose ruamel.yaml for comment-preserving formatting or PyYAML for simple dumps, Create a clean YAML snippet for Kubernetes/Ansible configs, Quickly validate indentation style before committing

FAQ

Does this tool fully validate YAML?

No—this focuses on indentation/whitespace normalization and basic indentation sanity checks. For full YAML validation, parse it with ruamel.yaml or PyYAML in your environment.

Why does YAML fail when I use tabs?

YAML indentation is defined in spaces; tabs often break parsing or behave inconsistently across tools. Use spaces or enable the “Convert tabs to spaces” option.

Can it preserve comments and quotes exactly?

The web formatter only reindents lines; it does not parse YAML semantics. For true round-trip formatting that preserves comments/quotes, use the generated ruamel.yaml script.

What indentation should I use (2 or 4 spaces)?

2 spaces is the most common for YAML and tends to reduce diff noise; 4 can improve readability for deeply nested files. Pick one and keep it consistent.

Will it reorder keys or sort mappings like Python can?

No. This tool keeps your line order; sorting keys requires real YAML parsing and can change meaning in some workflows.

How do I format YAML files in my Python project automatically?

Generate the Python script, commit it to your repo, and run it in CI or pre-commit. Use ruamel.yaml if you want the best preservation of YAML structure.

Why do I get an “Inconsistent indentation” error?

Your file has lines whose leading spaces don’t align to the detected indent step (for example 2 spaces on one level and 3 on another). Fix the misaligned lines or reindent in an editor.