Skip to content

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

FIX: Recover AI structured output when models emit stray prefixes (#41745)

Rafael dos Santos Silvac10cce7b729d
diff --git a/plugins/discourse-ai/lib/utils/best_effort_json_parser.rb b/plugins/discourse-ai/lib/utils/best_effort_json_parser.rb
index a050706575d..d552b920063 100644
--- a/plugins/discourse-ai/lib/utils/best_effort_json_parser.rb
+++ b/plugins/discourse-ai/lib/utils/best_effort_json_parser.rb
@@ -31,7 +31,9 @@ module DiscourseAi
# unclosed structures and strings.
# 2. JsonCompleter over a control-character-escaped copy: recovers
# values containing real newlines/tabs.
- # 3. SmarterJSON: lenient parsing for single quotes, unquoted keys,
+ # 3. JsonCompleter from the object brace preceding the schema key:
+ # recovers documents with stray characters before the real object.
+ # 4. SmarterJSON: lenient parsing for single quotes, unquoted keys,
# prose-wrapped JSON, and other LLM formatting quirks.
#
# A candidate wins if it holds the schema key; a parseable document
@@ -40,6 +42,7 @@ module DiscourseAi
attempts = [
-> { JsonCompleter.parse(cleaned) },
-> { JsonCompleter.parse(escape_control_characters(cleaned)) },
+ -> { parse_from_key_object(cleaned, key) },
-> { SmarterJSON.process_one(cleaned) },
]
@@ -60,6 +63,19 @@ module DiscourseAi
fallback
end
+ # Models occasionally emit stray characters before the real JSON
+ # object (observed: a duplicated opening brace-quote). Restart parsing
+ # from the object brace that precedes the schema key.
+ def parse_from_key_object(cleaned, key)
+ key_idx = cleaned.index("\"#{key}\"") || cleaned.index("'#{key}'")
+ return if key_idx.nil?
+
+ start = cleaned.rindex("{", key_idx)
+ return if start.nil? || start.zero?
+
+ JsonCompleter.parse(escape_control_characters(cleaned[start..]))
+ end
+
def remove_markdown_fences(text)
return text unless text.match?(/^```(?:json)?\s*\n/i)
diff --git a/plugins/discourse-ai/spec/lib/utils/best_effort_json_parser_spec.rb b/plugins/discourse-ai/spec/lib/utils/best_effort_json_parser_spec.rb
index db6d2f4bdad..069b7b636a0 100644
--- a/plugins/discourse-ai/spec/lib/utils/best_effort_json_parser_spec.rb
+++ b/plugins/discourse-ai/spec/lib/utils/best_effort_json_parser_spec.rb
@@ -183,6 +183,15 @@ RSpec.describe DiscourseAi::Utils::BestEffortJsonParser do
it "casts values to numbers for integer schemas" do
expect(described_class.extract_key('{"output": 42}', "integer", :output)).to eq(42)
end
+
+ it "recovers when the model emits stray characters before the JSON object" do
+ content = "Olá a todos! 😊\nVeja \"aspas\" e `List<string>` aqui."
+ input = "{\"#{{ output: content }.to_json}"
+
+ result = described_class.extract_key(input, "string", :output)
+
+ expect(result).to eq(content)
+ end
end
context "when very broken JSON is entered" do