Module: ActiveAgent::Providers::ToolChoiceClearing

Extended by:
ActiveSupport::Concern
Included in:
BaseProvider, OpenAI::ChatProvider, OpenAI::ResponsesProvider
Defined in:
lib/active_agent/providers/concerns/tool_choice_clearing.rb

Overview

Provides unified logic for clearing tool_choice after tool execution.

When a tool_choice is set to “required” or to a specific tool name, it forces the model to use that tool. After the tool is executed, we need to clear the tool_choice to prevent infinite loops where the model keeps calling the same tool repeatedly.

Each provider implements:

  • ‘extract_used_function_names`: Returns array of tool names that have been called

  • ‘tool_choice_forces_required?`: Returns true if tool_choice forces any tool use

  • ‘tool_choice_forces_specific?`: Returns [true, name] if tool_choice forces specific tool

Instance Method Summary collapse

Instance Method Details

#prepare_prompt_request_toolsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_agent/providers/concerns/tool_choice_clearing.rb', line 20

def prepare_prompt_request_tools
  return unless request.tool_choice

  functions_used = extract_used_function_names

  # Clear if forcing required and any tool was used
  if tool_choice_forces_required? && functions_used.any?
    request.tool_choice = nil
    return
  end

  # Clear if forcing specific tool and that tool was used
  forces_specific, tool_name = tool_choice_forces_specific?
  if forces_specific && tool_name && functions_used.include?(tool_name)
    request.tool_choice = nil
  end
end