Fix multiline JSON extraction in exceptions helpers#1474
Merged
rlundeen2 merged 2 commits intomicrosoft:mainfrom Mar 16, 2026
Merged
Fix multiline JSON extraction in exceptions helpers#1474rlundeen2 merged 2 commits intomicrosoft:mainfrom
rlundeen2 merged 2 commits intomicrosoft:mainfrom
Conversation
rlundeen2
approved these changes
Mar 16, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves JSON payload extraction from mixed model output strings by replacing a regex-based approach (which fails on multiline JSON and can select nested fragments) with a json.JSONDecoder().raw_decode() scan, and adds a regression test for the multiline/nested case.
Changes:
- Replace regex JSON extraction with incremental decoding via
json.JSONDecoder().raw_decode(). - Ensure multiline JSON embedded in surrounding text is extracted correctly (and avoids nested-fragment false matches).
- Add a regression test for multiline object extraction.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pyrit/exceptions/exceptions_helpers.py |
Reworks extract_json_from_string() to scan and decode JSON objects/arrays instead of using regex. |
tests/unit/exceptions/test_exceptions_helpers.py |
Adds a regression test covering multiline JSON with a nested single-line object. |
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
|
Looks great, thanks for the PR @biefan! |
riyosha
pushed a commit
to riyosha/PyRIT
that referenced
this pull request
Mar 24, 2026
Co-authored-by: rlundeen2 <137218279+rlundeen2@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This updates
extract_json_from_string()to correctly extract multiline JSON payloads from larger strings.Problem
extract_json_from_string()currently relies on a regex:That has two problems for real model output:
.does not match newlines by default, so multiline JSON objects and arrays are not matched as a whole.In practice, this means helper code can extract the wrong JSON fragment from valid responses that include explanatory text plus a pretty-printed JSON body.
Fix
Replace the regex-based extraction with
json.JSONDecoder().raw_decode()scanning logic:{/[starting positionsThis preserves existing behavior for single-line JSON while fixing multiline extraction and avoiding nested-fragment false matches.
Tests
Added a regression test covering a multiline JSON object embedded in surrounding text.
Validation command:
Validation result: