Skip to content

feat(llm): recover outer JSON span when fence stripping yields non-JSON#2610

Merged
benfrank241 merged 1 commit into
mainfrom
feat/strip-fences-json-fallback
Jul 7, 2026
Merged

feat(llm): recover outer JSON span when fence stripping yields non-JSON#2610
benfrank241 merged 1 commit into
mainfrom
feat/strip-fences-json-fallback

Conversation

@benfrank241

Copy link
Copy Markdown
Member

Builds on #2563 (line-based fence stripping) by grafting the most valuable idea from #2557 (parse-validated fallback) — so the two approaches compose instead of competing.

What

_strip_code_fences now:

  1. strips fences by line (from Fix _strip_code_fences truncating JSON when content contains inner backticks #2563) — inner backticks in JSON strings preserved
  2. if the stripped candidate is not valid JSON, falls back to the outermost parseable {..}/[..] span (from Harden fenced JSON parsing #2557)
  3. otherwise returns the content unchanged — never a worse candidate than the raw response

This recovers cases the line-based stripper alone gives up on: missing closing fence, JSON wrapped in prose, truncated output.

Tests

Extended test_strip_code_fences.py (13 pass): missing-closing-fence recovery, prose-wrapped JSON recovery, non-JSON fence left for retry, plus the existing inner-backtick + fence cases.

Supersedes the overlapping #2557 (which conflicted after #2558).

Grafts the parse-validated fallback from #2557 onto the line-based fence
stripper merged in #2563: after stripping, if the candidate is not valid
JSON (partial/absent fence, prose-wrapped or truncated output), fall back
to the outermost parseable {..}/[..] span. Never returns a worse candidate
than the raw content.
@benfrank241 benfrank241 merged commit a1ebb2d into main Jul 7, 2026
98 of 100 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant