Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(ai): send OpenAI-compatible tool payloads that strict endpoints accept (#3727)

Yaroslav Shparuke428d67c0e79
diff --git a/app/models/assistant/responder.rb b/app/models/assistant/responder.rb
index 95fe4c2c2b..15a0e797f3 100644
--- a/app/models/assistant/responder.rb
+++ b/app/models/assistant/responder.rb
@@ -194,7 +194,6 @@ class Assistant::Responder
messages << {
role: "tool",
tool_call_id: fn_result[:call_id],
- name: fn_result[:name],
content: content
}
end
diff --git a/app/models/provider/openai.rb b/app/models/provider/openai.rb
index 901d20efe8..1d3d039f27 100644
--- a/app/models/provider/openai.rb
+++ b/app/models/provider/openai.rb
@@ -669,9 +669,9 @@ class Provider::Openai < Provider
if function_results.any?
# Build assistant message with tool_calls
tool_calls = function_results.map do |fn_result|
- # Convert arguments to JSON string if it's not already a string
- arguments = fn_result[:arguments]
- arguments_str = arguments.is_a?(String) ? arguments : arguments.to_json
+ # Shared with ToolCall::Function.serialize_arguments so history and
+ # follow-up payloads stay in sync for strict OpenAI-compatible endpoints.
+ arguments_str = ToolCall::Function.serialize_arguments(fn_result[:arguments])
{
id: fn_result[:call_id],
@@ -706,7 +706,6 @@ class Provider::Openai < Provider
payload << {
role: "tool",
tool_call_id: fn_result[:call_id],
- name: fn_result[:name],
content: content
}
end
diff --git a/app/models/tool_call/function.rb b/app/models/tool_call/function.rb
index e91dc5aae8..844adf7f75 100644
--- a/app/models/tool_call/function.rb
+++ b/app/models/tool_call/function.rb
@@ -13,6 +13,15 @@ class ToolCall::Function < ToolCall
function_result: result
)
end
+
+ # Serializes tool-call arguments to the JSON-encoded string OpenAI requires.
+ # Blank strings (and nil) become "{}" so zero-argument calls don't send an
+ # empty string that strict OpenAI-compatible endpoints reject with a 400.
+ def serialize_arguments(args)
+ return "{}" if args.nil? || (args.is_a?(String) && args.blank?)
+
+ args.is_a?(String) ? args : args.to_json
+ end
end
def to_result
@@ -25,12 +34,16 @@ class ToolCall::Function < ToolCall
end
def to_tool_call
+ # OpenAI requires `function.arguments` to be a JSON-encoded string. Some
+ # OpenAI-compatible endpoints reject an object payload with a 400.
+ arguments = self.class.serialize_arguments(function_arguments)
+
{
id: provider_call_id,
type: "function",
function: {
name: function_name,
- arguments: function_arguments
+ arguments: arguments
}
}
end
diff --git a/test/models/assistant_test.rb b/test/models/assistant_test.rb
index 3ad3e43a95..add92aca21 100644
--- a/test/models/assistant_test.rb
+++ b/test/models/assistant_test.rb
@@ -651,7 +651,7 @@ class AssistantTest < ActiveSupport::TestCase
assert_not_nil tool_result_entry, "tool_result message missing from history"
assert_equal "call_abc", tool_call_entry[:tool_calls].first[:id]
assert_equal "call_abc", tool_result_entry[:tool_call_id]
- assert_equal "get_net_worth", tool_result_entry[:name]
+ assert_not tool_result_entry.key?(:name), "tool messages must not carry the deprecated `name` field"
end
private
diff --git a/test/models/provider/openai_test.rb b/test/models/provider/openai_test.rb
index 5a1ca309a2..2a28074fc7 100644
--- a/test/models/provider/openai_test.rb
+++ b/test/models/provider/openai_test.rb
@@ -760,4 +760,75 @@ class Provider::OpenaiTest < ActiveSupport::TestCase
config.build_input(prompt: "hi", messages: [ { role: "user", content: "old" } ])
end
end
+
+ test "generic chat path omits tool message name and sends string arguments" do
+ provider = Provider::Openai.new(
+ "test-token",
+ uri_base: "https://example.com/v1",
+ model: "test-model"
+ )
+
+ client = provider.instance_variable_get(:@client)
+ captured_params = nil
+ client.expects(:chat).with do |params|
+ captured_params = params.fetch(:parameters)
+ true
+ end.returns(
+ {
+ "id" => "chatcmpl-1",
+ "model" => "test-model",
+ "choices" => [ { "message" => { "role" => "assistant", "content" => "ok" } } ],
+ "usage" => { "prompt_tokens" => 10, "completion_tokens" => 5, "total_tokens" => 15 }
+ }
+ )
+
+ response = provider.chat_response(
+ "What is my net worth?",
+ model: "test-model",
+ functions: [ {
+ name: "get_net_worth",
+ description: "Gets a user's net worth",
+ params_schema: { type: "object", properties: {}, required: [], additionalProperties: false },
+ strict: true
+ } ],
+ function_results: [ {
+ call_id: "call_1",
+ name: "get_net_worth",
+ arguments: { "currency" => "USD" },
+ output: { "amount" => 10000 }
+ } ]
+ )
+
+ assert response.success?
+
+ tool_calls = captured_params[:messages]
+ .select { |m| m[:role] == "assistant" }
+ .flat_map { |m| m[:tool_calls] || [] }
+ assert_equal 1, tool_calls.size
+ assert_equal "get_net_worth", tool_calls.first.dig(:function, :name)
+ assert_instance_of String, tool_calls.first.dig(:function, :arguments)
+ assert_equal({ "currency" => "USD" }, JSON.parse(tool_calls.first.dig(:function, :arguments)))
+
+ tool_messages = captured_params[:messages].select { |m| m[:role] == "tool" }
+ assert_equal 1, tool_messages.size
+ assert_equal "call_1", tool_messages.first[:tool_call_id]
+ assert_not tool_messages.first.key?(:name), "tool messages must not carry the deprecated `name` field"
+ end
+
+ test "generic chat builder normalizes blank arguments to an empty JSON object" do
+ provider = Provider::Openai.new(
+ "test-token",
+ uri_base: "https://example.com/v1",
+ model: "test-model"
+ )
+
+ messages = provider.send(
+ :build_generic_messages,
+ prompt: "hi",
+ function_results: [ { call_id: "call_1", name: "get_net_worth", arguments: "", output: { "amount" => 10000 } } ]
+ )
+
+ tool_call = messages.find { |m| m[:role] == "assistant" }[:tool_calls].first
+ assert_equal "{}", tool_call.dig(:function, :arguments)
+ end
end
diff --git a/test/models/tool_call/function_test.rb b/test/models/tool_call/function_test.rb
new file mode 100644
index 0000000000..e54f26bbb3
--- /dev/null
+++ b/test/models/tool_call/function_test.rb
@@ -0,0 +1,56 @@
+require "test_helper"
+
+class ToolCall::FunctionTest < ActiveSupport::TestCase
+ test "to_tool_call serializes object arguments to a JSON string" do
+ tool_call = ToolCall::Function.new(
+ provider_id: "resp_1",
+ provider_call_id: "call_1",
+ function_name: "get_net_worth",
+ function_arguments: { "currency" => "USD" },
+ function_result: { "amount" => 10000 }
+ )
+
+ arguments = tool_call.to_tool_call.dig(:function, :arguments)
+
+ assert_instance_of String, arguments
+ assert_equal({ "currency" => "USD" }, JSON.parse(arguments))
+ end
+
+ test "to_tool_call leaves string arguments untouched" do
+ tool_call = ToolCall::Function.new(
+ provider_id: "resp_1",
+ provider_call_id: "call_1",
+ function_name: "get_net_worth",
+ function_arguments: '{"currency":"USD"}',
+ function_result: { "amount" => 10000 }
+ )
+
+ assert_equal '{"currency":"USD"}', tool_call.to_tool_call.dig(:function, :arguments)
+ end
+
+ test "to_tool_call normalizes blank arguments to an empty JSON object" do
+ [ "", " ", nil ].each do |blank_args|
+ tool_call = ToolCall::Function.new(
+ provider_id: "resp_1",
+ provider_call_id: "call_1",
+ function_name: "get_net_worth",
+ function_arguments: blank_args,
+ function_result: { "amount" => 10000 }
+ )
+
+ assert_equal "{}", tool_call.to_tool_call.dig(:function, :arguments), "expected #{blank_args.inspect} to serialize as \"{}\""
+ end
+ end
+
+ test "to_result keeps arguments as stored (object or string)" do
+ tool_call = ToolCall::Function.new(
+ provider_id: "resp_1",
+ provider_call_id: "call_1",
+ function_name: "get_net_worth",
+ function_arguments: { "currency" => "USD" },
+ function_result: { "amount" => 10000 }
+ )
+
+ assert_equal({ "currency" => "USD" }, tool_call.to_result[:arguments])
+ end
+end