A platform for community discussion. Free, open, simple.
FIX: Handle Gemini errors and blocked prompts reported in successful responses (#44262)
Régis Hanol3b7add4745d2
diff --git a/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rbindex 2b9e79fcb1f..2048b5d7ab9 100644--- a/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rb+++ b/plugins/discourse-ai/app/jobs/scheduled/topics_locale_detection_backfill.rb@@ -31,6 +31,8 @@ module Jobsreturn if topics.empty?topics.each do |topic|+ next if !DiscourseAi::Translation::TopicLocalizer.has_relocalize_quota?(topic, "")+DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic)rescue FinalDestination::SSRFDetector::LookupFailedError# do nothing, there are too many sporadic lookup failuresdiff --git a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb b/plugins/discourse-ai/lib/completions/endpoints/gemini.rbindex 486462bfe4f..568e98b93f9 100644--- a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/gemini.rb@@ -409,6 +409,7 @@ module DiscourseAidef decode(chunk)json = JSON.parse(chunk, symbolize_names: true)+ check_for_failure!(json)update_usage(json)candidate = json.dig(:candidates, 0)@@ -423,6 +424,7 @@ module DiscourseAistreaming_decoder.decode(chunk).map do |parsed|+ check_for_failure!(parsed)update_usage(parsed)candidate = parsed.dig(:candidates, 0)parts = candidate&.dig(:content, :parts)@@ -434,6 +436,17 @@ module DiscourseAi.compactend+ # Gemini reports errors and blocked prompts inside a successful (HTTP 200) response.+ def check_for_failure!(parsed)+ if (error = parsed[:error])+ raise CompletionFailed, error[:message].presence || error.to_json+ end++ if (block_reason = parsed.dig(:promptFeedback, :blockReason))+ Rails.logger.warn("#{self.class.name}: prompt blocked by provider: #{block_reason}")+ end+ end+def decode_parts(parts, batch_token:, streaming: false)idx = -1(parts || []).each_with_object([]) do |part, result|diff --git a/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rbindex 4cc96f51992..05ebd7157ee 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb@@ -77,6 +77,20 @@ describe Jobs::TopicsLocaleDetectionBackfill doexpect { job.execute({ limit: 10 }) }.not_to raise_errorend+ context "when relocalize quota is exhausted" do+ before do+ DiscourseAi::Translation::TopicLocalizer::MAX_QUOTA_PER_DAY.times do+ DiscourseAi::Translation::TopicLocalizer.has_relocalize_quota?(topic, "")+ end+ end++ it "skips locale detection for topics that have exceeded quota" do+ DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).never++ job.execute({})+ end+ end+it "logs a summary after running" doDiscourseAi::Translation::TopicLocaleDetector.stubs(:detect_locale)DiscourseAi::Translation::VerboseLogger.expects(:log).with(includes("Detected 1 topic locales"))diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rbindex fbe3bba600e..178567f7dca 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb@@ -1559,6 +1559,38 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Gemini do)end+ it "raises when an error event arrives mid-stream instead of returning the partial reply" do+ response = <<~TEXT+ data: {"candidates": [{"content": {"parts": [{"text": "Partial"}],"role": "model"}}]}++ data: {"error": {"code": 503,"message": "This model is currently experiencing high demand.","status": "UNAVAILABLE"}}++ TEXT++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:streamGenerateContent?alt=sse&key=123"+ stub_request(:post, url).to_return(status: 200, body: response)++ expect { llm.generate("Hello", user: user) { |_| } }.to raise_error(+ DiscourseAi::Completions::Endpoints::Base::CompletionFailed,+ /high demand/,+ )+ end++ it "logs the block reason when the prompt is blocked" do+ response = <<~TEXT+ data: {"promptFeedback": {"blockReason": "PROHIBITED_CONTENT"},"usageMetadata": {"promptTokenCount": 417,"totalTokenCount": 417}}++ TEXT++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:streamGenerateContent?alt=sse&key=123"+ stub_request(:post, url).to_return(status: 200, body: response)+ Rails.logger.expects(:warn).with(includes("PROHIBITED_CONTENT"))++ expect(llm.generate("Hello", user: user) { |_| }).to eq("")+ end+it "Can correctly handle streamed responses even if they are chunked badly" dodata = +""data << "da|ta: |"