Skip to content

fix(core): escape raw \r and \t in parse_partial_json strings#36948

Closed
zxuhan7 (zxuhan) wants to merge 1 commit intolangchain-ai:masterfrom
zxuhan:fix/36747-parse-partial-json-control-chars
Closed

fix(core): escape raw \r and \t in parse_partial_json strings#36948
zxuhan7 (zxuhan) wants to merge 1 commit intolangchain-ai:masterfrom
zxuhan:fix/36747-parse-partial-json-control-chars

Conversation

@zxuhan
Copy link
Copy Markdown

Closes #36747


parse_partial_json already replaces a raw \n inside a JSON string value with the \\n escape sequence so the partial parse can succeed under strict=True (RFC 8259 forbids unescaped control characters in strings). Models also commonly emit raw \r and \t, which currently fall through and cause json.JSONDecodeError: Invalid control character at: ....

The fix factors the per-character substitution into a small {"\n": "\\n", "\r": "\\r", "\t": "\\t"} map and escapes the same way for all three. Other control characters (\b, \f, \x00–\x1f) remain unhandled, matching the prior scope. Adds a parametrised regression test covering each escape under strict=True.

AI agent involvement: this contribution was drafted with assistance from an AI coding agent.

@github-actions github-actions Bot added core `langchain-core` package issues & PRs fix For PRs that implement a fix size: XS < 50 LOC labels Apr 22, 2026
@github-actions
Copy link
Copy Markdown

This PR has been automatically closed because you are not assigned to the linked issue.

External contributors must be assigned to an issue before opening a PR for it. Please:

  1. Comment on the linked issue to request assignment from a maintainer
  2. Once assigned, your PR will be reopened automatically

Maintainers: reopen this PR or remove the missing-issue-link label to bypass this check.

@github-actions github-actions Bot closed this Apr 22, 2026
@zxuhan zxuhan7 (zxuhan) deleted the fix/36747-parse-partial-json-control-chars branch April 22, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix missing-issue-link new-contributor size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse_partial_json fails on raw \\r and \\t inside strings

1 participant