0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/plugins/discourse-ai/lib/utils
Rafael dos Santos Silva c10cce7b72
FIX: Recover AI structured output when models emit stray prefixes (#41745)
Follow-up to #41716, found by live-testing the new parsing chain against
locally configured vLLM models (Jolteon/CDCK MoM, DeepSeek V4 Flash,
Qwen3.6 35B).

## The bug

DeepSeek V4 Flash reproducibly prefixes its structured output with a
stray brace-quote, so the streamed document looks like:

```
{"{"output":"Olá a todos! ..."}
```

The complete, correct translation is in there, but every parser in the
best-effort chain rejects the document (the stray prefix parses as an
object key with no value), and `extract_key` returned an empty string —
so translations from this model came back blank.

## The fix

Add a recovery attempt to the chain, between the control-character
re-escape and the lenient parser: locate the schema key in the text and
re-parse (truncation-tolerant) from the object brace immediately
preceding it. This is generic for any "garbage before the real object"
response, not just the doubled brace-quote observed here.

## Verification

Live runs of the real `PostRawTranslator` pipeline (hazard-rich source:
emoji, inline code with `<`/`&`, code fences, blockquote, quoted
strings) against the three vLLM models:

| model | before | after |
|---|---|---|
| Jolteon (CDCK/MoM) | complete, clean stream | complete, clean stream |
| Qwen3.6 35B A3B | complete, clean stream | complete, clean stream |
| DeepSeek V4 Flash | **blank translation** | complete translation via
fallback, warning logged |

All structure checks pass on the recovered output: no control
characters, no literal escape artifacts, angle brackets/query
strings/code fences/blockquote/emoji intact.

Plus a regression spec with the exact observed shape. Parser +
structured output suites: 111 examples, 0 failures.
2026-07-15 13:48:23 -03:00
..
diff_utils
research
ai_staff_action_logger.rb
best_effort_json_parser.rb FIX: Recover AI structured output when models emit stray prefixes (#41745) 2026-07-15 13:48:23 -03:00
dns_srv.rb
image_to_text.rb
pdf_to_text.rb SECURITY: Harden imagemagick execution 2026-06-30 16:10:52 +02:00
search.rb