Skip to main content

Auto-formatter for LLM prompt files

Project description

promptfmt

Auto-formatter for LLM prompt files.

PyPI License: MIT Python 3.9+

What It Does

promptfmt is an opinionated auto-formatter for LLM prompt files, similar to what Prettier does for JavaScript or Black does for Python. It normalizes whitespace, section delimiters, template variable syntax, line wrapping, and structural formatting for YAML/JSON prompt files.

After formatting, promptfmt verifies semantic equivalence -- it re-parses the formatted output and confirms that the meaning of the prompt has not changed (same messages, roles, variables, and metadata).

Installation

pip install promptfmt-ai

Dependencies: prompttools-core-ai >= 1.0, typer >= 0.12, rich >= 13.0, watchfiles >= 0.21

CLI Commands

promptfmt format

Format prompt files in-place or check formatting.

# Format a single file
promptfmt format prompts/greeting.yaml

# Format all prompt files in a directory (recursive)
promptfmt format prompts/

# Check only -- exit 1 if any file would change (for CI)
promptfmt format prompts/ --check

# Show a unified diff of changes
promptfmt format prompts/ --diff

# Quiet mode (no output except errors)
promptfmt format prompts/ -q

Options:

Option Default Description
--check false Check only, do not write files. Exit code 1 if changes needed.
--diff false Show unified diff of changes.
--delimiter-style ### Target delimiter style: ###, ---, ===, ***, ~~~
--variable-style double_brace Target variable syntax: double_brace, single_brace, angle_bracket
--max-line-length 120 Maximum line length for wrapping. 0 to disable.
--quiet, -q false Suppress non-error output.

promptfmt init

Generate a default .promptfmt.yaml configuration file in the current directory.

promptfmt init

This creates a .promptfmt.yaml with default settings:

# promptfmt configuration
delimiter_style: '###'
variable_style: double_brace
max_line_length: 120
wrap_style: soft
sort_metadata_keys: true
indent: 2
exclude:
  - 'vendor/**'
  - '*.generated.*'

Formatting Rules

promptfmt applies five rule categories in order:

1. Whitespace Normalization

  • Normalize line endings to LF
  • Strip trailing whitespace from every line
  • Remove leading blank lines at file start
  • Collapse 3+ consecutive blank lines to 2
  • Ensure file ends with exactly one newline

2. Delimiter Normalization

Normalizes section delimiters (---, ===, ***, ~~~) to a single consistent style. Code blocks (```) are preserved and not modified.

Supported styles: ###, ---, ===, ***, ~~~

3. Variable Syntax Normalization

Converts all template variable references to a single syntax style. Inline code spans (`code`) are preserved and not modified.

Style Syntax Example
double_brace {{var}} Hello {{name}}
single_brace {var} Hello {name}
angle_bracket <var> Hello <name>

Common HTML tags (div, span, p, br, etc.) are excluded from variable detection when using angle bracket style.

4. Line Wrapping

Wraps lines exceeding the configured maximum length at word boundaries. The following are never wrapped:

  • Lines inside code blocks
  • URL lines
  • Table rows (starting with |)
  • Heading lines (starting with #)

Leading indentation is preserved on continuation lines.

5. Structure Normalization (YAML/JSON only)

For YAML and JSON files:

  • Re-serializes with consistent indentation (default: 2 spaces)
  • Sorts metadata keys with priority ordering: model, name, description, defaults appear first, then remaining keys alphabetically
  • Within message objects, sorts keys with role before content
  • Re-applies whitespace normalization after structural changes

Configuration (.promptfmt.yaml)

Key Type Default Description
delimiter_style string "###" Target delimiter style
variable_style string "double_brace" Target variable syntax
max_line_length int 120 Max line length (0 to disable)
wrap_style string "soft" Wrapping approach
sort_metadata_keys bool true Sort keys in YAML/JSON
indent int 2 Indentation width for YAML/JSON
exclude list [] Glob patterns to exclude

Programmatic Usage

from promptfmt import format_file, format_content, FmtConfig, is_equivalent

# Format a file
config = FmtConfig(delimiter_style="---", variable_style="double_brace")
result = format_file("prompts/greeting.yaml", config)

print(result.changed)      # True if content changed
print(result.equivalent)   # True if semantically equivalent
print(result.formatted_content)

# Format raw content
from prompttools_core import PromptFormat
formatted = format_content(content, PromptFormat.YAML, config)

# Check semantic equivalence between two parsed prompts
from prompttools_core import parse_file
original = parse_file("original.yaml")
modified = parse_file("modified.yaml")
print(is_equivalent(original, modified))

CI Integration

GitHub Actions

- name: Check prompt formatting
  run: promptfmt format prompts/ --check

Pre-commit Hook

#!/bin/sh
promptfmt format prompts/ --check || exit 1

GitLab CI

check-prompt-format:
  script:
    - pip install promptfmt-ai
    - promptfmt format prompts/ --check

Exit codes:

Code Meaning
0 All files formatted (or no changes needed)
1 Files need formatting (with --check)
2 Errors occurred during formatting

License

MIT License. Author: Scott Converse.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

promptfmt_ai-1.0.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

promptfmt_ai-1.0.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file promptfmt_ai-1.0.0.tar.gz.

File metadata

  • Download URL: promptfmt_ai-1.0.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for promptfmt_ai-1.0.0.tar.gz
Algorithm Hash digest
SHA256 493605b6026a00eddc0a238b1f1ffdbadb435c63b71cee9885d8f18d13f602a0
MD5 e4b42fe58e412ce9ee68fe1f5facc8a6
BLAKE2b-256 d8dba0ed78665a3360d704bf3d7de0202b21c4adb29354d1220ad4c31bf20395

See more details on using hashes here.

File details

Details for the file promptfmt_ai-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: promptfmt_ai-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for promptfmt_ai-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48d45c58e442ba2c40073f6ed4607c0c39f8f1f11b97854027f543d257c41a06
MD5 9a05097c2044c28a822829dbf46021f0
BLAKE2b-256 17cd1aa84a04b39b3d291e03afa1584bfd9198f723463a1ad540666a9fca2be1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page