Skip to content

Support removing tools #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5c7a658
229: Support removing tools
tpaulshippy Jun 11, 2025
6a9b6d5
229: Add to docs
tpaulshippy Jun 11, 2025
5a57d23
Merge branch 'main' into issue-229
tpaulshippy Jun 11, 2025
5cc1176
Merge branch 'main' into issue-229
crmne Jul 16, 2025
fa6efec
Resolve rubocop offenses
tpaulshippy Jun 11, 2025
90a27cd
Merge branch 'main' into issue-229
tpaulshippy Jul 20, 2025
06a1d18
Remove unused method accidentally added during merge
tpaulshippy Jul 20, 2025
d47be42
Add integration test as suggested
tpaulshippy Jul 20, 2025
87bdeb4
Remove extra spacing
tpaulshippy Jul 20, 2025
09ccf35
Merge branch 'main' into issue-229
tpaulshippy Aug 3, 2025
c7198ec
Switch default image generation model to gpt-image-1 (#321)
tpaulshippy Aug 3, 2025
eeab34b
Add log_stream_debug configuration option
crmne Aug 6, 2025
4837ee4
Updated models. Yes, Opus 4.1 and GPT-OSS are here!
crmne Aug 6, 2025
9702c4e
Add on_tool_result callback and fix Rails callback chaining
crmne Aug 6, 2025
80bc774
Refactor providers from modules to classes
crmne Aug 6, 2025
55335c3
Fix JRuby compatibility issue with ErrorMiddleware initialization
crmne Aug 6, 2025
235ad69
Fix JRuby compatibility with optional hash argument
crmne Aug 6, 2025
6e10bf7
Fix Anthropic models incorrectly reporting structured output support
crmne Aug 6, 2025
1b31780
Add halt mechanism for tools to stop conversation continuation
crmne Aug 6, 2025
cdb2921
Improve halt documentation with clearer examples and flow explanation
crmne Aug 6, 2025
a24f472
Simplify halt documentation to prevent confusion
crmne Aug 7, 2025
0876d45
Skip providers that don't support function calling
tpaulshippy Aug 7, 2025
74822a5
Merge branch 'main' into issue-229
tpaulshippy Aug 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/guides/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,46 @@ puts response.content
{: .warning }
Ensure the model you select supports function calling/tools. Check model capabilities using `RubyLLM.models.find('your-model-id').supports_functions?`. Attempting to use `with_tool` on an unsupported model will raise `RubyLLM::UnsupportedFunctionsError`.

## Removing Tools

Each tool definition adds to the input tokens for every request, so it is often a good idea to remove tools that are not needed on a given request. You can remove tools from a `Chat` instance using these methods:

### Remove a Single Tool

Use `without_tool` to remove a specific tool by passing either the tool class or instance:

```ruby
# Remove by class
chat.without_tool(Weather)

# Or by instance
chat.without_tool(weather_tool)
```

### Remove Multiple Tools

Use `without_tools` to remove multiple tools at once:

```ruby
chat.without_tools(Weather, AnotherTool, some_tool_instance)
```

### Clear All Tools

To remove all tools from the chat instance:

```ruby
chat.clear_tools
```

All these methods return the `Chat` instance, allowing for method chaining:

```ruby
chat.clear_tools
.with_tool(Weather.new)
.ask("What's the weather?")
```

## The Tool Execution Flow

When you `ask` a question that the model determines requires a tool:
Expand Down
28 changes: 26 additions & 2 deletions lib/ruby_llm/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def with_tool(tool)
raise UnsupportedFunctionsError, "Model #{@model.id} doesn't support function calling"
end

tool_instance = tool.is_a?(Class) ? tool.new : tool
@tools[tool_instance.name.to_sym] = tool_instance
tool_instance = instantiate_tool(tool)
@tools[tool_name(tool_instance)] = tool_instance
self
end

Expand All @@ -64,6 +64,22 @@ def with_tools(*tools)
self
end

def without_tool(tool)
tool_instance = instantiate_tool(tool)
@tools.delete(tool_name(tool_instance))
self
end

def without_tools(*tools)
tools.each { |tool| without_tool(tool) }
self
end

def clear_tools
@tools.clear
self
end

def with_model(model_id, provider: nil, assume_exists: false)
@model, @provider = Models.resolve(model_id, provider:, assume_exists:, config: @config)
@connection = @provider.connection
Expand Down Expand Up @@ -212,6 +228,14 @@ def execute_tool(tool_call)
tool.call(args)
end

def tool_name(tool)
tool.name.to_sym
end

def instantiate_tool(tool)
tool.is_a?(Class) ? tool.new : tool
end

def instance_variables
super - %i[@connection @config]
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading