fix(agent): apply configured tool_choice to model requests#32518
Open
0xDevNinja wants to merge 1 commit into
Open
fix(agent): apply configured tool_choice to model requests#325180xDevNinja wants to merge 1 commit into
0xDevNinja wants to merge 1 commit into
Conversation
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
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #32465
Type of change
What does this PR do?
Setting
tool_choiceon an agent inopencode.jsonhad no effect - the key was accepted without error and silently dropped, so the request always sent the provider default (effectivelytool_choice: auto). For models that don't reliably emit tool calls underauto, 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) usesStructWithRest, and itsnormalize()moves any key not listed inKNOWN_KEYSintooptions.tool_choicewas neither a declared field nor inKNOWN_KEYS, so it ended up buried inoptionsand never reached the agent builder.Changes:
tool_choice(auto|required|none) on the v1 agent schema and add it toKNOWN_KEYSso it survivesnormalize.toolChoicein the agent builder (same pattern astemperature/top_p).session/prompt.ts. Structured output (json_schema) still forcesrequiredas before; otherwise the agent's configured value is used.agents.mdx.The request plumbing for
toolChoicealready existed (session/llm.ts,native-request.ts); this just connects the config to it.How did you verify your code works?
packages/opencode/test/agent/agent.test.tsassertingagent.build.tool_choice: "required"in config resolves totoolChoice === "required"on the built agent.bun test test/agent/agent.test.ts test/config/config.test.ts-> 138 pass.bun typecheckpasses for bothcoreandopencode.Screenshots / recordings
N/A.
Checklist