Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[patch]: Fix "Bad control character in string literal" #5160

Merged
merged 4 commits into from
Apr 22, 2024

Conversation

Huaichen
Copy link
Contributor

The error "Bad control character in string literal" occurs when calling JSON.parse(json). This typically happens because the JSON string contains control characters that are not allowed, such as newline characters. Any control characters in the JSON string should be escaped before parsing it using JSON.parse().

A test is added to "langchain-core/src/output_parsers/structured.ts" with adding several newlines into the json. The test failed due to "Error: SyntaxError: Bad control character in string literal in JSON at position 36 (line 2 column 36)".

The code in parse() was updated by replacing the newline literal inside a string element with '\n', and removing the newline literal outside of the string elements.

The error "Bad control character in string literal" occurs when calling JSON.parse(json). This typically happens because the JSON string contains control characters that are not allowed, such as newline characters. Any control characters in the JSON string should be escaped before parsing it using JSON.parse().
Added a test for PR# langchain-ai#5135. Added a few newlines to the JSON text, which will result in an "Bad control character in string literal" error before the fix.
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 20, 2024
Copy link

vercel bot commented Apr 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 22, 2024 11:51pm
langchainjs-docs ✅ Ready (Inspect) Visit Preview Apr 22, 2024 11:51pm

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Apr 20, 2024
@jacoblee93 jacoblee93 closed this Apr 22, 2024
@jacoblee93 jacoblee93 reopened this Apr 22, 2024
@jacoblee93 jacoblee93 changed the title Fix "Bad control character in string literal" core[patch]: Fix "Bad control character in string literal" Apr 22, 2024
@jacoblee93
Copy link
Collaborator

Thank you! One small question

@jacoblee93 jacoblee93 added question Further information is requested lgtm PRs that are ready to be merged as-is labels Apr 22, 2024
@jacoblee93 jacoblee93 merged commit dd46dc9 into langchain-ai:main Apr 22, 2024
23 checks passed
@jacoblee93
Copy link
Collaborator

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature lgtm PRs that are ready to be merged as-is question Further information is requested size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants