Skip to content

workers-ai-provider: tool_choice not forwarded to binding.run() inputs #404

@tomfuertes

Description

@tomfuertes

Package: workers-ai-provider v3.1.1

Bug

getArgs() correctly computes tool_choice via prepareToolsAndToolChoice(), but buildRunInputs() only forwards specific fields - silently dropping tool_choice:

// buildRunInputs returns:
{
  max_tokens: args.max_tokens,
  messages: finalMessages,
  temperature: args.temperature,
  tools: args.tools,       // included
  top_p: args.top_p,
  // tool_choice: args.tool_choice  <- MISSING
}

Impact

Models that require explicit tool_choice: "auto" to call tools (e.g. @cf/mistralai/mistral-small-3.1-24b-instruct) silently skip tool calls - the model acknowledges it should use tools in its text response but never calls them. @cf/zai-org/glm-4.7-flash was unaffected because it defaults to using tools when provided.

Workaround

Shim the binding to inject tool_choice: "auto" when tools are present:

const shimmedBinding = {
  run: (model, inputs, options) =>
    env.AI.run(model, inputs?.tools ? { ...inputs, tool_choice: "auto" } : inputs, options)
};
const provider = createWorkersAI({ binding: shimmedBinding });

Fix

One line in buildRunInputs in workersai-chat-language-model.ts:

tools: args.tools,
tool_choice: args.tool_choice,  // add this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions