Skip to content

FEAT: Run local tools with OpenAIChatTarget - #4595

Merged
Adrian Gavrila (adrian-gavrila) merged 4 commits into
microsoft:mainfrom
adrian-gavrila:adrian-gavrila-openai-chat-tool-loop
Nov 24, 2026
Merged

Adrian Gavrila (adrian-gavrila) merged 4 commits into
microsoft:mainfrom
adrian-gavrila:adrian-gavrila-openai-chat-tool-loop

Conversation

@adrian-gavrila

Copy link
Copy Markdown
Contributor

Description

OpenAIChatTarget can now run tools on the host through the same bounded tool loop that OpenAIResponseTarget and LiteLLMChatTarget use (#4021). Chat Completions deployments can then be used wherever a target that runs local tools is needed, without registering them again as a LiteLLM target.

  • OpenAIChatTarget takes tools, tool_providers, execute_tools and max_tool_rounds, with the same meaning and checks as on LiteLLMChatTarget. Running tools automatically requires n=1.
  • Each completion is retried and rate-limited on its own, as in OpenAIResponseTarget, so retrying a request in a later round never runs earlier tools again. A send without tools makes the same single request as before, and the target's identifier doesn't change.
  • To keep one implementation, so a tool behaves the same on both targets, LiteLLM's Chat Completions tool code moves to pyrit/prompt_target/common/chat_completions_tool_loop.py, which both targets now use. That code builds tool definitions, reads calls from responses, checks for conflicts with extra_body_parameters, and opens provider scopes around the loop. LiteLLMChatTarget and ClaudeChatTarget behave as before.
  • Registry copies of OpenAI targets with tools now work. OpenAITarget.get_reconstruction_parameters put the identifier's tool descriptions in place of the real Tool and provider objects, so copying an OpenAIResponseTarget that had tools failed. Copies now keep their tools, and a copy of a target without tools can add them.
  • Capability detection doesn't discover or run the chat target's tools, as it already doesn't for the other two targets.
@tool
async def add_one_async(*, value: int) -> int:
    """Add one to a number."""
    return value + 1


target = OpenAIChatTarget(tools=[add_one_async], max_tool_rounds=2)

This also edits litellm_chat_target.py, as #4584 does. The two changes don't overlap and merge without conflicts.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 21:09

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

Nested extra_body overrides bypass validation, allowing extra billed completions to be silently discarded and local tool definitions to be replaced without name-conflict checks.

2 open issues
What changed in this PR

Adds local tool execution to OpenAIChatTarget, extending the bounded tool loop introduced for Responses and LiteLLM targets to Chat Completions deployments.

Changes:

  • Shares tool discovery, validation, and bounded execution between OpenAI Chat and LiteLLM, with OpenAI retries and rate limiting applied per completion.
  • Preserves executable tools during OpenAI target reconstruction and suppresses tools during capability probes.
  • Adds regression coverage and documents local tool configuration.
File Description
tests/​unit/​prompt_target/​target/​test_tool.py Tests copying OpenAI targets with tools and providers.
tests/​unit/​prompt_target/​target/​test_runtime_temperature.py Updates retry mocks for the per-completion request method.
tests/​unit/​prompt_target/​target/​test_openai_chat_target.py Covers tool execution, history replay, retries, and probe suppression.
pyrit/​prompt_target/​openai/​openai_target.py Keeps real tool objects in reconstruction parameters.
pyrit/​prompt_target/​openai/​openai_chat_target.py Adds tool configuration and bounded local execution.
pyrit/​prompt_target/​litellm_chat_target.py Delegates tool-loop handling to shared helpers.
pyrit/​prompt_target/​common/​discover_target_capabilities.py Suppresses OpenAI Chat tools during capability probes.
pyrit/​prompt_target/​common/​chat_completions_tool_loop.py Extracts shared Chat Completions tool-loop helpers.
doc/​code/​targets/​claude_chat_target.md Documents OpenAI Chat as another shared-loop target.
doc/​code/​targets/​1_openai_chat_target.py Adds local tool guidance and an example.
doc/​code/​targets/​1_openai_chat_target.ipynb Mirrors the local tool documentation in the notebook.

Review effort: Balanced

@behnam-o Behnam (behnam-o) self-assigned this Nov 23, 2026

@behnam-o Behnam (behnam-o) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you! Looks good. A few non-blocking notes below.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@adrian-gavrila
Adrian Gavrila (adrian-gavrila) removed this pull request from the merge queue due to a manual request Nov 24, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Merged via the queue into microsoft:main with commit 97d2935 Nov 24, 2026
66 checks passed
@adrian-gavrila
Adrian Gavrila (adrian-gavrila) deleted the adrian-gavrila-openai-chat-tool-loop branch November 24, 2026 19:28
Behnam (behnam-o) pushed a commit to behnam-o/PyRIT that referenced this pull request Nov 24, 2026
- Merge main, including microsoft#4595 (OpenAIChatTarget runs local tools); tool
  reporting moves into the shared Chat Completions tool-loop mixin.
- Replace the stand-in class list with a declared RUNS_TOOLS_LOCALLY flag
  on targets, so a tool-running OpenAIChatTarget can stand in.
- Keep max_attempts out of the seed's identity: it is now only a runtime
  argument of generate_simulated_conversation_async (default 5).
- Record a preparation failure when every stand-in reply is empty.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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