Repository navigation
fix(libs): reject non-dict output in parse_and_check_json_markdown - #45225
Open
TINGyu123644 wants to merge 1 commit into
Open
TINGyu123644 wants to merge 1 commit into
TINGyu123644 wants to merge 1 commit into
Conversation
parse_and_check_json_markdown only special-cases lists; any other non-dict object falls through to the `key not in json_obj` check: - a valid JSON scalar (e.g. a fenced `42`, `null` or `true`, which parse_json_markdown explicitly supports via its fence fallback) raises TypeError instead of the contract OutputParserError; - a valid JSON string passes validation via substring matching whenever an expected key appears inside the string, and is returned to callers that then crash indexing it. Enforce that the parsed object is a dict (or a single-element list wrapping a dict) and raise OutputParserError otherwise. Verified against the existing unit tests plus new regression tests.
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.
Fixes #45224
Problem found
parse_and_check_json_markdowninapi/libs/json_in_md_parser.pyonly special-caseslistresults before running thekey not in json_objcheck, so non-dict objects leak into it with two wrong behaviours:```json\n42\n```,null,true— the fence fallback inparse_json_markdowndeliberately supports non-bracket content, and an existing unit test pins fenced scalars as supported) raisesTypeError: argument of type 'int' is not iterableinstead of the contractOutputParserError.str.__contains__semantics), and is returned to callers as if it were the parsed object.RuleConfigGeneratorOutputParser.parsethen crashes onparsed["prompt"]withTypeError: string indices must be integers.The parser consumes raw LLM output, so these inputs are realistic. Existing callers catch broad
Exception, so today this degrades the feature rather than crashing the process, but the error contract is broken and case 2 skips validation entirely.Reproduction
Run against
main(before this patch):Fix
After the single-element list unwrapping, enforce
isinstance(json_obj, dict)and raiseOutputParserError(f"got invalid return object. obj:{json_obj}")otherwise — same error shape the list branch already uses. Behaviour for dict (and single-dict list) results, and for unfenced/unparseable text, is unchanged.Verification
Local sandbox with the package structure mirrored (
libs.json_in_md_parser+core.llm_generator.output_parser.errorsstub,pytestvia uv):42/null/true→OutputParserError; JSON string containing an expected key substring →OutputParserError): failed on the unpatched code (3 ×TypeError, 1 × no error raised), all pass after the patch.api/tests/unit_tests/libs/test_json_in_md_parser.pysuite (13 tests): all pass unchanged after the patch (no regression).Diff: 1 file changed in the parser (4 lines added), regression tests appended to the existing test module.
The change was drafted with an AI coding assistant and reviewed before submission.