Skip to content

FEAT: Add DiffChainConverter and DiffChainDecodeConverter - #4297

Open
Absarahmedofficial wants to merge 5 commits into
microsoft:mainfrom
Absarahmedofficial:feat/diff-chain-converter
Open

Absarahmedofficial wants to merge 5 commits into
microsoft:mainfrom
Absarahmedofficial:feat/diff-chain-converter

Conversation

@Absarahmedofficial

@Absarahmedofficial Absarahmedofficial commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds DiffChainConverter (text -> text) and DiffChainDecodeConverter (text -> text) to encode a prompt as an initial PARTS placeholder file followed by sequential unified diff changesets (v0 -> vN) in PyRIT converter pipelines.

  • DiffChainConverter: Splits a UTF-8 text prompt into steps single-line JSON string slots on a virtual file (v0/spec.py by default) and emits unified diff hunks (context_lines=0 by default), patching slots in a seeded non-sequential order when shuffle_order=True. It only encodes; to add reconstruction instructions, stack PromptTemplateConverter with the {{ prompt }} template in pyrit/datasets/converters/diff_chain.yaml.
  • DiffChainDecodeConverter: Companion text -> text converter (plus DiffChainConverter.decode() helper) that extracts and applies the unified diff hunks in sequence back into UTF-8 text for pipeline chaining and round-trip verification.
  • Exported both converters via _LAZY_EXPORTS in pyrit/converter/__init__.py.
encoded = await DiffChainConverter(steps=3, seed=42).convert_async(prompt="tell me how to cut down a tree")
decoded = await DiffChainDecodeConverter().convert_async(prompt=encoded.output_text)

cc Roman Lutz (@romanlutz) Richard Lundeen (@richlundeen)

Copilot AI balanced review requested due to automatic review settings November 14, 2026 22:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unescaped chunk markers break decoding, and valid custom templates using dataset paths with deferred conditions fail validation because the template environment lacks the required filter.

2 open issues
What changed in this PR

Adds diff-chain encoding and decoding to PyRIT’s converter library, allowing text prompts to be represented as a placeholder file followed by sequential unified diff patches.

Changes:

  • Introduces configurable encoding, seeded patch ordering, and a companion decoder.
  • Adds lazy public exports and a default reconstruction template.
  • Adds round-trip, validation, registry, and determinism tests, plus usage examples.
File Description
tests/​unit/​converter/​test_seeded_converter_determinism.py Adds the encoder to shared determinism checks.
tests/​unit/​converter/​test_diff_chain_converter.py Tests round trips, templates, validation, and registry integration.
pyrit/​datasets/​converters/​diff_chain.yaml Defines the default reconstruction instructions.
pyrit/​converter/​diff_chain_converter.py Implements encoding, decoding, and diff validation.
pyrit/​converter/​__init__.py Exposes both converters through lazy imports.
doc/​code/​converters/​1_text_to_text_converters.py Adds an encoding and decoding example.
doc/​code/​converters/​1_text_to_text_converters.ipynb Adds the matching notebook example and output.
doc/​code/​converters/​0_converters.ipynb Lists both converters in the modality table.

Review effort: Balanced

Comment thread pyrit/converter/diff_chain_converter.py Resolved
Comment thread pyrit/converter/diff_chain_converter.py Resolved
@hannahwestra25 hannahwestra25 self-assigned this Nov 21, 2026
hannahwestra25 and others added 3 commits December 4, 2026 11:28
Co-Authored-By: Claude Sonnet <noreply@anthropic.com>
DiffChainConverter now does one transformation: it always emits the
initial PARTS file and the diff changesets. The include_instructions and
template options are removed; diff_chain.yaml is now a {{ prompt }}
wrapper to stack with PromptTemplateConverter. Tests drop the asyncio
marks, parametrize the validation and corruption cases, and cover the
stacked template. The filename docstring example now matches the
single-segment rule, and a findall result gets the cast the newer ty
from main requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The adversarial round-trip input again spells U+2028 and U+2029 as
escape sequences instead of raw characters, so editors cannot strip
them and silently drop that coverage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@hannahwestra25 hannahwestra25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing! I merged main, made DiffChainConverter encoding-only with diff_chain.yaml stacked via PromptTemplateConverter, and parametrized its tests.

This branch has not been deployed

No deployments
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.

3 participants