Repository navigation
RubyLLM2 W1-12: Accept RubyLLM 2.0's tool call and provider options interfaces on the 1.16 fork - #44
Conversation
Tool#call takes keyword arguments and a reserved tool_call: keyword, and Chat#execute_tool calls it that way. The 1.x positional Hash still works unless allow_positional_tool_arguments is false. Chat gains with_provider_options / provider_options, sharing @params with with_params / params. Co-authored-by: Sam Boland <sam.boland@appfolio.com>
|
🤖 Supernova Code Review — View trace — via automation "Code Review Opus 5.5 Medium" (run), installed by Sam Boland (sam.boland@appfolio.com) |
There was a problem hiding this comment.
I did an adversarial review of the tool-call and provider-options changes and left 3 inline findings in lib/ruby_llm/chat.rb:
- Line 474: tools that still override
call(args)the 1.16 way now gettool_callmixed into their argument Hash. With a strictexecute,super(args)then fails withunknown keyword: tool_call. With a**kwargsexecute,tool_callleaks into it. - Line 474:
**(args || {})raisesTypeErrorwhen the model sends arguments that parse to something other than a Hash, like an Array or a String. Before this PR,normalize_argsturned those into{}. - Line 163:
with_provider_optionsisn't delegated fromActiveRecord::ChatMethods, so records usingacts_as_chatcan't switch to it.
— via automation "Code Review Opus 5.5 Medium" (run), installed by Sam Boland (sam.boland@appfolio.com)
|
|
||
| RubyLLM.instrument('tool_call.ruby_llm', payload, config: @config) do |event| | ||
| result = tool.call(args) | ||
| result = tool.call(**(args || {}), tool_call:) |
There was a problem hiding this comment.
Tools that still override call(args) with the 1.16 signature now get tool_call mixed into their arguments, so "1.16 behaviour is unchanged by default" doesn't hold for them.
On Ruby 3, keywords passed to a method that declares no keywords are packed into one positional Hash. So def call(args) now receives { "query" => "x", tool_call: #<ToolCall> }. If that override then calls super(args), tool_arguments returns the Hash with :tool_call still in it. validate_keyword_arguments then reports unknown keyword: tool_call for a strict execute, or a **kwargs execute receives it, which goes against the "**kwargs does not receive it" contract. allow_positional_tool_arguments = true doesn't protect these tools.
Suggested fix: only pass the keyword form when the tool's call accepts keywords. For example, check tool.method(:call).parameters for :key/:keyrest and otherwise fall back to tool.call(args). Please also add a spec with a legacy def call(args) = super(args) override.
|
|
||
| RubyLLM.instrument('tool_call.ruby_llm', payload, config: @config) do |event| | ||
| result = tool.call(args) | ||
| result = tool.call(**(args || {}), tool_call:) |
There was a problem hiding this comment.
If a model sends a non-Hash argument payload, **(args || {}) now raises TypeError out of chat.ask, where it used to fail gracefully.
ToolCall#arguments is whatever JSON.parse returns (ChatCompletions::Tools#parse_tool_call_arguments, StreamAccumulator#tool_calls_from_stream). A model that emits "[]" or "\"x\"" produces an Array or a String, and ** on those raises TypeError: no implicit conversion of Array into Hash. Before this PR, tool.call(args) went through normalize_args, which has an explicit non-Hash fallback to {}. The tool then either ran or returned an Invalid tool arguments error the model could recover from.
Suggested fix: normalize first, e.g. kwargs = args.respond_to?(:to_hash) ? args.to_hash : {}, then call tool.call(**kwargs, tool_call:).
| end | ||
|
|
||
| # RubyLLM 2.0 name for with_params; shares @params so both names always agree. | ||
| def with_provider_options(provider_options) |
There was a problem hiding this comment.
with_provider_options isn't delegated from RubyLLM::ActiveRecord::ChatMethods, so moving acts_as_chat callers off with_params raises NoMethodError.
lib/ruby_llm/active_record/chat_methods.rb delegates with_params(...) to to_llm (around line 120), but this PR adds no matching with_provider_options there. Any Supernova code that calls with_params on a persisted chat record can't make the 2.0 move this PR is meant to enable.
Suggested fix: add the same delegation, plus a spec.
def with_provider_options(...)
to_llm.with_provider_options(...)
self
end|
Rulings on the staging bot's three inline findings. All three are right; please fix all three in this PR.
Verified and fine as is: the |
execute_tool passes keywords only when the tool's call accepts them (keyrest or a tool_call keyword) and otherwise passes the 1.16 positional Hash, so a missed override keeps working on the default config and raises when allow_positional_tool_arguments is false. Non-Hash arguments become an empty Hash instead of raising TypeError. The ActiveRecord chat delegates with_provider_options and provider_options, as 2.0 does. Co-authored-by: Sam Boland <sam.boland@appfolio.com>
|
Supernova has stopped updating this pull request's title and description. The description was edited outside Supernova after Supernova last wrote it, so later pushes will leave the title and description as they are instead of replacing those changes. To have Supernova keep them up to date again, turn automatic updates back on. You'll be asked to confirm first, because turning them back on replaces the current description with a newly generated one. — via Coder session (view) |
samuelboland
left a comment
There was a problem hiding this comment.
Verified: keyword and positional Tool#call paths, tool_call reserved and passed only to a declared keyword, legacy call(args) overrides kept on the positional path, non-Hash arguments normalized to {}, with_provider_options/provider_options on Chat and acts_as_chat records; specs cover each; CI green on 6b9d65a.
What this does
Lets the 1.16 fork accept the two interfaces RubyLLM 2.0 uses for tool calls and request options, so Supernova can move its code to them before the 2.0 bump. 1.16 behaviour is unchanged by default.
Tool calls
Tool#call(args = nil, tool_call: nil, **arguments)accepts keywords (the 2.0 form) as well as the 1.16 positional Hash. Symbol keys, String keys and a positional Hash give the same arguments toexecute.ArgumentError.tool_callis a reserved keyword. It reachesexecuteonly whenexecutedeclares atool_call:keyword (execute_accepts_tool_call?), and it is left out of keyword validation and the inferred schema.**kwargsdoes not receive it, matching 2.0.Chat#execute_toolcalls the tool throughcall_tool: arguments that are not a Hash (an Array or String fromJSON.parse) become{}, as before this PR. When the tool'scallaccepts keywords (the baseTool#call, or an override with**ortool_call:), it callstool.call(**kwargs, tool_call:), the 2.0 form. A tool that still overridescall(args)the 1.16 way gets the positional Hash, so it keeps working on the default config.allow_positional_tool_arguments(defaulttrue). Whenfalse, a positional Hash call raisesArgumentError("Tool#call takes keyword arguments; positional Hash arguments are removed in RubyLLM 2.0"). Supernova's test suite sets it tofalseto catch every remaining positional caller.Chat records:
acts_as_chatrecords delegatewith_provider_options(returns the record) andprovider_optionstoto_llm, as on 2.0.Provider options
Chat#with_provider_options(hash)sets@paramstohash.to_hand returns the chat;Chat#provider_optionsreads it. Like 2.0, it replaces rather than merges.with_paramsstill works and shares@params.Not changed: no provider or protocol code, so no VCR cassettes were re-recorded.
Files
lib/ruby_llm/tool.rb: the newcall,TOOL_CALL_KEYWORD,KEYWORD_PARAMETER_KINDS,execute_accepts_tool_call?,tool_arguments, andtool_callexcluded fromexecute_keyword_signature.lib/ruby_llm/chat.rb:with_provider_options,provider_options,call_toolandtool_call_accepts_keywords?used byexecute_tool.lib/ruby_llm/active_record/chat_methods.rb:with_provider_optionsandprovider_optionsdelegation.lib/ruby_llm/configuration.rb:allow_positional_tool_arguments.spec/ruby_llm/tool_spec.rb,spec/ruby_llm/chat_tools_spec.rb,spec/ruby_llm/chat_spec.rb,spec/ruby_llm/active_record/acts_as_spec.rb.Brief: RubyLLM2 W1-12 (prep wave on 1.16 before the 2.0 bump).