ruby_llm: [BUG] Multiple simultaneous tool calls behaves unexpectedly and causes error

Basic checks

  • I searched existing issues - this hasn’t been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code

What’s broken?

I created a super simple demo here: https://github.com/finbarr/multi-tool-demo

If the LLM returns multiple tool calls with a single message, RubyLLM tries to execute each sequentially but makes intermediate calls back to the LLM after each one, rather than running them all and then making a callback to the LLM. Somewhere along the way it breaks with

'RubyLLM::ErrorMiddleware.parse_error': messages: text content blocks must be non-empty (RubyLLM::BadRequestError)

How to reproduce

Expected behavior

  • RubyLLM should call each tool, aggregate the results, then make a callback to the LLM with all of the tool results.
  • It shouldn’t break.

What actually happened

  • RubyLLM calls the tools one at a time and tries to return the results individually to the LLM.
  • It breaks.

Environment

  • ruby 3.4.3
  • rails 8.0.2
  • ruby_llm 1.3.0

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 2
  • Comments: 26 (14 by maintainers)

Most upvoted comments

Feels like two related issues:

  1. RubyLLM is not calling the tool multiple times in parallel properly. (this issue)
  2. When Anthropic’s API returns an empty response, RubyLLM is sending a blank text content block on subsequent calls that Anthropic rejects. (#227)

I’m going to try to fix the second issue first as it is breaking my system constantly.

Yeah I agree. The bug definitely exists when not streaming.

@tpaulshippy also thanks for the fix for the blank text issue! Much more robust now.

@tpaulshippy I updated the gem version to the latest in https://github.com/finbarr/multi-tool-demo and I can still reproduce the issue. Here are the logs:

➜  multi-tool-demo git:(master) ./script/demo.rb 
D, [2025-06-11T20:50:22.451932 #94285] DEBUG -- RubyLLM: request: POST https://api.anthropic.com/v1/messages
D, [2025-06-11T20:50:22.451998 #94285] DEBUG -- RubyLLM: request: {"model":"claude-3-7-sonnet-20250219","messages":[{"role":"user","content":[{"type":"text","text":"Roll the dice 3 times all at once in a single array of tool calls, not across multiple messages."}]}],"temperature":0.7,"stream":false,"max_tokens":64000,"tools":[{"name":"dice_roll","description":"Rolls a single six-sided die and returns the result.","input_schema":{"type":"object","properties":{},"required":[]}}]}
D, [2025-06-11T20:50:24.394229 #94285] DEBUG -- RubyLLM: response: Status 200
D, [2025-06-11T20:50:24.396327 #94285] DEBUG -- RubyLLM: response: {"id" => "msg_01U9hFa6ZN2prY8jtATrtwKt",
 "type" => "message",
 "role" => "assistant",
 "model" => "claude-3-7-sonnet-20250219",
 "content" =>
  [{"type" => "text",
    "text" =>
     "I'll roll a six-sided die three times for you and show all the results at once."},
   {"type" => "tool_use",
    "id" => "toolu_01E62sKg96X9TdWGaFgHWbbt",
    "name" => "dice_roll",
    "input" => {}},
   {"type" => "tool_use",
    "id" => "toolu_01EcVQf4uiJWj4zbBW8B57o3",
    "name" => "dice_roll",
    "input" => {}},
   {"type" => "tool_use",
    "id" => "toolu_01DFC32JiWfpN2DdyysTZUy1",
    "name" => "dice_roll",
    "input" => {}}],
 "stop_reason" => "tool_use",
 "stop_sequence" => nil,
 "usage" =>
  {"input_tokens" => 393,
   "cache_creation_input_tokens" => 0,
   "cache_read_input_tokens" => 0,
   "output_tokens" => 95,
   "service_tier" => "standard"}}

D, [2025-06-11T20:50:24.421769 #94285] DEBUG -- RubyLLM: Tool dice_roll called with: {}
D, [2025-06-11T20:50:24.421824 #94285] DEBUG -- RubyLLM: Tool dice_roll returned: {roll: 2}
D, [2025-06-11T20:50:24.433403 #94285] DEBUG -- RubyLLM: request: POST https://api.anthropic.com/v1/messages
D, [2025-06-11T20:50:24.433540 #94285] DEBUG -- RubyLLM: request: {"model":"claude-3-7-sonnet-20250219","messages":[{"role":"user","content":[{"type":"text","text":"Roll the dice 3 times all at once in a single array of tool calls, not across multiple messages."}]},{"role":"assistant","content":[{"type":"text","text":"I'll roll a six-sided die three times for you and show all the results at once."},{"type":"tool_use","id":"toolu_01E62sKg96X9TdWGaFgHWbbt","name":"dice_roll","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01E62sKg96X9TdWGaFgHWbbt","content":"{roll: 2}"}]}],"temperature":0.7,"stream":false,"max_tokens":64000,"tools":[{"name":"dice_roll","description":"Rolls a single six-sided die and returns the result.","input_schema":{"type":"object","properties":{},"required":[]}}]}
D, [2025-06-11T20:50:26.711232 #94285] DEBUG -- RubyLLM: response: Status 200
D, [2025-06-11T20:50:26.712629 #94285] DEBUG -- RubyLLM: response: {"id" => "msg_01GbLXDWyNsdo6DGhqXMGan2",
 "type" => "message",
 "role" => "assistant",
 "model" => "claude-3-7-sonnet-20250219",
 "content" =>
  [{"type" => "tool_use",
    "id" => "toolu_01TzoRH61ZzupaPcUwJCimnY",
    "name" => "dice_roll",
    "input" => {}}],
 "stop_reason" => "tool_use",
 "stop_sequence" => nil,
 "usage" =>
  {"input_tokens" => 465,
   "cache_creation_input_tokens" => 0,
   "cache_read_input_tokens" => 0,
   "output_tokens" => 37,
   "service_tier" => "standard"}}

D, [2025-06-11T20:50:26.733408 #94285] DEBUG -- RubyLLM: Tool dice_roll called with: {}
D, [2025-06-11T20:50:26.733475 #94285] DEBUG -- RubyLLM: Tool dice_roll returned: {roll: 2}
D, [2025-06-11T20:50:26.743849 #94285] DEBUG -- RubyLLM: request: POST https://api.anthropic.com/v1/messages
D, [2025-06-11T20:50:26.744052 #94285] DEBUG -- RubyLLM: request: {"model":"claude-3-7-sonnet-20250219","messages":[{"role":"user","content":[{"type":"text","text":"Roll the dice 3 times all at once in a single array of tool calls, not across multiple messages."}]},{"role":"assistant","content":[{"type":"text","text":"I'll roll a six-sided die three times for you and show all the results at once."},{"type":"tool_use","id":"toolu_01E62sKg96X9TdWGaFgHWbbt","name":"dice_roll","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01E62sKg96X9TdWGaFgHWbbt","content":"{roll: 2}"}]},{"role":"assistant","content":[{"type":"tool_use","id":"toolu_01TzoRH61ZzupaPcUwJCimnY","name":"dice_roll","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01TzoRH61ZzupaPcUwJCimnY","content":"{roll: 2}"}]}],"temperature":0.7,"stream":false,"max_tokens":64000,"tools":[{"name":"dice_roll","description":"Rolls a single six-sided die and returns the result.","input_schema":{"type":"object","properties":{},"required":[]}}]}
D, [2025-06-11T20:50:28.003918 #94285] DEBUG -- RubyLLM: response: Status 200
D, [2025-06-11T20:50:28.005641 #94285] DEBUG -- RubyLLM: response: {"id" => "msg_01RzSDTRgNRohKne6qp4rS9d",
 "type" => "message",
 "role" => "assistant",
 "model" => "claude-3-7-sonnet-20250219",
 "content" =>
  [{"type" => "tool_use",
    "id" => "toolu_01EmhU6wTuDEdMeUuA6XKLFS",
    "name" => "dice_roll",
    "input" => {}}],
 "stop_reason" => "tool_use",
 "stop_sequence" => nil,
 "usage" =>
  {"input_tokens" => 517,
   "cache_creation_input_tokens" => 0,
   "cache_read_input_tokens" => 0,
   "output_tokens" => 37,
   "service_tier" => "standard"}}

D, [2025-06-11T20:50:28.026427 #94285] DEBUG -- RubyLLM: Tool dice_roll called with: {}
D, [2025-06-11T20:50:28.026501 #94285] DEBUG -- RubyLLM: Tool dice_roll returned: {roll: 4}
D, [2025-06-11T20:50:28.038451 #94285] DEBUG -- RubyLLM: request: POST https://api.anthropic.com/v1/messages
D, [2025-06-11T20:50:28.038689 #94285] DEBUG -- RubyLLM: request: {"model":"claude-3-7-sonnet-20250219","messages":[{"role":"user","content":[{"type":"text","text":"Roll the dice 3 times all at once in a single array of tool calls, not across multiple messages."}]},{"role":"assistant","content":[{"type":"text","text":"I'll roll a six-sided die three times for you and show all the results at once."},{"type":"tool_use","id":"toolu_01E62sKg96X9TdWGaFgHWbbt","name":"dice_roll","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01E62sKg96X9TdWGaFgHWbbt","content":"{roll: 2}"}]},{"role":"assistant","content":[{"type":"tool_use","id":"toolu_01TzoRH61ZzupaPcUwJCimnY","name":"dice_roll","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01TzoRH61ZzupaPcUwJCimnY","content":"{roll: 2}"}]},{"role":"assistant","content":[{"type":"tool_use","id":"toolu_01EmhU6wTuDEdMeUuA6XKLFS","name":"dice_roll","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01EmhU6wTuDEdMeUuA6XKLFS","content":"{roll: 4}"}]}],"temperature":0.7,"stream":false,"max_tokens":64000,"tools":[{"name":"dice_roll","description":"Rolls a single six-sided die and returns the result.","input_schema":{"type":"object","properties":{},"required":[]}}]}
D, [2025-06-11T20:50:29.269028 #94285] DEBUG -- RubyLLM: response: Status 200
D, [2025-06-11T20:50:29.270321 #94285] DEBUG -- RubyLLM: response: {"id" => "msg_01UFFiooekNZM9qi1oUTzLie",
 "type" => "message",
 "role" => "assistant",
 "model" => "claude-3-7-sonnet-20250219",
 "content" =>
  [{"type" => "text",
    "text" => "Here are your three dice rolls: 2, 2, and 4."}],
 "stop_reason" => "end_turn",
 "stop_sequence" => nil,
 "usage" =>
  {"input_tokens" => 569,
   "cache_creation_input_tokens" => 0,
   "cache_read_input_tokens" => 0,
   "output_tokens" => 21,
   "service_tier" => "standard"}}

I do have a possible PR fix for it, but I didn’t want to throw AI slop at you without really understanding the change, and I haven’t had time to dig into it properly.

@tpaulshippy it’s all in https://github.com/finbarr/multi-tool-demo. Relevant snippets below:

class DiceRoll < RubyLLM::Tool
  description "Rolls a single six-sided die and returns the result."

  def execute
    roll = rand(1..6)

    {
      roll: roll
    }
  end
end


# Create a new chat instance
chat = Chat.create!(model_id: "claude-3-7-sonnet-20250219")

chat.with_tool(DiceRoll)

chat.ask("Roll the dice 3 times all at once in a single array of tool calls, not across multiple messages.")