Skip to content

fix(core): add truncation recovery to parseSpecStreamLine#213

Open
nil957 wants to merge 1 commit intovercel-labs:mainfrom
nil957:fix/parseSpecStreamLine-recovery
Open

fix(core): add truncation recovery to parseSpecStreamLine#213
nil957 wants to merge 1 commit intovercel-labs:mainfrom
nil957:fix/parseSpecStreamLine-recovery

Conversation

@nil957
Copy link

@nil957 nil957 commented Mar 13, 2026

Summary

Addresses #196

When LLM responses are truncated mid-stream, parseSpecStreamLine now attempts to repair the JSON by closing unclosed brackets, braces, and strings before giving up.

Problem

During streaming scenarios, network issues or model interruptions can cause incomplete JSON patches like:

{"op":"add","path":"/root","value":"main"

Previously, these would silently fail with no recovery attempt.

Solution

Added attemptJsonRepair() helper that:

  1. Tracks unclosed brackets/braces (respecting string boundaries)
  2. Closes unclosed strings first
  3. Closes remaining brackets/braces in reverse order

Recovery handles:

  • Missing closing braces: {"op":"add"... → adds }
  • Missing closing brackets: [1,2,3 → adds ]
  • Unclosed strings: "value":"hello → adds "
  • Nested structures: handles multiple levels

Returns null for truly malformed JSON (mismatched brackets) to avoid false positives.

Testing

Added comprehensive test suite covering:

  • Valid JSON parsing (unchanged behavior)
  • Truncation recovery scenarios
  • Edge cases (malformed vs truncated)

Checklist

  • Tests added
  • No breaking changes
  • Documentation updated (JSDoc comments)

Addresses vercel-labs#196

When LLM responses are truncated mid-stream, parseSpecStreamLine now
attempts to repair the JSON by closing unclosed brackets, braces, and
strings before giving up.

This improves resilience during streaming scenarios where network
issues or model interruptions cause incomplete JSON patches.

Recovery handles:
- Missing closing braces: {"op":"add"... -> adds }
- Missing closing brackets: [1,2,3 -> adds ]
- Unclosed strings: "value":"hello -> adds "
- Nested structures: handles multiple levels

Returns null for truly malformed JSON (mismatched brackets) to avoid
false positives.
@vercel
Copy link
Contributor

vercel bot commented Mar 13, 2026

Someone is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

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