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.0.1.tar.gz (8.0 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.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cleanprompt-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3ba7e94a383df7dc8fcf51d25482ac1633fc931ce1a353f0471055a87ada66d5
MD5 34edad26354f8dd4ccdeec783f7b4cc7
BLAKE2b-256 03a5d5004ab19381037ff586742fcc2b20b5e86d5f008572a5d73532fb6c61a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cleanprompt-0.0.1-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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14bf07c348447a598c82cc8117b94901b3b6c2aa293f8c85b1245cf49de420b4
MD5 552f4b83fb5b7704af7c80cf327de953
BLAKE2b-256 45d1cbbcb52389c603f178f788218b98bbb1550ab3efebd7e75217d152d301d0

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