Repository navigation
fix(rag): detect top-level JSON arrays in auto format detection - #1459
Theater-ahyeon wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 6e047d7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
PR Checklist
What is the current behavior?
detectFormat()accepted{or[as the opening character but only ever accepted}as the closing one:A top-level JSON array ends with
], so it never entered theJSON.parsebranch and was reported as"text".detectStrategy()indocument/transformers.tsmaps that result to a chunker, so withstrategy: "auto"any document whose content is a JSON array — API response dumps, exported record sets,.jsonfixtures — went toRecursiveChunkerinstead ofJsonChunker, losing the structuralpath: valuechunks and thefields/samplePathsmetadata.Object payloads were unaffected.
What is the new behavior?
The brackets are paired, so arrays are detected too:
The existing
try { JSON.parse(text) } catchstill guards malformed input, so[unclosed]keeps falling through to the html / latex / code / table / markdown checks and still returns"text".fixes (issue)
fixes #1458
Notes for reviewers
/\n\|.+\|\n/requires a newline before the first row, so a table at the very start of a document is also missed) — that is a separate bug and I would rather not bundle it.JsonChunker'sformat: "json"is overwritten by theformat: "text"it inherits from the token chunker, becausebuildMetadataends withObject.assign(meta, params.extra)and onlysourceTypeis stripped from the inherited metadata.packages/rag/src/__tests__/format-detector.spec.tswith four cases: array detection, object detection, malformed fall-through, and an end-to-end check thatchunkByStrategy("auto", '[{"name":"alice"},{"name":"bob"}]')now yieldsmetadata.fields === 2and a chunk containing0.name: alice.Verified locally (Windows host, no network or API keys):
vitest runinpackages/rag-> 47 passed across 19 files.format-detector.tsreverted tomain, the array detection test and the end-to-end test fail (expected 'text' to be 'json',expected undefined to be 2); with the change they pass.biome checkclean on both changed files (the CRLF complaint when checking a raw Windows checkout disappears against the committed LF blob).AI assistance: this fix and its tests were produced with an AI coding agent under my direction. The bug, the paired-bracket fix, the duplicate check and every test result quoted above were verified by running those commands against a checkout of
mainat72a46c7.Summary by cubic
Fixes
detectFormat()so top-level JSON arrays are detected as JSON instead of"text". Previously the check accepted{or[as an opening bracket but only}as a closing one, so arrays ending in]fell through toRecursiveChunkerunder theautostrategy, losing JSON structural chunks and metadata. Brackets are now paired ({...}and[...]), andJSON.parsestill guards malformed input.fixes #1458
Written for commit 6e047d7. Summary will update on new commits.
Summary by CodeRabbit