Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions lib/ruby_llm/stream_accumulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ def accumulate_tool_calls(new_tool_calls) # rubocop:disable Metrics/PerceivedCom
new_tool_calls.each_value do |tool_call|
if tool_call.id
tool_call_id = tool_call.id.empty? ? SecureRandom.uuid : tool_call.id
tool_call_arguments = tool_call.arguments.empty? ? +'' : tool_call.arguments
tool_call_arguments = tool_call.arguments
if tool_call_arguments.nil? || (tool_call_arguments.respond_to?(:empty?) && tool_call_arguments.empty?)
tool_call_arguments = +''
end
@tool_calls[tool_call.id] = ToolCall.new(
id: tool_call_id,
name: tool_call.name,
Expand All @@ -88,7 +91,9 @@ def accumulate_tool_calls(new_tool_calls) # rubocop:disable Metrics/PerceivedCom
else
existing = @tool_calls[@latest_tool_call_id]
if existing
existing.arguments << tool_call.arguments
fragment = tool_call.arguments
fragment = '' if fragment.nil?
existing.arguments << fragment
if tool_call.thought_signature && existing.thought_signature.nil?
existing.thought_signature = tool_call.thought_signature
end
Expand Down
18 changes: 18 additions & 0 deletions spec/ruby_llm/stream_accumulator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RubyLLM::StreamAccumulator do
describe '#add' do
it 'handles tool call deltas that omit arguments' do
accumulator = described_class.new
tool_call = RubyLLM::ToolCall.new(id: 'call_1', name: 'weather', arguments: nil)
chunk = RubyLLM::Chunk.new(role: :assistant, content: nil, tool_calls: { 'call_1' => tool_call })

expect { accumulator.add(chunk) }.not_to raise_error

message = accumulator.to_message(nil)
expect(message.tool_calls['call_1'].arguments).to eq({})
end
end
end