[BUG] rag: detectFormat never recognizes top-level JSON arrays, so "auto" chunking skips the JSON chunker
Describe the bug
packages/rag/src/utils/format-detector.ts decides whether the input is JSON with:
if ((text.startsWith("{") || text.startsWith("[")) && text.endsWith("}")) {
The opening check accepts { or [, but the closing check only ever accepts }. A top-level JSON array ends with ], so it can never satisfy the condition and the JSON.parse branch is never reached for arrays.
Steps To Reproduce
import { detectFormat } from "../utils/format-detector"; // packages/rag/src/utils/format-detector.ts
detectFormat('[{"id":1},{"id":2}]'); // returns "text", expected "json"
detectFormat('{"id":1}'); // returns "json"
Reproduced on main at 72a46c7. No API key or network needed — detectFormat is a pure function.
Expected behavior
A well-formed top-level JSON array should be detected as json.
Impact
detectStrategy() in packages/rag/src/document/transformers.ts maps the detected format to a chunker, so with strategy: "auto" any document whose content is a JSON array — API response dumps, exported record sets, .json fixtures — is routed to RecursiveChunker instead of JsonChunker. The structural path: value representation and the fields / samplePaths metadata are silently not produced.
Objects ({ ... }) are unaffected, so this only misroutes array payloads.
Notes
- Possible fix: pair the brackets, i.e.
(text.startsWith("{") && text.endsWith("}")) || (text.startsWith("[") && text.endsWith("]")). The existingJSON.parsein a try/catch still guards against malformed input. - While testing this I hit a separate, unrelated problem:
JsonChunkerspreads the token chunker's metadata intobuildMetadata'sextra, andbuildMetadatadoesObject.assign(meta, params.extra), so theformat: "json"it sets is overwritten by the inheritedformat: "text".sourceTypeis explicitly stripped from the inherited metadata butformatis not. I am not bundling that with a fix for this issue — happy to open a separate one if useful.