fix: guard malformed JSON in JSONConverter content_key path#11980
Conversation
When configured with content_key and no jq_schema, JSONConverter.run called json.loads without a guard, so a single invalid .json source raised an unhandled JSONDecodeError and aborted the whole run — unlike the jq_schema branch, which logs a warning and skips the file. Guard the non-jq json.loads the same way. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
@chuenchen309 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
|
Hi @chuenchen309, thanks for the opening the PR. I also noticed you opened 7 PRs in the last few hours. Was this due to you using Haystack or by running AI tools on the code? |
|
Hi @davidsbatista — happy to be straight about it. I'm a light user of Haystack, not a heavy day-to-day one, but it's a project I'm genuinely interested in and wanted to contribute back to. For that reason, most of these came from using an AI coding assistant (Claude Code) to do focused code review across parts of the codebase, rather than from bugs I stumbled onto purely through my own usage — that's why several landed in a short window. Each PR isn't just an AI patch pasted in though: for every one I had it write a failing test that reproduces the actual defect, verified the fix makes the test pass, ran the relevant suite plus I take the point on pace — 7 PRs in a few hours is a lot to land on anyone's plate. I'll hold off on opening any more for now so these four don't get buried; happy to revisit later if useful, and no worries at all if some/all of these aren't a good fit for the project right now. |
Thanks a lot for the clarification, and happy to know about your interest and use of Haystack, also that not all PRs were automated and that some human reasoning was involved. We will review the PRs as time permits. |
|
Thanks @davidsbatista, really appreciate it — no rush on any of these. 🙏 |
davidsbatista
left a comment
There was a problem hiding this comment.
Looks good! Thanks for the contribution.
What
JSONConverter._extract_text_and_metadataguards thejq_schemapath (it logs a warning and skips the file on failure), but the non-jq branch — used when the converter is configured withcontent_keyand nojq_schema— callsjson.loads(file_content)with no guard:A single malformed
.jsonsource therefore raises an unhandledjson.JSONDecodeErrorand aborts the entirerun(), even when other sources in the batch are valid. This is inconsistent with thejq_schemapath and with the existing handling of decode errors (UnicodeError) a few lines above, both of which skip the offending file and continue.Fix
Wrap the non-jq
json.loadsin the same warn-and-skip guard already used by thejqbranch, catching the specificjson.JSONDecodeError.Test
Added
test_run_with_malformed_json_and_content_key, which mirrors the existingtest_run_with_non_json_filebut exercises thecontent_key-only path. It fails before the change (uncaughtJSONDecodeError) and passes after, asserting the file is skipped with a single warning andrun()returns{"documents": []}.Verified locally: the new test and the other non-jq tests in
test/components/converters/test_json.pypass; the jq-dependent tests require the optionaljqextra (not installed locally) and are unaffected by this change.ruff checkandruff format --checkare clean. Added a release note.Authored with AI assistance (Claude Code); the change and tests were reviewed and verified locally by me.