Skip to content

FEAT: Add PinyinConverter for Chinese Pinyin-mix transformations - #2649

Open
Nantha kumar (Nanduu24) wants to merge 1 commit into
microsoft:mainfrom
Nanduu24:feat/pinyin-converter
Open

FEAT: Add PinyinConverter for Chinese Pinyin-mix transformations#2649
Nantha kumar (Nanduu24) wants to merge 1 commit into
microsoft:mainfrom
Nanduu24:feat/pinyin-converter

Conversation

@Nanduu24

@Nanduu24 Nantha kumar (Nanduu24) commented Sep 13, 2026

Copy link
Copy Markdown

Description

Adds a deterministic (no-LLM) text-to-text converter, PinyinConverter, that rewrites Chinese (Hanzi) characters as their Pinyin romanization. Pinyin mixing is a Chinese-specific adversarial text transformation described in recent Chinese LLM safety work such as CSSBench: Hanzi characters or spans are rewritten as full or abbreviated Pinyin while the text stays understandable to a Chinese-reading model. This can bypass keyword- and token-level safety filters that match on Hanzi rather than on romanized readings. I could not find an existing PyRIT converter that performs a Hanzi→Pinyin transformation (translation changes the language; character-noise and Unicode converters don't model romanization).

Behavior

  • mode: how a converted Hanzi is rendered
    • full — full toneless reading (zhong)
    • initial — first letter only (z)
    • mixed — chooses full or initial independently per character
  • proportion ([0.0, 1.0]): fraction of Hanzi to convert. Values below 1.0 leave the rest as Hanzi, producing mixed Hanzi/Pinyin text. Selection count is round(proportion * n_hanzi), positions chosen via the converter's seeded RNG.
  • separator: optional string inserted between converted syllables (e.g. " "zhong xin); no trailing separator is left.
  • seed: reproducible selection and, in mixed mode, reproducible per-character rendering.
  • Non-Hanzi characters (Latin, digits, punctuation, whitespace, emoji, CJK punctuation) always pass through unchanged.

Example:

full    : nihao,shijie!zheshiyigeceshi。
initial : nh,sj!zsygcs。
mixed   : nih,shij!zheshiyigeceshi。
partial : jin 天天qi 很hao wo men 出去玩ba   (mode=full, proportion=0.5, separator=" ")

Dependency

pypinyin (MIT, pure-Python) is added as an optional dependency (pip install pyrit[pinyin]) and imported lazily inside the converter, following the pattern used by other optional-dependency converters. It is also added to the all extra. Conversion tests are skipif-gated when pypinyin is not installed, so they run in the dev_all CI job and skip cleanly in the dev job.

Testing

  • 15 new unit tests in tests/unit/converter/test_pinyin_converter.py covering each mode, proportion/mixing, separator handling, passthrough, reproducibility, and validation.
  • Full converter unit suite passes locally (1513 passed, 34 skipped).
  • ruff check, ruff format --check, and ty check all pass.

Closes #2647

Adds a deterministic (no-LLM) text converter that rewrites Chinese (Hanzi)
characters as their Pinyin romanization, a Chinese-specific adversarial pattern
described in recent Chinese LLM safety work (e.g. CSSBench). Pinyin mixing can
bypass keyword/token-level safety filters that match on Hanzi rather than on
romanized readings.

Supports three rendering modes (full reading, first-letter initial, and a
per-character mix), a configurable proportion of Hanzi to convert (values below
1.0 leave the rest as Hanzi, producing mixed Hanzi/Pinyin text), an optional
syllable separator, and a seed for reproducible selection. Non-Hanzi characters
always pass through unchanged.

pypinyin is added as an optional dependency (pip install pyrit[pinyin]) and
imported lazily, following the pattern used by other optional-dependency
converters. Tests that exercise conversion are skipped when pypinyin is absent.

Closes microsoft#2647
@Nanduu24

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Pinyin Mix converter for Chinese adversarial text transformations

1 participant