Skip to content

FIX: warn when chat targets ignore extra choices from n > 1 - #4749

Closed
Denzil Nelson (Denzil10) wants to merge 4 commits into
microsoft:mainfrom
Denzil10:fix/openai-chat-n-choices
Closed

Denzil Nelson (Denzil10) wants to merge 4 commits into
microsoft:mainfrom
Denzil10:fix/openai-chat-n-choices

Conversation

@Denzil10

Copy link
Copy Markdown

Description

Fixes #4742. OpenAIChatTarget(n=3) (and LiteLLMChatTarget) send n=3, are billed for three choices, and return only choices[0] with no indication that anything was dropped. This PR makes the drop visible instead of silent.

  • build_response_pieces_async (shared by both chat targets) now logs one warning when a response has more than one choice. It reports the count and the finish_reason of each ignored choice, so a dropped content_filter choice shows up, and says that the extra choices are still billed.
  • The n docstrings on OpenAIChatTarget and LiteLLMChatTarget now state that only the first choice is returned.
  • Tests: a helper-level test, an end-to-end test using the mocked ChatCompletion from the issue (3 choices, one content_filter), and a control showing a single-choice response does not warn. The end-to-end test fails on main (no warning) and passes here.

Why a warning, not "return every choice" or "reject n > 1"

The issue lists three options and no maintainer picked one, so this is the least invasive. I checked the other two:

  • Returning one piece per choice (as OpenAICompletionTarget does) breaks multi-turn use. build_text_chat_messages raises unless each message has exactly one piece, and the multimodal builder would merge alternative replies into a single assistant turn on replay. Tool calls from non-first choices would also put calls from different branches into one history entry.
  • Rejecting n > 1 would break behavior existing tests rely on, for example test_chat_tool_targets_accept_nested_settings_without_local_execution accepts n=2 when tools are not executed locally. The repo also moved toward warning over raising in similar cases (FIX: warn instead of raising on reasoning-model token truncation (Chat and Responses targets) #2166).

If maintainers prefer one of those, I am happy to rework this PR. Automatic tool execution requires n=1 is unchanged.

AI assistance: Claude Code helped trace the code paths and draft this change; I reviewed the diff and ran the tests.

Copilot AI balanced review requested due to automatic review settings November 29, 2026 06:58

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

Responses with a content-filtered first choice return early and bypass the unused-choice warning, leaving extra choices, their finish reasons, and their costs unreported.

1 open issue
What changed in this PR

This PR addresses #4742 by making discarded chat-completion choices visible without changing the existing first-choice-only behavior.

Changes:

  • Adds a shared warning reporting ignored choices, their finish reasons, and billing implications.
  • Clarifies n in the OpenAI and LiteLLM target documentation.
  • Adds mocked regression tests for multi-choice warnings and single-choice silence.
File Description
tests/​unit/​prompt_target/​target/​test_openai_chat_target.py Tests warning behavior through the target’s send path.
tests/​unit/​prompt_target/​target/​test_chat_completions_helpers.py Tests the shared warning and first-choice selection.
pyrit/​prompt_target/​openai/​openai_chat_target.py Documents how extra choices are handled.
pyrit/​prompt_target/​litellm_chat_target.py Documents the same first-choice-only behavior.
pyrit/​prompt_target/​common/​chat_completions_response_parser.py Adds the warning helper and invokes it during response construction.

Review effort: Balanced

Returns:
list[MessagePiece]: All constructed response pieces (may be empty).
"""
warn_unused_choices(response=response)
@romanlutz Roman Lutz (romanlutz) self-assigned this Dec 1, 2026
The content-filter early return in both chat targets bypasses
build_response_pieces_async, so a response whose first choice was
content-filtered dropped its extra choices silently - exactly the case
where the ignored choices may hold usable completions.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@romanlutz

Copy link
Copy Markdown
Contributor

Issue author said they want to tackle this themselves. Closing this PR. Please read issue descriptions.

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.

BUG: OpenAIChatTarget(n>1) requests n choices but keeps only choices[0]

3 participants