voltagent icon indicating copy to clipboard operation
voltagent copied to clipboard

[BUG] rag: detectFormat never recognizes top-level JSON arrays, so "auto" chunking skips the JSON chunker

Open Theater-ahyeon opened this issue 1 month ago • 0 comments

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 existing JSON.parse in a try/catch still guards against malformed input.
  • While testing this I hit a separate, unrelated problem: JsonChunker spreads the token chunker's metadata into buildMetadata's extra, and buildMetadata does Object.assign(meta, params.extra), so the format: "json" it sets is overwritten by the inherited format: "text". sourceType is explicitly stripped from the inherited metadata but format is not. I am not bundling that with a fix for this issue — happy to open a separate one if useful.

Theater-ahyeon avatar Oct 17 '26 08:10 Theater-ahyeon