Skip to content

Commit 2d0a2c5

Browse files
committed
Handle empty string arguments in tool calls and add tests for multi-turn streaming conversations without parameters
Fixes #98: Streaming with Gemini + Tools causes JSON parsing error when no tool call arguments are given
1 parent c9b0e01 commit 2d0a2c5

12 files changed

+2166
-268
lines changed

lib/ruby_llm/stream_accumulator.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ def to_message
4242

4343
private
4444

45-
def tool_calls_from_stream
45+
def tool_calls_from_stream # rubocop:disable Metrics/MethodLength
4646
tool_calls.transform_values do |tc|
47+
# The key fix - handle empty string arguments
48+
arguments = if tc.arguments.is_a?(String) && !tc.arguments.empty?
49+
JSON.parse(tc.arguments)
50+
elsif tc.arguments.is_a?(String)
51+
{} # Return empty hash for empty string arguments
52+
else
53+
tc.arguments
54+
end
55+
4756
ToolCall.new(
4857
id: tc.id,
4958
name: tc.name,
50-
arguments: tc.arguments.is_a?(String) ? JSON.parse(tc.arguments) : tc.arguments
59+
arguments: arguments
5160
)
5261
end
5362
end

spec/fixtures/vcr_cassettes/chat_function_calling_anthropic_claude-3-5-haiku-20241022_can_use_tools_with_multi-turn_streaming_conversations.yml

Lines changed: 94 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/chat_function_calling_anthropic_claude-3-5-haiku-20241022_can_use_tools_without_parameters_in_multi-turn_streaming_conversations.yml

Lines changed: 763 additions & 0 deletions
Large diffs are not rendered by default.

spec/fixtures/vcr_cassettes/chat_function_calling_bedrock_anthropic_claude-3-5-haiku-20241022-v1_0_can_use_tools_with_multi-turn_streaming_conversations.yml

Lines changed: 48 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/chat_function_calling_bedrock_anthropic_claude-3-5-haiku-20241022-v1_0_can_use_tools_without_parameters_in_multi-turn_streaming_conversations.yml

Lines changed: 225 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/chat_function_calling_deepseek_deepseek-chat_can_use_tools_with_multi-turn_streaming_conversations.yml

Lines changed: 60 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/chat_function_calling_deepseek_deepseek-chat_can_use_tools_without_parameters_in_multi-turn_streaming_conversations.yml

Lines changed: 331 additions & 0 deletions
Large diffs are not rendered by default.

spec/fixtures/vcr_cassettes/chat_function_calling_gemini_gemini-2_0-flash_can_use_tools_with_multi-turn_streaming_conversations.yml

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)