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.
pip install yamlfixVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `yamlfix` both as a command-line tool (simulated) and programmatically using `fix_files` to modify a file in place, and `fix_code` to format a YAML string. It highlights how the tool corrects common formatting inconsistencies like indentation, missing document headers, and list styling.
Always review changes made by `yamlfix` using version control (e.g., `git diff`) before committing. Consider using `--check` flag or `fix_code` with manual comparison for critical files. Report unexpected behavior to the project's GitHub issues. Explicitly configure quoting behavior if specific styles are required (e.g., `preserve_quotes`, `quote_basic_values`).
If a file should not have a `---` header or a shebang needs to remain at a specific position, verify `yamlfix`'s behavior and consider excluding such files or configuring `explicit_document_start` if available. For shebangs, manual intervention or a pre-commit hook specifically for shebangs might be necessary. Check the `explicit_document_start` configuration option.
Ensure configuration keys and their types strictly match those defined in the `YamlfixConfig` class. When using environment variables, be mindful that all values are initially strings and Pydantic will attempt to cast them to the expected type (e.g., 'true'/'false' to boolean, numeric strings to int/float). Consult the official documentation for available configuration options and their expected types.
pip install yamlfix
pip install yamlfix (if not already installed) or ensure your Python environment's script directory is in your system's PATH.
Correct the YAML syntax errors in 'your_file.yaml' to ensure it is valid YAML. Review the error message for specific details about the parsing issue.
Ensure the YAML string or content of the file being processed is syntactically valid YAML. Review the error message for line and column details to pinpoint the issue.