Skip to main content

A library for writing multi-line strings with proper indentation in Python code

Project description

Dedent

A Python library for writing multi-line strings with proper indentation in your code, then automatically removing that indentation from the final string.

Perfect for LLM prompts and other multi-line strings that you want to keep properly indented within your code structure, especially when nested inside functions.

Installation

pip install prompt_dedent

Or install from source:

git clone https://github.com/yourusername/dedent.git
cd dedent
pip install -e .

Quick Start

from prompt_dedent import dedent

text = dedent(
    """
    This is a multi-line string
    that can be indented in your code
    to match your code structure.
    """
)

print(text)
# Output:
# This is a multi-line string
# that can be indented in your code
# to match your code structure.

Common Use Case: LLM Prompts

The typical use case for dedent is writing LLM prompts that are nested inside functions. Without dedent, you'd have to choose between messy unindented strings or awkward formatting. With dedent, you can write clean, properly indented prompts:

from dedent import dedent

def analyze_message(message: str) -> dict:
    system_message = dedent(
        f"""
        Determine whether the following message includes a scheduling request.
        A scheduling request is defined as a scheduling inquiry that can be tied
        to a specific date, set of dates, or date range.
        
        Examples of scheduling requests:
            - this Saturday
            - next Friday
            - Tuesday, Feb 20th
            - August 25 - Sept 16
            - June 18th at 5pm
        
        Examples that are NOT scheduling requests:
            - saturday (without context)
            - friday (without context)
            - weekends
            - weekdays
        
        Return your response in the following format:
        {{
            "has_scheduling_request": bool
        }}
        """
    )
    
    # Use system_message with your LLM API...
    return {"has_scheduling_request": True}

Notice how the prompt stays properly indented within the function, making your code much more readable!

Features

  • Automatic indentation removal: Write strings with proper indentation that matches your code
  • Nested indentation support: Create nested structures like bullet points
  • F-string insertion: Insert multi-line variables into your dedented strings using the insert() function
  • Trailing whitespace removal: Automatically removes trailing whitespace and empty lines

Usage

Basic Usage

from dedent import dedent

message = dedent(
    """
    Hello, World!
    This is a nicely formatted message.
    """
)

Nested Indentation

from dedent import dedent

documentation = dedent(
    """
    Here's a list:
    
        - First item
        - Second item
            - Nested item
    """
)

Inserting Variables with insert()

When you need to insert a multi-line string variable into your dedented text:

from prompt_dedent import dedent, insert

inserted_text = """Line 1
Line 2
    - Indented line"""

result = dedent(
    f"""
    Here's some text:
        {insert(inserted_text)}
    And more text.
    """
)

Preserving Trailing Whitespace

By default, trailing whitespace is removed. To preserve it:

text = dedent(
    """
    Some text
    
    """,
    remove_trailing_whitespace=False
)

Rules

  1. First line must be empty: The string must start with a newline
  2. Second line determines base indentation: The indentation of the second line is used as the base
  3. All content lines must have at least base indentation: Lines with content must start with at least the base indentation
  4. Whitespace-only lines become empty: Lines with only whitespace are converted to empty strings

Tips

In VS Code, when you hit Enter inside a triple-quoted string, the default indentation is set to where the opening """ is. So this format is easiest:

dedent(
    """
    Your text here.
    """
)

Rather than:

dedent("""
    Your text here.
""")

License

MIT

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

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

prompt_dedent-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

prompt_dedent-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for prompt_dedent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9d834bdccc86a1c87ab1fdddaf2143fdd2290c87e449bb22726dc8861254ad39
MD5 5ec3d931bff49ab545ac00d50a191447
BLAKE2b-256 09e154f1b218482c5ae080b7c8791fdfa5c4fd3d510394357ad4e9efc2cd6a69

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for prompt_dedent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 418b4f8f4ca0d505f12e0667172a81d29211f079aaededa5002d0e96a4eee838
MD5 4eaa940359b010e5527b763187b01ba9
BLAKE2b-256 a4b4ac0fb66f9a728d2c79e20e208a1e90479ff8a7c8ed162cb292fa58fd71a9

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