The personal finance app for everyone (by everyone)
Ruby
fix(assistant): keep whitespace-only streamed chunks so spaces before numbers survive (#4515)
alexcastgonzccb25dc09c1c
diff --git a/app/models/assistant/responder.rb b/app/models/assistant/responder.rbindex b2f72b9211..bf24e707a9 100644--- a/app/models/assistant/responder.rb+++ b/app/models/assistant/responder.rb@@ -77,10 +77,22 @@ class Assistant::Responderdef request_response(function_results: [], previous_response_id: nil, tool_choice: nil)response_has_text = false+ leading_whitespace = +""+streamer = proc do |chunk|- if chunk.type == "output_text" && chunk.data.present?- response_has_text = true- emit(:output_text, chunk.data)+ # A whitespace-only chunk is real text (OpenAI streams the space before a number as its own chunk),+ # but a message cannot be saved with whitespace-only content, so hold leading whitespace back+ # until the first non-blank chunk arrives and emit it together with that chunk.+ if chunk.type == "output_text" && !chunk.data.to_s.empty?+ if response_has_text+ emit(:output_text, chunk.data)+ elsif chunk.data.blank?+ leading_whitespace << chunk.data+ else+ response_has_text = true+ emit(:output_text, leading_whitespace + chunk.data)+ leading_whitespace = +""+ endendenddiff --git a/test/models/assistant/responder_test.rb b/test/models/assistant/responder_test.rbindex 5eb5141c6f..2117c70aab 100644--- a/test/models/assistant/responder_test.rb+++ b/test/models/assistant/responder_test.rb@@ -41,6 +41,49 @@ class Assistant::ResponderTest < ActiveSupport::TestCaseassert_equal 8, Assistant::Responder::DEFAULT_MAX_TOOL_CALL_ITERATIONSend+ test "streamed chunks that are only whitespace are emitted so the spaces before numbers survive" do+ reply = Provider::LlmConcept::ChatResponse.new(+ id: "1", model: "gpt-4.1",+ messages: [ Provider::LlmConcept::ChatMessage.new(id: "1", output_text: "as of 10-22-2026") ],+ function_requests: []+ )++ @llm.stubs(:chat_response).with do |_prompt, **kwargs|+ [ "as", " of", " ", "10", "-", "22", "-", "202", "6" ].each do |text|+ kwargs[:streamer].call(Provider::LlmConcept::ChatStreamChunk.new(type: "output_text", data: text, usage: nil))+ end+ true+ end.returns(provider_success_response(reply))++ streamed = []+ @responder.on(:output_text) { |text| streamed << text }+ @responder.respond++ assert_equal "as of 10-22-2026", streamed.join+ end++ test "leading whitespace-only chunks are held back and emitted with the first non-blank chunk" do+ reply = Provider::LlmConcept::ChatResponse.new(+ id: "1", model: "gpt-4.1",+ messages: [ Provider::LlmConcept::ChatMessage.new(id: "1", output_text: "\n Hello there") ],+ function_requests: []+ )++ @llm.stubs(:chat_response).with do |_prompt, **kwargs|+ [ "\n", " ", "Hello", " ", "there" ].each do |text|+ kwargs[:streamer].call(Provider::LlmConcept::ChatStreamChunk.new(type: "output_text", data: text, usage: nil))+ end+ true+ end.returns(provider_success_response(reply))++ streamed = []+ @responder.on(:output_text) { |text| streamed << text }+ @responder.respond++ assert_equal "\n Hello", streamed.first+ assert_equal "\n Hello there", streamed.join+ end+test "final round requests forbid tool calls via tool_choice while keeping tool definitions" dofunction_request = Provider::LlmConcept::ChatFunctionRequest.new(id: "1", call_id: "1", function_name: "echo", function_args: "{}"