Skip to content

fix(agent): apply configured tool_choice to model requests#32518

Open
0xDevNinja wants to merge 1 commit into
anomalyco:devfrom
0xDevNinja:wire-agent-tool-choice
Open

fix(agent): apply configured tool_choice to model requests#32518
0xDevNinja wants to merge 1 commit into
anomalyco:devfrom
0xDevNinja:wire-agent-tool-choice

Conversation

@0xDevNinja

Copy link
Copy Markdown

Issue for this PR

Closes #32465

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Setting tool_choice on an agent in opencode.json had no effect - the key was accepted without error and silently dropped, so the request always sent the provider default (effectively tool_choice: auto). For models that don't reliably emit tool calls under auto, this made agentic sessions impossible: the model was never asked to commit to a tool call.

Root cause: the v1 agent schema (packages/core/src/v1/config/agent.ts) uses StructWithRest, and its normalize() moves any key not listed in KNOWN_KEYS into options. tool_choice was neither a declared field nor in KNOWN_KEYS, so it ended up buried in options and never reached the agent builder.

Changes:

  • Declare tool_choice (auto | required | none) on the v1 agent schema and add it to KNOWN_KEYS so it survives normalize.
  • Carry it onto the runtime agent as toolChoice in the agent builder (same pattern as temperature/top_p).
  • Pass it into the chat request in session/prompt.ts. Structured output (json_schema) still forces required as before; otherwise the agent's configured value is used.
  • Document the option in agents.mdx.

The request plumbing for toolChoice already existed (session/llm.ts, native-request.ts); this just connects the config to it.

How did you verify your code works?

  • Added a test in packages/opencode/test/agent/agent.test.ts asserting agent.build.tool_choice: "required" in config resolves to toolChoice === "required" on the built agent.
  • bun test test/agent/agent.test.ts test/config/config.test.ts -> 138 pass.
  • bun typecheck passes for both core and opencode.

Screenshots / recordings

N/A.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The agent tool_choice option was accepted in config but silently dropped:
the v1 agent schema pushed any unknown key into options during normalize, so
it never reached the agent builder, and the request always sent the default
(effectively tool_choice: auto). Models that don't reliably emit tool calls
under auto could never be forced into a tool call.

Add tool_choice to the v1 agent schema (and KNOWN_KEYS so it survives
normalize), carry it onto the runtime agent as toolChoice, and pass it into
the chat request (json_schema structured output still forces required).

Closes anomalyco#32465
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.

opencode Agent config is silently ignored — opencode always sends (1.17.7)

1 participant