Skip to content

Python: Preserve allowed tool choices in Chat Completions - #10285

Open
Atharva Vichare (atty57) wants to merge 1 commit into
microsoft:mainfrom
atty57:fix/10279-chat-completions-allowed-tools
Open

Atharva Vichare (atty57) wants to merge 1 commit into
microsoft:mainfrom
atty57:fix/10279-chat-completions-allowed-tools

Conversation

@atty57

@atty57 Atharva Vichare (atty57) commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

OpenAIChatCompletionClient drops allowed_tools and sends only "auto" or "required", allowing the model to choose any declared tool. Chat Completions supports an allowed-tools choice, so the request should preserve the caller's restriction.

Description & Review Guide

  • What are the major changes? Serialize allowed_tools using the Chat Completions {"type": "allowed_tools", "allowed_tools": {"mode": ..., "tools": [...]}} format, with nested function-name references. Replace the fallback tests with 12 request-level regressions that capture JSON from the real OpenAI SDK through a mock HTTP transport.
  • What is the impact of these changes? Both auto and required retain their allowed tool names in streaming and non-streaming requests. Empty lists are preserved rather than widened to unrestricted choices. The declared tool schemas, plain tool-choice modes, and required named-function behavior remain unchanged.
  • What do you want reviewers to focus on? The Chat Completions-specific payload nesting and preservation of the restriction across both response modes.

The regression cases failed against the previous implementation and pass with this change. The full OpenAI unit suite passes (816 tests; integration tests excluded), as do package formatting, lint, strict source Pyright, and all five test type checkers. Source and wheel distributions build successfully.

Related Issue

Fixes #10279

No competing open PR was found. The issue does not yet have a ready-for-implementation label or an explicit maintainer agreement comment; this PR presents the proposed fix for review, and the agreement checklist item remains unchecked.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: Codex assisted with issue analysis, implementation, regression tests, validation, and this PR description.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

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

The required-tool allowlist is cleared after the first tool executes, allowing follow-up requests to select excluded tools such as issue_refund.

1 open issue
What changed in this PR

Updates the Python Chat Completions client to serialize caller-provided allowed-tool restrictions instead of falling back to unrestricted tool-choice modes, addressing #10279.

Changes:

  • Uses the nested Chat Completions allowed_tools payload for auto and required, including empty lists.
  • Replaces fallback tests with 12 SDK request-level cases covering streaming and non-streaming requests.
File Description
python/​packages/​openai/​tests/​openai/​test_openai_chat_completion_client.py Adds mock-transport tests that verify serialized restrictions and declared tool names.
python/​packages/​openai/​agent_framework_openai/​_chat_completion_client.py Serializes allowed-tool choices instead of logging a warning and discarding them.

Review effort: Balanced

"the setting will be ignored. Use OpenAIChatClient (Responses API) instead."
)
run_options["tool_choice"] = mode
elif mode in ("auto", "required") and (allowed := tool_mode.get("allowed_tools")) is not None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The payload shape is right for Chat Completions. Two things to settle before it replaces the old fallback: the empty list, and subclasses that inherit the new shape.

What I checked.

  • The shape. The OpenAI SDK's ChatCompletionAllowedToolsParam requires mode ("auto"/"required") and tools, where each Chat Completions entry is {"type": "function", "function": {"name": ...}} (openai-python chat_completion_allowed_tools_param.py). That's what _chat_completion_client.py:850-857 builds. The Responses client (_chat_client.py:1826-1831) uses the flat {"type": "function", "name": ...} form, which is correct for that API, so the two clients now differ only where the APIs do.
  • The other tool_choice cases. required_function_name still takes precedence, allowed_tools=None still sends the bare mode, and validate_tool_mode (core/_types.py:5476-5485) already rejects allowed_tools with none.
  • The new test. It checks the actual request JSON through httpx.MockTransport, in both streaming and non-streaming modes, which is better than the old _prepare_options assertions.
  • git merge-tree against main is clean.

1. An empty allowed_tools is now sent as "tools": []. Before this PR, [] turned into the plain mode. Now {"mode": "required", "tools": []} asks the model to call one of zero tools, which can't be satisfied. I haven't checked whether the API rejects an empty tools array here, and the mock transport in the new test can't tell you either. Two safer options: map [] with auto to "none" (no tool may be called, which is what an empty allow-list means) and raise ChatClientInvalidRequestException for [] with required. Or validate [] away in validate_tool_mode so every client behaves the same. Either way, I'd remove the [] case from the "preserved" parametrization until the API's behaviour is confirmed against a live call.

2. FoundryLocalClient inherits the new payload. It subclasses RawOpenAIChatCompletionClient (foundry_local/_foundry_local_client.py:27,138) and doesn't override the tool_choice mapping in its _prepare_options (:286). Before this PR, Foundry Local (and any base_url OpenAI-compatible server) got "auto"/"required" plus a warning. Now it gets an allowed_tools object, which local and compatible servers may not accept. If Foundry Local doesn't support it, a small override that restores the old warn-and-degrade behaviour there would keep that client working. That would match how Ollama and Bedrock handle allowed_tools today: Ollama raises, and Bedrock warns and ignores it.

I didn't run the tests or make a live API call.

Reviewed with help from Claude Code.

This branch was successfully deployed

1 active deployment
github-app-auth — 58aeeca8 Deployed Nov 12, 2026 by atty57 via add_label #27642
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: OpenAIChatCompletionClient ignores allowed_tools and sends tool_choice: "auto", but Chat Completions supports allowed_tools

3 participants