Skip to content

Commit 93f2cd9

Browse files
committed
Cover for symbol or string keys.
1 parent 0c62d47 commit 93f2cd9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/agents/runner.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ def build_message_params(msg)
338338
# RubyLLM stores tool_calls as: { call_id => ToolCall_object, ... }
339339
# Reference: openai/tools.rb:35 uses hash iteration |_, tc|
340340
params[:tool_calls] = msg[:tool_calls].each_with_object({}) do |tc, hash|
341-
hash[tc[:id]] = OpenStruct.new(tc)
341+
tool_call_id = tc[:id] || tc["id"]
342+
hash[tool_call_id] = OpenStruct.new(tc)
342343
end
343344
end
344345

spec/agents/runner_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,45 @@
348348
end
349349
end
350350

351+
context "with tool_calls stored using string keys" do
352+
let(:context_with_string_tool_calls) do
353+
{
354+
conversation_history: [
355+
{ role: :user, content: "What's the weather in SF?" },
356+
{
357+
role: :assistant,
358+
content: "Let me check that for you",
359+
tool_calls: [
360+
{ "id" => "call_123", "name" => "get_weather", "arguments" => { "location" => "SF" } }
361+
]
362+
},
363+
{ role: :tool, content: "72°F, Sunny", tool_call_id: "call_123" },
364+
{ role: :assistant, content: "It's 72°F and sunny in SF!" }
365+
]
366+
}
367+
end
368+
369+
before do
370+
stub_simple_chat("Clear skies ahead!")
371+
end
372+
373+
it "restores tool_calls and tool results when tool_call ids are string keyed" do
374+
result = runner.run(agent, "Anything else?", context: context_with_string_tool_calls)
375+
376+
expect(result.success?).to be true
377+
378+
tool_message = result.messages.find { |msg| msg[:role] == :tool }
379+
expect(tool_message).not_to be_nil
380+
expect(tool_message[:tool_call_id]).to eq("call_123")
381+
382+
assistant_with_tools = result.messages.find do |msg|
383+
msg[:role] == :assistant && msg[:tool_calls]&.any?
384+
end
385+
expect(assistant_with_tools).not_to be_nil
386+
expect(assistant_with_tools[:tool_calls].first[:id]).to eq("call_123")
387+
end
388+
end
389+
351390
context "with empty tool result" do
352391
let(:context_with_empty_tool_result) do
353392
{

0 commit comments

Comments
 (0)