Yamlfix
Yamlfix is a simple, opinionated YAML formatter that automatically corrects formatting issues and preserves comments. It ensures consistent styling, adds document headers, corrects truthy strings, removes unnecessary apostrophes, and handles line endings and list styles. The library is actively maintained, with version 1.19.1 released in December 2025, and has a consistent release cadence with frequent updates.
Warnings
- gotcha Yamlfix can sometimes inadvertently alter the structure or quoting of certain YAML constructs, particularly multi-line strings, volumes definitions (e.g., in Docker Compose), or long keys, leading to functional changes if not carefully reviewed.
- gotcha The tool might move YAML 'shebangs' (e.g., `#!/usr/bin/env yaml`) to the left or introduce unexpected header separators (`---`) even if the file is correctly formed without one, which might break scripts relying on specific file content or existing YAML standards.
- gotcha Configuration via `pyproject.toml`, `.yamlfix.toml`, or environment variables is powerful but can be overridden by conflicting settings, and values are parsed by Pydantic, which enforces type. Providing a value with the wrong type (e.g., a string for a boolean) will result in errors.
Install
-
pip install yamlfix
Imports
- fix_files
from yamlfix import fix_files
- fix_code
from yamlfix import fix_code
Quickstart
import os
import stat
from yamlfix import fix_files, fix_code
# --- CLI Usage Example ---
# Create a malformed YAML file
malformed_cli_yaml = """
key: value
another_key: nested_value
- item1
- item2
"""
with open("malformed.yaml", "w") as f:
f.write(malformed_cli_yaml)
# Run yamlfix via CLI (simulate for quickstart)
# In a real scenario, you would run: !yamlfix malformed.yaml
print("\n--- CLI (Simulated) ---")
print("Original malformed.yaml:")
with open("malformed.yaml", "r") as f:
print(f.read())
# Simulate fixing by reading, fixing with fix_code, and writing back
fixed_content_cli = fix_code(malformed_cli_yaml)
with open("malformed.yaml", "w") as f:
f.write(fixed_content_cli)
print("Fixed malformed.yaml:")
with open("malformed.yaml", "r") as f:
print(f.read())
# --- Programmatic Usage Example (fix_files) ---
# Create another malformed YAML file
malformed_file_yaml = """
list:
- item_a
- item_b
# A comment
-item_c
"""
with open("another_malformed.yml", "w") as f:
f.write(malformed_file_yaml)
print("\n--- Programmatic (fix_files) ---")
print("Original another_malformed.yml:")
with open("another_malformed.yml", "r") as f:
print(f.read())
# Fix the file(s) programmatically
fix_files(["another_malformed.yml"])
print("Fixed another_malformed.yml:")
with open("another_malformed.yml", "r") as f:
print(f.read())
# --- Programmatic Usage Example (fix_code) ---
print("\n--- Programmatic (fix_code) ---")
code_to_fix = """
key1: value1
key2: value2
"""
print("Original code string:\n" + code_to_fix)
fixed_code_string = fix_code(code_to_fix)
print("Fixed code string:\n" + fixed_code_string)
# Cleanup generated files
os.remove("malformed.yaml")
os.remove("another_malformed.yml")