Skip to content

BUG: CharacterSpaceConverter introduces whitespace holes and trailing whitespace #4043

Closed

Description

@Kaap10

Describe the bug

CharacterSpaceConverter spaces out characters with " ".join(prompt) before removing punctuation with re.sub(...). This order of operations introduces whitespace artifacts:

  1. Stripping punctuation leaves empty character "holes", producing 4 spaces between words (e.g., "Hello, world" -> "H e l l o w o r l d" instead of the standard 3-space inter-word spacing "H e l l o w o r l d").
  2. Contractions or mid-word punctuation leave double spaces (e.g., "How's" -> "H o w s" instead of "H o w s").
  3. Trailing punctuation leaves unwanted trailing whitespace (e.g., "hello!" -> "h e l l o ").

Additionally, the punctuation regex is recompiled on every invocation of convert_async rather than precompiled at module level.

Steps/Code to Reproduce

import asyncio
from pyrit.converter import CharacterSpaceConverter

async def main():
    converter = CharacterSpaceConverter()
    res1 = await converter.convert_async(prompt="hello!")
    print(repr(res1.output_text))  # prints 'h e l l o ' with trailing space

    res2 = await converter.convert_async(prompt="Hello, world! How's everything?")
    print(repr(res2.output_text))  # prints 'H e l l o    w o r l d    H o w  s   e v e r y t h i n g '

asyncio.run(main())

Expected Results

Punctuation should be stripped prior to character spacing so spacing remains uniform without artificial gaps or trailing whitespace:

  • "hello!" -> 'h e l l o'
  • "Hello, world! How's everything?" -> 'H e l l o w o r l d H o w s e v e r y t h i n g'

Actual Results

  • 'h e l l o '
  • 'H e l l o w o r l d H o w s e v e r y t h i n g '

Versions

  • OS: Windows 11
  • Python version: 3.12
  • PyRIT version: main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions