Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-donkeys-detect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/rag": patch
---

fix(rag): detect top-level JSON arrays in `detectFormat` so the `auto` chunking strategy routes them to the JSON chunker instead of the recursive one
27 changes: 27 additions & 0 deletions packages/rag/src/__tests__/format-detector.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { chunkByStrategy } from "../document/transformers";
import { detectFormat } from "../utils/format-detector";

describe("detectFormat", () => {
it("detects a top-level JSON array", () => {
expect(detectFormat('[{"id":1},{"id":2}]')).toBe("json");
});

it("detects a top-level JSON object", () => {
expect(detectFormat('{"id":1}')).toBe("json");
});

it("falls through when the brackets do not parse as JSON", () => {
expect(detectFormat("[unclosed]")).toBe("text");
expect(detectFormat("{not json")).toBe("text");
});
});

describe("chunkByStrategy auto", () => {
it("routes a top-level JSON array to the JSON chunker", () => {
const chunks = chunkByStrategy("auto", '[{"name":"alice"},{"name":"bob"}]');
expect(chunks.length).toBeGreaterThan(0);
expect(chunks[0]?.metadata?.fields).toBe(2);
expect(chunks.map((chunk) => chunk.content).join("\n")).toContain("0.name: alice");
});
});
5 changes: 4 additions & 1 deletion packages/rag/src/utils/format-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export function detectFormat(input: string): DetectedFormat {
if (!text) return "text";

// JSON: quick check + parse
if ((text.startsWith("{") || text.startsWith("[")) && text.endsWith("}")) {
if (
(text.startsWith("{") && text.endsWith("}")) ||
(text.startsWith("[") && text.endsWith("]"))
) {
try {
JSON.parse(text);
return "json";
Expand Down