fix(ingest): stop hallucinating summaries from filenames on empty PDFs

Scanned PDFs without a successful OCR pass (e.g. when no VLM is configured
on the KB, or when the VLM returned a "no text content" reply that the
sanitizer discarded) reach the summary and wiki-ingest pipelines with
nothing but markdown image references for content. The downstream LLM
prompts used to receive the filename, file type, and title alongside that
empty content, and the generate_summary template explicitly forbade the
model from refusing — so the model would invent a topic from the filename
alone, producing e.g. a "Canon scanner manual" summary for an MX5280.pdf
that is actually a scanned legal letter.

Two changes shut this down:

1. Drop filename / title / file-type from every LLM prompt input. The
   wiki summary, knowledge extract, candidate slug, and chunk citation
   prompts now see only the document content. The knowledge summary
   path no longer prepends the "Document Type: X / File Name: Y" intro.

2. Add a content-sufficiency guard in front of the LLM call. A new
   helper strips markdown image references and HTML image tags and
   refuses to invoke the model when fewer than 50 real-text runes
   remain. getSummary returns a sentinel error in that case and the
   caller marks the knowledge as SummaryStatusFailed instead of
   surfacing the first chunk's bare image reference. Wiki ingest skips
   LLM extraction with a clear log message.

The generate_summary.yaml prompt is also updated: the "NEVER output
refusal phrases" clause is removed and replaced with an explicit
empty-content rule that returns a deterministic placeholder line.

Real OCR (Tesseract / PaddleOCR) and filename-pattern sanitisation are
intentionally left for separate PRs.
This commit is contained in:
Claude
2026-05-02 09:06:32 +00:00
committed by lyingbug
parent d3d433574f
commit 15550d14d4
7 changed files with 183 additions and 42 deletions
+37 -16
View File
@@ -2242,6 +2242,13 @@ func (s *knowledgeService) processChunks(ctx context.Context,
// defaultMaxInputChars is the default maximum characters used as input for summary generation.
const defaultMaxInputChars = 1024 * 24
// errInsufficientSummaryContent signals that getSummary refused to call the
// LLM because the document had no usable text after image markup was stripped
// (typical for scanned PDFs where VLM OCR yielded nothing). Callers should
// mark the knowledge's summary as failed instead of falling back to the first
// chunk's raw content (which would just be a bare image reference).
var errInsufficientSummaryContent = errors.New("insufficient text content for summary generation")
// getSummary generates a summary for knowledge content using an AI model
func (s *knowledgeService) getSummary(ctx context.Context,
summaryModel chat.Chat, knowledge *types.Knowledge, chunks []*types.Chunk,
@@ -2295,22 +2302,23 @@ func (s *knowledgeService) getSummary(ctx context.Context,
logger.GetLogger(ctx).Infof("getSummary: content length=%d chars (max=%d) for knowledge %s",
len([]rune(chunkContents)), maxInputChars, knowledge.ID)
// Prepare content with metadata for summary generation
contentWithMetadata := chunkContents
// Add knowledge metadata if available
if knowledge != nil {
metadataIntro := fmt.Sprintf("Document Type: %s\nFile Name: %s\n", knowledge.FileType, knowledge.FileName)
// Add additional metadata if available
if knowledge.Type != "" {
metadataIntro += fmt.Sprintf("Knowledge Type: %s\n", knowledge.Type)
}
// Prepend metadata to content
contentWithMetadata = metadataIntro + "\nContent:\n" + contentWithMetadata
// Bail out before the LLM call when there is not enough actual text to
// summarise. We deliberately do not pass filename/file-type metadata to the
// LLM: scanned PDFs frequently carry filenames like "MX5280.pdf" (the
// scanner model), and feeding that to the model would invite it to
// hallucinate a scanner manual instead of admitting the document had no
// extractable text.
if !hasSufficientTextContent(chunkContents) {
logger.GetLogger(ctx).Warnf(
"getSummary: content for knowledge %s has insufficient text after stripping image markup (len=%d); skipping LLM call",
knowledge.ID, len([]rune(strings.TrimSpace(stripImageMarkup(chunkContents)))),
)
return "", errInsufficientSummaryContent
}
// Pass the raw chunk text to the LLM with no filename / file-type framing.
contentWithMetadata := chunkContents
// Determine max output tokens from config
maxTokens := 2048
if s.config.Conversation.Summary != nil && s.config.Conversation.Summary.MaxCompletionTokens > 0 {
@@ -2482,11 +2490,24 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn
summary, err := s.getSummary(ctx, chatModel, knowledge, textChunks)
if err != nil {
logger.Errorf(ctx, "Failed to generate summary for knowledge %s: %v", payload.KnowledgeID, err)
// Use first chunk content as fallback
// For the insufficient-content case (scanned PDF without OCR, etc.)
// we deliberately do NOT fall back to the first chunk's raw content,
// since that chunk is typically just a bare markdown image reference
// and surfacing it in the description is misleading.
if errors.Is(err, errInsufficientSummaryContent) {
knowledge.Description = ""
knowledge.SummaryStatus = types.SummaryStatusFailed
knowledge.UpdatedAt = time.Now()
if updateErr := s.repo.UpdateKnowledge(ctx, knowledge); updateErr != nil {
logger.Errorf(ctx, "Failed to mark summary as failed: %v", updateErr)
return fmt.Errorf("failed to update knowledge: %w", updateErr)
}
return nil
}
// For other errors (LLM API issues etc.), fall back to the first chunk.
if len(textChunks) > 0 {
summary = textChunks[0].Content
if len(summary) > 500 {
// Use rune-based truncation to avoid cutting UTF-8 multi-byte characters
runes := []rune(summary)
if len(runes) > 500 {
summary = string(runes[:500])
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"sort"
"strings"
"sync"
@@ -1213,6 +1214,48 @@ func appendUnique(arr types.StringArray, s string) types.StringArray {
return append(arr, s)
}
// minTextContentRunes is the minimum number of non-whitespace, non-image-reference
// runes required for content to be considered substantive enough for LLM
// summarization or wiki extraction. Documents below this threshold (e.g. a
// scanned PDF where OCR yielded nothing) are routed to a deterministic empty-
// content fallback instead of being passed to the LLM, which would otherwise
// hallucinate based on metadata alone.
const minTextContentRunes = 50
var (
mdImageRefRE = regexp.MustCompile(`!\[[^\]]*\]\([^)]*\)`)
// Strip the outer <images> wrapper before the inner <image> blocks so that
// nested structures like "<images><image>x</image></images>" don't leave a
// stray closing tag behind. RE2 has no backreferences, so we use two
// separate patterns instead of `<(image|images)\b...</\1>`.
htmlImagesBlockRE = regexp.MustCompile(`(?is)<images\b[^>]*>.*?</images>`)
htmlImageBlockRE = regexp.MustCompile(`(?is)<image\b[^>]*>.*?</image>`)
htmlImageTagRE = regexp.MustCompile(`(?i)<img\b[^>]*>`)
)
// stripImageMarkup removes markdown image references, raw <img> tags, and
// <image>/<images> XML blocks so that content consisting only of image
// placeholders (e.g. from PDFScannedParser pages without successful VLM OCR)
// is recognised as effectively empty.
func stripImageMarkup(s string) string {
s = htmlImagesBlockRE.ReplaceAllString(s, "")
s = htmlImageBlockRE.ReplaceAllString(s, "")
s = mdImageRefRE.ReplaceAllString(s, "")
s = htmlImageTagRE.ReplaceAllString(s, "")
return s
}
// hasSufficientTextContent reports whether the given content carries enough
// real text (after image markup is stripped) to warrant an LLM call. It is
// the primary defence against filename-driven hallucinations on scanned PDFs.
func hasSufficientTextContent(content string) bool {
stripped := strings.TrimSpace(stripImageMarkup(content))
if stripped == "" {
return false
}
return len([]rune(stripped)) >= minTextContentRunes
}
// cleanLLMJSON strips markdown code-fence wrappers and sanitizes control characters
// from LLM-generated JSON output so it can be safely unmarshalled.
func cleanLLMJSON(s string) string {
@@ -473,6 +473,18 @@ func (s *wikiIngestService) mapOneDocument(
}
logger.Infof(ctx, "wiki ingest: doc %s chunks=%d content_len(raw=%d,truncated=%d)", knowledgeID, len(chunks), rawRuneCount, len([]rune(content)))
// Refuse to run LLM-based extraction when the document carries no real
// text — e.g. a scanned PDF whose pages were converted to images but where
// VLM OCR produced nothing usable. Without this guard the LLM would have
// only image markup left and would happily fabricate entities/concepts.
if !hasSufficientTextContent(content) {
logger.Warnf(ctx,
"wiki ingest: doc %s has insufficient text content after stripping image markup (raw_len=%d), skipping LLM extraction",
knowledgeID, rawRuneCount,
)
return nil, nil, nil
}
docTitle := knowledgeID
if kn, err := s.knowledgeSvc.GetKnowledgeByIDOnly(ctx, knowledgeID); err == nil && kn != nil && kn.Title != "" {
docTitle = kn.Title
@@ -547,9 +559,6 @@ func (s *wikiIngestService) mapOneDocument(
go func() {
defer wg.Done()
summaryContent, summaryErr = s.generateWithTemplate(ctx, chatModel, agent.WikiSummaryPrompt, map[string]string{
"Title": docTitle,
"FileName": docTitle,
"FileType": "document",
"Content": content,
"Language": lang,
"ExtractedSlugs": slugListing,
@@ -780,7 +789,6 @@ func (s *wikiIngestService) extractEntitiesAndConceptsNoUpsert(
}
extractionJSON, err := s.generateWithTemplate(ctx, chatModel, agent.WikiKnowledgeExtractPrompt, map[string]string{
"Title": docTitle,
"Content": content,
"Language": lang,
"PreviousSlugs": prevSlugsText,
@@ -92,7 +92,6 @@ func (s *wikiIngestService) extractCandidateSlugs(
granularity := batchCtx.ExtractionGranularity.Normalize()
raw, err := s.generateWithTemplate(ctx, chatModel, agent.WikiCandidateSlugPrompt, map[string]string{
"Title": docTitle,
"Content": content,
"Language": lang,
"PreviousSlugs": prevSlugsText,
@@ -301,7 +300,6 @@ func (s *wikiIngestService) classifyChunkCitations(
eg.Go(func() error {
chunksXML := renderChunksXML(batch)
raw, err := s.generateWithTemplate(ectx, chatModel, agent.WikiChunkCitationPrompt, map[string]string{
"DocTitle": docTitle,
"CandidateSlugs": candidatesXML,
"ChunksXML": chunksXML,
"Language": lang,
@@ -106,3 +106,70 @@ func TestReconstructContentEmpty(t *testing.T) {
t.Errorf("Empty chunks should produce empty content, got %q", content)
}
}
func TestStripImageMarkup(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"plain text untouched", "Hello world.", "Hello world."},
{"single markdown image removed", "![alt](images/page_1.png)", ""},
{
"scanned-pdf style page references all stripped",
"![MX5280_page_1.png](images/MX5280_page_1.png)\n\n![MX5280_page_2.png](images/MX5280_page_2.png)",
"\n\n",
},
{"mixed text and image keeps text", "Intro paragraph.\n![fig](a.png)\nConclusion.", "Intro paragraph.\n\nConclusion."},
{"html img tag stripped", `Before <img src="x.png" alt="y"/> after`, "Before after"},
{"images xml block stripped", "Body <images><image>x</image></images> tail", "Body tail"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := stripImageMarkup(tt.input)
if got != tt.want {
t.Errorf("stripImageMarkup(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
func TestHasSufficientTextContent(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty string", "", false},
{"only whitespace", " \n\n\t ", false},
{
"only image references (scanned PDF without OCR)",
"![MX5280_page_1.png](images/MX5280_page_1.png)\n![MX5280_page_2.png](images/MX5280_page_2.png)",
false,
},
{"short text below threshold", "tiny", false},
{
"short text mixed with images still below threshold",
"![a](x.png)\nshort\n![b](y.png)",
false,
},
{
"sufficient text passes",
"This is a substantial paragraph of real content describing a legal matter and the parties involved.",
true,
},
{
"sufficient text mixed with images still passes",
"![cover](cover.png)\nThis is a substantial paragraph of real content describing a legal matter and the parties involved.\n![sig](sig.png)",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := hasSufficientTextContent(tt.input)
if got != tt.want {
t.Errorf("hasSufficientTextContent(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}