Skip to main content

Utility to correctly strip LLM prompts

Project description

pytest pre-commit

CleanPrompt

A simple yet frequently used Python utility for safely dedenting and stripping whitespace from LLM prompt strings.

The problem

When building prompts within Python code, multi-line strings ("""...""") are convenient, but they introduce unwanted leading indentation and newlines that can interfere with model tokenization. A naive textwrap.dedent(s).strip() works for most simple prompts. However, typing textwrap.dedent(s).strip() gets repetitive quite fast. Many developers wrap it in a single function somewhere in their code. Wouldn't it be nice to have this simple function tucked in a small package and use it everywhere? This is what CleanPrompt is trying to achieve.

Once we have the familiar textwrap.dedent(s).strip() wrapped in a function, in a package, other concerns start to appear: it fails for more complex cases! For example in few-shot prompts, trailing space (e.g., A: ) is a critical cue for the model.

CleanPrompt solves this by providing a set of simple, safe functions to intelligently dedent and strip whitespace, giving you full control over whether to strip leading, trailing, both, or no whitespace at all.

Installation

pip install cleanprompt

Usage and Examples

The package provides one main function, clean(), and three convenient aliases for common use cases.


Example 1: Standard Prompt Cleaning (Default)

For most self-contained prompts, you want to remove all code indentation and any surrounding blank lines. The default cleanup_prompt handles this perfectly.

import cleanprompt as cp

raw_prompt = """

    You are a helpful assistant.
    Please summarize the following text:

    ...text...

"""

clean = cp.clean(raw_prompt)

# Result:
# "You are a helpful assistant.\nPlease summarize the following text:\n\n...text..."

Example 2: Preserving Trailing Spaces for Few-Shot Cues

This is the critical use case. For few-shot prompts, you must preserve the trailing space after the final "cue" (e.g., A: ) to guide the model.

Here, we use clean_leading to only strip whitespace from the beginning of the string, leaving the important trailing space untouched.

import cleanprompt as cp

raw_few_shot = """
    Q: What is 2+2?
    A: 4

    Q: What is the capital of France?
    A: Paris

    Q: Who was the first US president?
    A: """

clean = cp.clean_leading(raw_few_shot)

# The clean string correctly ends with "A: "
assert clean.endswith("A: ")

# Result:
# "Q: What is 2+2?\nA: 4\n\nQ: What is the capital of France?\nA: Paris\n\nQ: Who was the first US president?\nA: "

License

Distributed under the terms of the MIT license.

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

cleanprompt-0.1.0.tar.gz (47.5 kB view details)

Uploaded Source

Built Distribution

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

cleanprompt-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file cleanprompt-0.1.0.tar.gz.

File metadata

  • Download URL: cleanprompt-0.1.0.tar.gz
  • Upload date:
  • Size: 47.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for cleanprompt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1aaf4b9cdb94b22f4b6b8af28e3c683726b3d26ff0285c6ae4753c746e357c48
MD5 66f532c538ecfb2bf5702bfd9454f00c
BLAKE2b-256 d3a719755fe0e4f951657b0b0f89b083608d83cd83bf7decd3eec53037a56534

See more details on using hashes here.

File details

Details for the file cleanprompt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cleanprompt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for cleanprompt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1545ee538949944ec37b7971bf6c227b060baf1e243abf3bc9ee1f16c82b9e2
MD5 33c81d89b4142e5877ef6f861e995d07
BLAKE2b-256 5519536d937e00f73996ffad887134be996046bce39744f35ae782bc03fd38bb

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