fix: respect string literals when splitting JSON objects#2015
Open
JSap0914 wants to merge 1 commit into
Open
Conversation
split_and_parse_json_objects counted every { and } to find object
boundaries, including braces inside JSON string values. A value such as
"x}y" terminated the object early, so the segment failed to parse and
valid objects were silently dropped.
Track string state (and backslash escapes) so braces inside string
values no longer affect the depth count.
Add regression tests covering closing/opening braces and escaped quotes
inside string values.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens split_and_parse_json_objects so it won’t mis-split JSON objects when braces appear inside JSON string values, and adds regression tests to prevent recurrence.
Changes:
- Track string/escape state while scanning to ignore
{/}inside JSON string values. - Add pytest regression cases covering braces inside string values and escaped quotes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/general/test_split_and_parse_json_objects.py | Adds regression tests for braces/quotes inside JSON strings. |
| crawl4ai/utils.py | Updates splitting logic to ignore braces while inside JSON strings. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+727
to
+740
| in_string = False | ||
| escape = False | ||
|
|
||
| for i, char in enumerate(json_string): | ||
| # Skip the character escaped by a preceding backslash (e.g. \" or \\). | ||
| if escape: | ||
| escape = False | ||
| continue | ||
| if char == "\\": | ||
| escape = True | ||
| continue | ||
| if char == '"': | ||
| in_string = not in_string | ||
| continue |
| assert unparsed == [] | ||
|
|
||
|
|
||
| def test_balanced_braces_inside_string_still_work(): |
| assert unparsed == [] | ||
|
|
||
|
|
||
| def test_escaped_quote_inside_string_value(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
split_and_parse_json_objectssplits a JSON array string into individualobject segments by tracking brace (
{/}) depth. The scan counted everybrace, including those that appear inside JSON string values. A value such as
"x}y"therefore terminated the object early, so the resulting segment wasinvalid JSON, failed to parse, and the valid objects were silently dropped.
Example (before this change):
Both objects are lost even though the input is valid JSON.
The fix tracks string state (and backslash escapes) so braces inside string
values no longer affect the depth count:
This helper is used to recover individual objects from LLM extraction output
(
extraction_strategy.py), where string values containing braces are common.List of files changed and why
crawl4ai/utils.py- make the brace scanner insplit_and_parse_json_objectsstring-aware (skip braces inside strings, honor\escapes).tests/general/test_split_and_parse_json_objects.py- new regression tests covering closing/opening braces and escaped quotes inside string values, plus plain/balanced cases.How Has This Been Tested?
The three new cases fail on
develop(objects dropped) and pass with this change; the existingtests/regression/test_reg_utils.pysuite continues to pass.Checklist: