Utility to correctly strip LLM prompts
Project description
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cleanprompt-0.1.1.tar.gz.
File metadata
- Download URL: cleanprompt-0.1.1.tar.gz
- Upload date:
- Size: 47.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59e4ff7fa3effff4c4ec124d93f92fb51f2393ea9c591404cc8c8c7af311cd29
|
|
| MD5 |
a12f0ff9d91f997dc660917bd87cffb6
|
|
| BLAKE2b-256 |
0e7cedd59e1063678255680dcd51821b43e6e728f8dfc3e90bfc450880047847
|
File details
Details for the file cleanprompt-0.1.1-py3-none-any.whl.
File metadata
- Download URL: cleanprompt-0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0537d89c8f99f5b8d8bc234a5bdfba82ed0a66b5f64a9f568afffd0bc5fb8ba7
|
|
| MD5 |
1e0e8c8a22b294fe9b769544765ef034
|
|
| BLAKE2b-256 |
8c1ec7395e1272e35b80cb2e8a50c5824552824d4ea0eb5c0b83c369a8b6ee91
|