Skip to content

FIX: Report adversarial chat refusals inside the JSON reply as refusals - #4827

Open
Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:agents/fix-adversarial-json-refusal-handling
Open

Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:agents/fix-adversarial-json-refusal-handling

Conversation

@behnam-o

Copy link
Copy Markdown
Collaborator

Description

An adversarial chat that declines an objective can reply with valid JSON whose next_message is empty and whose rationale explains the refusal. The shared adversarial-reply parser treated that as malformed JSON: it retried the turn up to RETRY_MAX_NUM_ATTEMPTS times and then errored the objective with InvalidJsonException, instead of recording an adversarial-chat refusal the way it already does for provider-flagged refusals (#2443, #2551, #2918).

  • _parse_adversarial_reply is shared by Red Teaming, Crescendo, TAP, PAIR, and simulated conversations. It now raises AdversarialChatRefusedException with the rationale when next_message is blank or null and rationale is set. A blank reply with no explanation is still InvalidJsonException, as introduced in MAINT: Improving JSON parsing logic to raise JSONException on empty required strings #2567.
  • pyrit_json_retry retries a refusal on a clean history only within the first three attempts, because a model sometimes complies on a fresh attempt. Provider-flagged refusals, which previously got a single attempt, follow the same rule. Rolled-back turns are recorded with ConversationRetryReason.REFUSAL.
  • Crescendo now ends UNDETERMINED with adversarial_chat_refused, like Red Teaming. If the objective target was already reached, it ends as a truncated run instead. TAP and PAIR prune a refused branch instead of logging a JSON or execution error, and end UNDETERMINED with adversarial_chat_refused when no branch reached the objective target.
  • Refusal retries no longer carry InvalidJsonException's status 500, so they stop showing up as server errors in the GUI's overload summary.

Before / after. The adversarial chat always replies {"next_message": "", "rationale": "I won't write an attack prompt for this objective.", "last_response_summary": ""}, with RETRY_MAX_NUM_ATTEMPTS at its default of 10. The objective target is never called in any of these runs.

Attack Before After
Red Teaming, Crescendo 10 adversarial calls, raises InvalidJsonException, 10 retry events with status 500 3 adversarial calls, UNDETERMINED / adversarial_chat_refused with the rationale, 3 retry events with status 400
TAP (2 branches) 20 adversarial calls, ERROR ("JSON parsing error"), 20 retry events with status 500 6 adversarial calls, UNDETERMINED / adversarial_chat_refused, 6 retry events with status 400

🤖 Generated with Claude Code

An adversarial chat that declines an objective can return valid JSON with a
blank next_message and its reason in rationale. The shared parser treated that
as invalid JSON, so the turn was retried up to RETRY_MAX_NUM_ATTEMPTS times and
the objective ended in an InvalidJsonException error.

- Raise AdversarialChatRefusedException for a blank or null next_message with a
  rationale; a blank reply without one is still invalid JSON.
- Retry refusals in pyrit_json_retry only within the first three attempts, and
  record refusal rollbacks with their own ConversationRetryReason.
- End Crescendo UNDETERMINED / adversarial_chat_refused like Red Teaming, and let
  TAP and PAIR prune refused branches instead of reporting a JSON or execution
  error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 23:57

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

Active issues can cause Red Teaming to mark an untested objective complete and Crescendo to return a response mismatched with its score and trimmed conversation.

2 open issues
What changed in this PR

This PR distinguishes adversarial-chat refusals from malformed JSON, preserving refusal outcomes across PyRIT’s attack workflows.

Changes:

  • Recognizes blank or null replies with a rationale as refusals.
  • Limits refusal retries and records their rollback reason.
  • Updates attack outcomes, regression coverage, and resiliency documentation.
File Description
tests/​unit/​prompt_normalizer/​test_json_retry.py Tests refusal retries and rollback reasons.
tests/​unit/​executor/​attack/​multi_turn/​test_tree_of_attacks.py Tests refused branches and outcome precedence.
tests/​unit/​executor/​attack/​multi_turn/​test_crescendo_resilience.py Tests truncation and preparation failures.
tests/​unit/​executor/​attack/​multi_turn/​test_adversarial_chat_refusal.py Adds cross-attack refusal regressions.
tests/​unit/​executor/​attack/​component/​test_adversarial_conversation_manager.py Tests refusal parsing and retry behavior.
tests/​unit/​exceptions/​test_exceptions.py Tests refusal retry limits.
pyrit/​prompt_normalizer/​json_retry.py Tracks refusal-specific rollback reasons.
pyrit/​models/​messages/​conversation_retry.py Adds the refusal retry reason.
pyrit/​executor/​attack/​multi_turn/​tree_of_attacks.py Prunes refused branches and classifies outcomes.
pyrit/​executor/​attack/​multi_turn/​simulated_conversation.py Clarifies refusal retries in documentation.
pyrit/​executor/​attack/​multi_turn/​crescendo.py Handles refusals as preparation failures or truncation.
pyrit/​executor/​attack/​component/​adversarial_conversation_manager.py Recognizes refusals inside JSON replies.
pyrit/​exceptions/​exception_classes.py Adds bounded refusal retries.
doc/​code/​setup/​2_resiliency.py Documents refusal retry behavior.
doc/​code/​setup/​2_resiliency.ipynb Mirrors the resiliency documentation update.

Review effort: Balanced

Comment on lines +372 to +373
if isinstance(rationale, str) and rationale.strip():
raise AdversarialChatRefusedException(message=rationale)
Comment on lines +528 to +529
if context.last_response is not None:
return self._create_attack_result(
@behnam-o
Behnam (behnam-o) marked this pull request as ready for review December 4, 2026 00:07

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.

2 participants