Skip to content

MAINT: Deprecate n on chat and completion targets - #4793

Open
Akshay Hitesh Shah (AkshayShah03) wants to merge 2 commits into
microsoft:mainfrom
AkshayShah03:fix/chat-n-warning
Open

Akshay Hitesh Shah (AkshayShah03) wants to merge 2 commits into
microsoft:mainfrom
AkshayShah03:fix/chat-n-warning

Conversation

@AkshayShah03

@AkshayShah03 Akshay Hitesh Shah (AkshayShah03) commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #4742. Following the discussion there, this deprecates the n argument on the chat and completion targets for removal in 1.4.0, instead of only warning about it. Every API that offers n also takes the full conversation history, so sending once per sample, each in its own conversation, gets the same samples, and each one goes through PyRIT's normal converters, scoring, retries and content-filter handling.

  • OpenAIChatTarget, LiteLLMChatTarget and OpenAICompletionTarget emit a DeprecationWarning through print_deprecation_message(..., removed_in="1.4.0") whenever n is passed, including n=1, naming one send per sample, each in its own conversation, as the replacement. The warning points at the caller's line.
  • The logged warning from the first version of this PR now fires only for an n above 1 that arrives through extra_body_parameters (top level or nested extra_body). It is skipped when the n argument was passed, so the caller never gets both.
  • The n docstrings on all three targets say it is deprecated. OpenAICompletionTarget's says to send once per sample instead, since it does return every choice today.
  • Tests: pytest.warns(DeprecationWarning) for n=1 and n=3 on OpenAIChatTarget and OpenAICompletionTarget, and for LiteLLMChatTarget; no warning of either kind when n is unset; the logged warning for extra_body_parameters={"n": 2} and nested {"extra_body": {"n": 3}}, and not for n=1 or a nested override to 1; no logged warning when the n argument already warned. 9 of the 20 new cases fail on main; the rest check that nothing warns.

Rebased onto current main. The prompt_target, output, backend and scenario unit suites pass locally (11,325 tests).

Disclosure: this change and its tests were written with the help of an AI coding assistant (Claude).

Copilot AI balanced review requested due to automatic review settings December 2, 2026 02:28

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.

🟢 Approval recommended

No active review issues were identified for the change warning users about first-choice-only responses.

No open issues 🎉

What changed in this PR

Addresses #4742 by warning when PyRIT chat targets request multiple choices but return only the first, without changing response behavior.

Changes:

  • Adds a construction-time warning using shared choice-count lookup, including passthrough overrides.
  • Clarifies first-choice-only behavior in both targets’ documentation.
  • Adds warning coverage for OpenAI and LiteLLM targets.
File Description
tests/​unit/​prompt_target/​target/​test_openai_chat_target.py Tests warnings for defaults, multiple choices, and overrides.
tests/​unit/​prompt_target/​target/​test_litellm_chat_target.py Tests warning behavior for single and multiple choices.
pyrit/​prompt_target/​openai/​openai_chat_target.py Documents that only the first choice is returned.
pyrit/​prompt_target/​litellm_chat_target.py Documents first-choice-only behavior and the warning.
pyrit/​prompt_target/​common/​chat_completions_tool_loop.py Adds the shared warning and extracts choice-count lookup.

Review effort: Balanced

@richlundeen

Copy link
Copy Markdown
Contributor

Thanks Akshay Hitesh Shah (@AkshayShah03)! After some research we're going to deprecate n instead of only warning about it (reasoning in #4742). Could you turn this PR into the deprecation?

  1. In OpenAIChatTarget, LiteLLMChatTarget and OpenAICompletionTarget, emit a deprecation warning whenever n is passed. Warn for any value, including 1, because passing it will fail once it's removed. Use print_deprecation_message from pyrit/common/deprecation.py with removed_in="1.4.0", and name one send per sample, each in its own conversation, as the replacement.
  2. Keep your logged warning only for an n above 1 that arrives through extra_body_parameters. That isn't a deprecated argument, but it still discards the extra choices. Don't warn twice when the n argument already triggered the deprecation warning.
  3. Mark n as deprecated in the docstrings of all three targets. OpenAICompletionTarget does return every choice today, so its docstring should say to send once per sample instead.
  4. Tests: pytest.warns(DeprecationWarning) for n=1 and n=3, no warning when n is unset, the logged warning still firing for extra_body_parameters={"n": 2}, and the deprecation warning for OpenAICompletionTarget.
  5. Retitle to MAINT: Deprecate n on chat and completion targets.

@AkshayShah03 Akshay Hitesh Shah (AkshayShah03) changed the title FIX: Warn when a chat target requests n>1 choices but returns only the first MAINT: Deprecate n on chat and completion targets Dec 3, 2026
@AkshayShah03

Copy link
Copy Markdown
Contributor Author

Thanks Richard Lundeen (@richlundeen), done in a53ba07 (rebased onto main). n now raises a DeprecationWarning for removal in 1.4.0 on all three targets, the logged warning fires only for n > 1 from extra_body_parameters when the argument wasn't passed, the docstrings are updated, and the tests cover the cases you listed. I've updated the title and description to match.

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.

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

3 participants