fix(aux): bracket-prefixed and fence-containing prose titles are kept

`_is_truncated_structured_output` flagged any reply starting with `{`/`[` or
containing an odd number of fences, so a provider that ignores response_format
and answers `[WIP] Fix login flow` or `Fix ``` rendering in chat` lost its
title (the derived title stayed). Truncated JSON is recognised by its JSON-shaped
opener (`{"`, `["`, `[{`) and an unclosed fence only when the fence opens the
reply — the fragments from #83903 are still dropped.

Review follow-up on #113960; two legit-title cases added to the existing
parametrized invariant (red on the previous head).
This commit is contained in:
teknium1
2026-09-17 01:37:16 -07:00
committed by Teknium
parent 0b52e40347
commit eff18764ff
2 changed files with 8 additions and 3 deletions

View File

@@ -245,9 +245,10 @@ def _extract_json_title(raw: str) -> Optional[str]:
def _is_truncated_structured_output(raw: str) -> bool:
"""Structured output the token cap cut before its closing quote/brace/fence (``{"title``, a bare fence opener).
Checked only after the JSON paths failed, and on structure alone (a leading brace/bracket, an
unclosed fence) so quoted or *emphasized* prose titles are untouched (#83903)."""
return raw.count("```") % 2 == 1 or raw.startswith(("{", "["))
Checked only after the JSON paths failed, and on structure alone (a JSON-shaped opener, a fence
opener that is never closed) so quoted, *emphasized* or ``[WIP]``-prefixed prose titles are
untouched (#83903)."""
return raw.startswith(('{"', '["', "[{")) or (raw.startswith("```") and raw.count("```") % 2 == 1)
def _extract_title_text(content: str) -> str:

View File

@@ -82,6 +82,10 @@ class TestGenerateTitle:
("修复登录按钮", "修复登录按钮"),
("42", "42"),
('```json\n{"title": "Fix login button"', "Fix login button"),
# Bracket/brace-prefixed prose and a literal fence inside a sentence are titles, not
# truncated JSON — a provider that ignores response_format still gets its title kept.
("[WIP] Fix login flow", "[WIP] Fix login flow"),
("Fix ``` rendering in chat", "Fix ``` rendering in chat"),
],
)
def test_truncated_structured_output_never_becomes_the_title(self, content, expected):