Repository navigation
Python: Preserve allowed tool choices in Chat Completions - #10285
Atharva Vichare (atty57) wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 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_toolspayload forautoandrequired, 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: |
Muhammad Abdullah Butt (1AbdullahButt)
left a comment
There was a problem hiding this comment.
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
ChatCompletionAllowedToolsParamrequiresmode("auto"/"required") andtools, where each Chat Completions entry is{"type": "function", "function": {"name": ...}}(openai-pythonchat_completion_allowed_tools_param.py). That's what_chat_completion_client.py:850-857builds. 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_choicecases.required_function_namestill takes precedence,allowed_tools=Nonestill sends the bare mode, andvalidate_tool_mode(core/_types.py:5476-5485) already rejectsallowed_toolswithnone. - 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_optionsassertions. git merge-treeagainstmainis 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.

Motivation & Context
OpenAIChatCompletionClientdropsallowed_toolsand 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
allowed_toolsusing 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.autoandrequiredretain 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.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-implementationlabel or an explicit maintainer agreement comment; this PR presents the proposed fix for review, and the agreement checklist item remains unchecked.AI Assistance
AI assistance details: Codex assisted with issue analysis, implementation, regression tests, validation, and this PR description.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.