diff --git a/knowledge-fs/packages/api/src/llm-semantic-chunker.test.ts b/knowledge-fs/packages/api/src/llm-semantic-chunker.test.ts index e204d9fab9d..b592a13eeca 100644 --- a/knowledge-fs/packages/api/src/llm-semantic-chunker.test.ts +++ b/knowledge-fs/packages/api/src/llm-semantic-chunker.test.ts @@ -13,6 +13,7 @@ import { createInMemoryDocumentSemanticWindowCheckpointRepository } from "./docu import { DEFAULT_MAX_SEMANTIC_WINDOWS, + LlmSemanticChunkingOutputError, type LlmSemanticCompletionCatalogEntry, type LlmSemanticWindowManifestEntry, type SemanticChunkingLlmProvider, @@ -1513,6 +1514,36 @@ describe("LLM semantic chunker", () => { ).rejects.toThrow(error); }); + it("classifies an unknown response unit ID as an invalid model-runtime response", async () => { + const chunker = createLlmSemanticChunker({ + maxChunkChars: 20, + reasoningProviderFactory: () => + new ScriptedProvider([ + ({ units }) => ({ chunks: [chunkRange(units[0]?.id, "u-missing")] }), + ]), + }); + + const failure = chunker.chunk({ + knowledgeSpaceId: KNOWLEDGE_SPACE_ID, + parseArtifact: artifact([ + { + id: "paragraph", + metadata: {}, + sectionPath: ["Validation"], + text: "Alpha. Beta.", + type: "paragraph", + }, + ]), + retrievalProfile: profile(), + }); + + await expect(failure).rejects.toBeInstanceOf(LlmSemanticChunkingOutputError); + await expect(failure).rejects.toMatchObject({ + code: "MODEL_RUNTIME_RESPONSE_INVALID", + retryable: false, + }); + }); + it("drops ungrounded image OCR entities and their relations without discarding the chunk", async () => { const provider = new ScriptedProvider([ ({ units }) => ({ diff --git a/knowledge-fs/packages/api/src/llm-semantic-chunker.ts b/knowledge-fs/packages/api/src/llm-semantic-chunker.ts index 5eba1cefc71..2a47353fa4b 100644 --- a/knowledge-fs/packages/api/src/llm-semantic-chunker.ts +++ b/knowledge-fs/packages/api/src/llm-semantic-chunker.ts @@ -2836,7 +2836,9 @@ function validateAndMaterializeWindowOutput({ const start = unitIndex.get(candidate.startUnitId); const end = unitIndex.get(candidate.endUnitId); if (start === undefined || end === undefined) { - throw new Error("LLM semantic chunking response referenced an unknown unit ID"); + throw new LlmSemanticChunkingOutputError( + "LLM semantic chunking response referenced an unknown unit ID", + ); } if (start !== expectedStart || end < start) { throw new Error(