Skip to content

providers: support tool_choice force-a-specific-tool (named function) form - #297

Merged
FMXExpress merged 3 commits into
mainfrom
claude/tool-choice-named
Jun 16, 2026
Merged

FMXExpress merged 3 commits into
mainfrom
claude/tool-choice-named

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

What

Adds support for the force-a-specific-tool form of tool_choice (OpenAI's {"type":"function","function":{"name":...}}). It needed needed doing — but the original "minor, just OpenAI.pas" framing was incomplete: the named form was dropped in two places, so a provider-only change would've been a no-op.

Where it was dropped

  1. Gateway /v1/responses parse — Req.GetStr('tool_choice') returns '' for the object form, so it fell into the "drop + debug log" branch and never reached the provider.
  2. OpenAI request builder — only emitted auto/none/required.

(Note: /v1/chat/completions doesn't forward tool_choice at all — it runs tools server-side — so this is scoped to the /v1/responses client-tools passthrough, which is where it matters.)

The fix (end-to-end)

Convention: any non-empty TChatOptions.ToolChoice that isn't a keyword (auto/none/required) is a tool name to force. Each provider emits its own native shape:

  • Gateway: parse the object-form tool_choice, extract function.name, carry it in ToolChoice (provider-agnostic) — frees the ChildObject wrappers correctly.
  • OpenAI builder: {"type":"function","function":{"name":<n>}}.
  • Anthropic builder: {"type":"tool","name":<n>} — included for parity, otherwise a forwarded name would silently fall back to auto.
  • Gemini: unchanged (it ignores tool_choice entirely today — documented in the field comment).

TChatOptions.ToolChoice's doc comment now spells out the full contract.

Tests

New tool_choice_tests (in make test) pins, for both builders: keyword form stays a string / maps to any, the named form emits the object shape carrying the tool name, and empty omits the field. make test-openai-server-tools + test-anthropic-server-tools still pass; full binary builds clean.

https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6


Generated by Claude Code

… form

The named tool_choice -- {"type":"function","function":{"name":...}} on
OpenAI -- was dropped at two points: the /v1/responses gateway parse
(GetStr returns '' for the object form) and the OpenAI request builder
(only auto/none/required emitted). So a Responses client couldn't force a
specific tool.

Convention: any non-empty TChatOptions.ToolChoice that isn't a keyword
(auto/none/required) is a tool NAME to force. Wired end-to-end:
- Gateway /v1/responses: parse the object form, extract function.name into
  ToolChoice (provider-agnostic).
- OpenAI builder: emit {"type":"function","function":{"name":<n>}}.
- Anthropic builder: emit {"type":"tool","name":<n>} (parity -- prevents
  a silent fall-back to auto when the gateway forwards a name).
- Gemini still ignores tool_choice (unchanged; documented).

New tool_choice_tests pins keyword vs named emission for both builders.
Builds clean; existing openai/anthropic server-tool tests still pass.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3cfa7d7e61

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +4935 to +4938
FnObjIn := TCObjIn.ChildObject('function');
if FnObjIn <> nil then
try
ForcedFn := Trim(FnObjIn.GetStr('name', ''));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle Responses tool_choice names at top level

In the /v1/responses passthrough, this only extracts a nested Chat Completions-style function.name, but Responses clients force a function with {"type":"function","name":"get_weather"}; the same handler parses function tools from top-level tools[].name just above. For that standard request shape, ForcedFn stays empty, the branch drops tool_choice, and the provider falls back to auto, so clients still cannot force a specific tool through /v1/responses.

Useful? React with 👍 / 👎.

claude added 2 commits June 16, 2026 14:51
Codex P2 on #297: the /v1/responses tool_choice parse only read the nested
Chat-Completions function.name, but Responses clients force a tool with the
flat {"type":"function","name":"X"} shape (top-level name -- the same
shape tools[] entries use). For that standard request the name stayed
empty and tool_choice was dropped, so clients still couldn't force a tool.

Read the top-level name first, fall back to the nested function.name.
…hapes

Pull the /v1/responses tool_choice parse out of HandleResponses into an
exposed ResolveResponsesToolChoice(Req) so it can be tested directly (same
pattern as IsRestrictedFsPath / AccumulateGatewayStatsRaw). HandleResponses
now calls it; behaviour unchanged.

New responses_tool_choice_tests pins: absent -> ''; keyword auto/none/
required (case-folded); bare non-keyword string dropped; Responses flat
top-level name; Chat-Completions nested function.name; top-level preferred
when both present; object with no name dropped. (PasClaw.JSON moved to the
interface uses so the param type is visible; removed the now-redundant
implementation-uses entry.)
@FMXExpress
FMXExpress merged commit 619c156 into main Jun 16, 2026
FMXExpress pushed a commit that referenced this pull request Jun 16, 2026
Trivial Makefile conflict in the .PHONY list and the aggregate
`test:` target -- main added test-tool-choice +
test-responses-tool-choice from PR #297 (jaen's tool_choice work);
this branch added test-agents-md. Both sets kept.

No code conflicts; binary + test-agents-md still build clean.
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
The README + docs/changelog.md last entry was 2026-06-13 (FMXExpress#244). Five
substantial PRs (FMXExpress#314 FMXExpress#315 FMXExpress#316 FMXExpress#317 FMXExpress#318) and ten more feature PRs
between them and that entry shipped without changelog entries. This
brings both files current through today.

Twenty new entries (newest first, matching the existing style):

  2026-06-21
    FMXExpress#318  Relay pull-worker provider (queue + provider + /v1/relay/* HTTP)

  2026-06-20
    FMXExpress#317  pasclaw plan + build PLAN.md pickup + build --goal + cog mode
    FMXExpress#316  Cloudflare AI Gateway provider catalog rows
    FMXExpress#315  MCP progressive disclosure + tool_search
    FMXExpress#314  Stock defaults adopt lean-edit shape; fs_grep split-gate

  2026-06-18
    FMXExpress#312  Web UI workspace.zip export + import
    FMXExpress#310  Model-callable cron tool
    FMXExpress#309  FTS5 snippet window widened 24 -> 60
    FMXExpress#307  Windows cmd.exe quoting fix

  2026-06-17
    FMXExpress#306  cog-build honours profile=security sandbox
    FMXExpress#304  cog-build default profile = max-build
    FMXExpress#303  cog-build list[Path] outputs + Secret inputs + -q

  2026-06-16
    FMXExpress#302  cog-build optional workspace_in
    FMXExpress#300  pasclaw build cmd + cog-build/ predictor (parent feature)
    FMXExpress#299  Checkpoints zpaq backend + /redo
    FMXExpress#298  AGENTS.md + pasclaw init + /init
    FMXExpress#297  tool_choice force-a-specific-tool form
    FMXExpress#296  Delphi ExtraEnv injection fix
    FMXExpress#295  KB native PDF ingest

  2026-06-15
    FMXExpress#292  pasclaw profile diff + bench

Skipped per the changelog's stated convention ("Bug fixes and review
follow-ups are in the full git log"):

  FMXExpress#293  cmd/profile dcc64 fix
  FMXExpress#294  skills_manage gemini empty-enum fix
  FMXExpress#301  cmd.build dcc64 fix
  FMXExpress#311  cron dcc64 fix

Files updated identically (the docs/changelog.md note at the top says
"This file mirrors the changelog section at the top of the root
README.md"):

  M README.md           20 entries + 20 link refs added
  M docs/changelog.md   same

The link-ref block at the bottom of each file gained 20 [#NNN] entries
keeping the existing one-per-line / sorted-numerically format.
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.

2 participants