Skip to content

FEAT: Bound OpenAIResponseTarget's tool loop, and end the turn when a tool's infrastructure fails (part 1) - #4101

Merged
Richard Lundeen (richlundeen) merged 4 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-1-tool-loop
Nov 9, 2026
Merged

Richard Lundeen (richlundeen) merged 4 commits into
microsoft:mainfrom
richlundeen:richlundeen-sandbox-stack-1-tool-loop

Conversation

@richlundeen

@richlundeen Richard Lundeen (richlundeen) commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part 1 of 5 of sandboxed code execution, which lets PyRIT test models that act by running code: their tool calls run in a sandbox, and a check program in the same sandbox scores what they did. This part needs no sandbox and is useful on its own. It makes OpenAIResponseTarget's tool loop safe to leave running, and lets a tool end the turn when its own infrastructure fails.

The stack, merged in order:

  1. FEAT: Bound OpenAIResponseTarget's tool loop, and end the turn when a tool's infrastructure fails (part 1) #4101 Bounded tool loop for OpenAIResponseTarget (this PR)
  2. FEAT: Declared limits, per-call timeouts and pre-run validation for SandboxClient (part 2) #4102 Sandbox interface, with Docker as the example adapter
  3. FEAT: SandboxRegistry, so targets and scorers share a sandbox by name (part 4) #4103 SandboxRegistry
  4. FEAT: SandboxToolProvider, so a model's tool calls run in a sandbox (part 3) #4104 SandboxToolProvider
  5. FEAT: SandboxCheckScorer, which scores a turn by what a check program finds in the sandbox (part 5) #4105 SandboxCheckScorer

OpenAIResponseTarget runs a model's tool calls in a loop until the model stops calling tools. That loop had no bound, and a tool whose infrastructure failed could only return an error to the model, asking it to fix a problem that isn't its own.

  • max_tool_rounds (default 10, matching the LiteLLM tool loop in FEAT: Add native Claude target and bounded tool execution #4021) bounds how many responses with tool calls one send runs. At the bound, the pending calls are answered as not run ({"error": "max_tool_rounds_exhausted"}), so the conversation's history stays valid, and the turn ends with an error response.
  • ToolInfrastructureError(message, invoked=...): a tool raises it when its infrastructure fails. The target answers every call in the round, ends the turn with an error response instead of asking the model to fix infrastructure, and records whether anything may have run (invoked). The model is told only that the tool's infrastructure failed; the error response, which is never replayed to the model, keeps the details.
  • ToolLoopStop and ToolLoopStopReason (pyrit.models): the error response's metadata says why the loop stopped (max_tool_rounds_exhausted or tool_infrastructure_error).
  • Malformed-argument results now include the validation message, so the model can correct its call.
  • The target's identifier records max_tool_rounds only when the target runs local tools, so other targets' identifiers don't change.
  • Docs: the Targets section of doc/code/framework.md.

Behavior change: OpenAIResponseTarget targets that run tools now stop after 10 tool rounds per send by default. Pass a larger max_tool_rounds for longer agentic loops.

Related: #4021 (native Claude target with a bounded tool loop, which can adopt ToolInfrastructureError and ToolLoopStop).

🤖 Generated with Claude Code

… tool's infrastructure fails

- max_tool_rounds (default 10) bounds the previously unbounded tool loop. When
  it is reached, pending calls are answered as not run and the turn ends with
  an error response carrying a ToolLoopStop reason.
- A tool raises ToolInfrastructureError(invoked=...) when its infrastructure
  fails; the turn ends once every call in the round has a result, and the
  model is told only that the tool's infrastructure failed.
- ToolLoopStop and ToolLoopStopReason are exported from pyrit.models.
- Malformed-argument results include the validation message.

Part 1 of 5 of sandboxed code execution.

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

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 are reported for the tool-execution limits and infrastructure-failure handling changes.

No open issues 🎉

What changed in this PR

This PR bounds OpenAIResponseTarget’s local tool execution and ends turns when tool infrastructure fails. It provides the first layer of the planned sandboxed code-execution stack while remaining useful independently.

Changes:

  • Limits local tool execution to 10 rounds by default, with a configurable max_tool_rounds.
  • Adds infrastructure-failure handling and structured stop metadata, answering pending calls to preserve valid history.
  • Includes argument-validation details in tool results and adds regression tests and framework documentation.
File Description
tests/​unit/​prompt_target/​target/​test_tool.py Checks validation messages in malformed-argument results.
tests/​unit/​prompt_target/​target/​test_openai_response_target_function_chaining.py Adds coverage for round limits, failures, identifiers, and replayable history.
pyrit/​prompt_target/​openai/​openai_response_target.py Bounds tool execution and handles infrastructure failures.
pyrit/​prompt_target/​common/​tool_provider.py Defines ToolInfrastructureError with invocation status.
pyrit/​prompt_target/​__init__.py Exports the new infrastructure exception.
pyrit/​models/​target/​tool_execution_metadata.py Defines tool-loop stop reasons and metadata.
pyrit/​models/​__init__.py Exports the new stop-metadata types.
doc/​code/​framework.md Documents bounded tool loops and infrastructure-failure handling.

Review effort: Balanced

@richlundeen Richard Lundeen (richlundeen) changed the title [BREAKING] FEAT: Bound OpenAIResponseTarget's tool loop, and end the turn when a tool's infrastructure fails [BREAKING] FEAT: Bound OpenAIResponseTarget's tool loop, and end the turn when a tool's infrastructure fails (part 1) Nov 9, 2026
@richlundeen Richard Lundeen (richlundeen) changed the title [BREAKING] FEAT: Bound OpenAIResponseTarget's tool loop, and end the turn when a tool's infrastructure fails (part 1) FEAT: Bound OpenAIResponseTarget's tool loop, and end the turn when a tool's infrastructure fails (part 1) Nov 9, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Automated review completed. No blocking concerns remain. This review records source coverage; it does not authorize merge.

@behnam-o Behnam (behnam-o) self-assigned this Nov 9, 2026
Richard Lundeen (richlundeen) added a commit to richlundeen/PyRIT that referenced this pull request Nov 9, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@richlundeen
Richard Lundeen (richlundeen) added this pull request to the merge queue Nov 9, 2026
Merged via the queue into microsoft:main with commit 694a3c0 Nov 9, 2026
59 checks passed
@richlundeen
Richard Lundeen (richlundeen) deleted the richlundeen-sandbox-stack-1-tool-loop branch November 9, 2026 19:46
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