Skip to content

A platform for community discussion. Free, open, simple.

FIX: Return no title suggestions instead of a 500 when the reply isn't a list

Régis Hanol8b6461fb5935
diff --git a/plugins/discourse-ai/lib/ai_helper/assistant.rb b/plugins/discourse-ai/lib/ai_helper/assistant.rb
index 27af5cd8719..19acd0af672 100644
--- a/plugins/discourse-ai/lib/ai_helper/assistant.rb
+++ b/plugins/discourse-ai/lib/ai_helper/assistant.rb
@@ -173,8 +173,6 @@ module DiscourseAi
# for incomplete elements (e.g. a trailing nil while an array is
# mid-stream).
helper_response = structured_output&.read_buffered_property(schema_key)
- helper_response = helper_response.compact if helper_response.is_a?(Array)
- helper_response = [] if schema_type == "array" && helper_response.nil?
end
helper_response
@@ -199,7 +197,7 @@ module DiscourseAi
result[:suggestions] = (
if result[:type] == :list
- helper_response.flatten.map { |suggestion| sanitize_result(suggestion) }
+ list_suggestions(helper_response)
else
sanitized = sanitize_result(helper_response)
result[:diff] = parse_diff(input, sanitized) if result[:type] == :diff
@@ -431,6 +429,14 @@ module DiscourseAi
result.gsub(SANITIZE_REGEX, "")
end
+ # The mode's agent may not declare an array response format, and providers
+ # don't always enforce the schema, so the response may not be a list of strings.
+ def list_suggestions(response)
+ return [] if !response.is_a?(Array)
+
+ response.flatten.grep(String).map { |suggestion| sanitize_result(suggestion) }.compact_blank
+ end
+
def publish_update(channel, payload, user, client_id: nil)
# when publishing we make sure we do not keep large backlogs on the channel
# and make sure we clear the streaming info after 60 seconds
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
index 4f0e539db05..0f41e5da353 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
@@ -417,6 +417,34 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
expect(response[:suggestions]).to eq(["The solitary horse", "A horse lost in time"])
end
+
+ it "keeps only the non-blank string suggestions" do
+ mixed_titles = { output: [["The solitary horse", nil], 1, " ", "A horse lost in time"] }
+
+ response =
+ DiscourseAi::Completions::Llm.with_prepared_responses([mixed_titles]) do
+ assistant.generate_and_send_prompt(mode, english_text, user)
+ end
+
+ expect(response[:suggestions]).to eq(["The solitary horse", "A horse lost in time"])
+ end
+
+ it "returns no suggestions when the title agent replies with text instead of a list" do
+ plain_text_agent =
+ Fabricate(
+ :ai_agent,
+ response_format: nil,
+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
+ )
+ SiteSetting.ai_helper_title_suggestions_agent = plain_text_agent.id
+
+ response =
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ ["1. The solitary horse\n2. A horse lost in time"],
+ ) { assistant.generate_and_send_prompt(mode, english_text, user) }
+
+ expect(response[:suggestions]).to eq([])
+ end
end
context "when using a prompt that returns a string" do