The personal finance app for everyone (by everyone)
Ruby
Reject blank chat health probe answers (#5180)
Andy Wangefbbadb77e31
diff --git a/app/models/ai/transport/ruby_llm_adapter.rb b/app/models/ai/transport/ruby_llm_adapter.rbindex 65a5478f48..c21ff508ff 100644--- a/app/models/ai/transport/ruby_llm_adapter.rb+++ b/app/models/ai/transport/ruby_llm_adapter.rb@@ -44,7 +44,10 @@ module Ai::Transport::RubyLlmAdapterchat.add_message(role: :user, content: Ai::Transport::Base::PROBE_PROMPT)message = mapping_errors { chat.generate }check_answering_model!(model.name, message.model)- message.content.to_s+ text = message.content.to_s+ raise Ai::Error::InvalidOutput, "the chat probe returned no text" if text.blank?++ textenddef probe_tools(model, timeout: endpoint.timeout_seconds)diff --git a/test/models/ai_health/probe_chat_output_test.rb b/test/models/ai_health/probe_chat_output_test.rbnew file mode 100644index 0000000000..c97c0373d0--- /dev/null+++ b/test/models/ai_health/probe_chat_output_test.rb@@ -0,0 +1,90 @@+require "test_helper"++class AiHealth::ProbeChatOutputTest < ActiveSupport::TestCase+ PROTOCOLS = %i[chat_completions responses anthropic].freeze++ setup do+ @probe = AiHealth::Probe.new(cache: ActiveSupport::Cache::MemoryStore.new)+ Rails.logger.stubs(:error)+ DebugLogEntry.stubs(:capture)+ end++ PROTOCOLS.each do |protocol|+ { ok: "OK", nonliteral: "All systems working", empty: "", nil: nil, whitespace: " \n " }.each do |scenario, content|+ test "#{protocol}: chat probe checks #{scenario} output" do+ endpoint = configured_endpoint(protocol)+ request = stub_chat_response(protocol, content)++ result = @probe.llm(provider: protocol == :anthropic ? :anthropic : :openai,+ endpoint: endpoint.base_url, access_token: "test-token", model: "test-model")++ assert_requested request+ if %i[ok nonliteral].include?(scenario)+ assert result.passing?+ else+ assert result.failing?+ assert_equal :invalid_response, result.failure_code+ end+ end+ end++ test "#{protocol}: chat probe preserves nonblank answer text" do+ endpoint = configured_endpoint(protocol)+ text = " All systems working\n"+ stub_chat_response(protocol, text)++ assert_equal text, Ai::Transport.for(endpoint).probe(ai_model(endpoint:), :chat)+ end++ test "#{protocol}: model-list probe does not send a chat request" do+ endpoint = configured_endpoint(protocol, probe_check: :models)+ url = protocol == :anthropic ? "#{endpoint.base_url}/v1/models/test-model" : "#{endpoint.base_url}/models"+ body = protocol == :anthropic ? { id: "test-model" } : { data: [ { id: "test-model" } ] }+ request = stub_request(:get, url).to_return(status: 200, headers: json_headers, body: body.to_json)++ result = @probe.llm(provider: protocol == :anthropic ? :anthropic : :openai,+ endpoint: endpoint.base_url, access_token: "test-token", model: "test-model")++ assert result.passing?+ assert_requested request+ assert_not_requested :post, chat_url(protocol)+ end+ end++ private+ def configured_endpoint(protocol, probe_check: :chat)+ endpoint = ai_endpoint(protocol: protocol == :anthropic ? :anthropic : :compatible,+ base_url: protocol == :anthropic ? "https://anthropic.example.test" : "https://llm.example.test/v1",+ responses_api: protocol == :responses, probe_check:)+ @probe.stubs(:configured_endpoint).returns(endpoint)+ endpoint+ end++ def chat_url(protocol)+ case protocol+ when :chat_completions then "https://llm.example.test/v1/chat/completions"+ when :responses then "https://llm.example.test/v1/responses"+ when :anthropic then "https://anthropic.example.test/v1/messages"+ end+ end++ def json_headers = { "Content-Type" => "application/json" }++ def stub_chat_response(protocol, content)+ body = { model: "test-model" }+ case protocol+ when :chat_completions+ body.merge!(id: "c1", choices: [ { index: 0, finish_reason: "stop", message: { role: "assistant", content: } } ])+ when :responses+ output = content.nil? ? [] : [ { type: "message", id: "msg_1", role: "assistant", status: "completed",+ content: [ { type: "output_text", text: content, annotations: [] } ] } ]+ body.merge!(id: "r1", status: "completed", output:)+ when :anthropic+ body.merge!(id: "m1", type: "message", role: "assistant", stop_reason: "end_turn",+ content: content.nil? ? [] : [ { type: "text", text: content } ],+ usage: { input_tokens: 20, output_tokens: 10 })+ end++ stub_request(:post, chat_url(protocol)).to_return(status: 200, headers: json_headers, body: body.to_json)+ end+end