Skip to content

fix(ai): handle escape sequences in INSIDE_OBJECT_KEY state of fixJson#14232

Open
Ricardo-M-L wants to merge 1 commit intovercel:mainfrom
Ricardo-M-L:fix/fix-json-object-key-escape
Open

fix(ai): handle escape sequences in INSIDE_OBJECT_KEY state of fixJson#14232
Ricardo-M-L wants to merge 1 commit intovercel:mainfrom
Ricardo-M-L:fix/fix-json-object-key-escape

Conversation

@Ricardo-M-L
Copy link
Copy Markdown
Contributor

Summary

  • The INSIDE_OBJECT_KEY state in fixJson did not handle backslash escape sequences (\\), while the INSIDE_STRING state properly handled them by pushing INSIDE_STRING_ESCAPE onto the stack
  • This caused the parser to incorrectly identify the end of an object key when the key contained escape sequences like \", \\, or \n — the escaped quote was treated as the closing delimiter
  • Added \\ case to INSIDE_OBJECT_KEY matching the existing INSIDE_STRING behavior, and added test cases covering escaped object keys

Bug

For input like {"key\"name": "value"}, the parser in INSIDE_OBJECT_KEY state encounters \" and treats the escaped " as the end of the key. It then transitions to INSIDE_OBJECT_AFTER_KEY, expecting a : next — but instead finds name, causing incorrect truncation/parsing.

The INSIDE_STRING state (lines 195-214) already handles this correctly:

case '\\': {
  stack.push('INSIDE_STRING_ESCAPE');
  break;
}

This PR adds the same handling to INSIDE_OBJECT_KEY.

Test plan

  • Added test: object key with escaped quote (\")
  • Added test: object key with escaped backslash (\\)
  • Added test: incomplete object with escaped key
  • All existing 46 tests continue to pass (49 total with new tests)

The INSIDE_OBJECT_KEY state in fixJson did not handle backslash escape
sequences, while the INSIDE_STRING state properly handled them by
pushing INSIDE_STRING_ESCAPE onto the stack. This inconsistency caused
the parser to incorrectly identify the key boundary when an object key
contained escape sequences like \", \\, or \n — treating the escaped
quote as the end of the key.

Add backslash handling to INSIDE_OBJECT_KEY matching the existing
INSIDE_STRING behavior, and add test cases for escaped object keys.
@tigent tigent bot added ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. bug Something isn't working as documented labels Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. bug Something isn't working as documented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant