Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(wiki): set MaxTokens for generateWithTemplate LLM calls

jiahao6635eeef89db3b96
diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.go
index 4b5d7caf8..40f6e0c19 100644
--- a/internal/application/service/wiki_ingest.go
+++ b/internal/application/service/wiki_ingest.go
@@ -148,6 +148,17 @@ const (
// materially prolonging task runtime when the remote is genuinely down.
wikiLLMMaxAttempts = 3
+ // wikiLLMMaxTokens is the completion-token budget for every LLM call
+ // routed through generateWithTemplate. Combined wiki extraction emits a
+ // single large JSON document (entities + concepts + details). When
+ // MaxTokens is left at 0, OpenAI-compatible clients omit max_tokens and
+ // providers such as DeepSeek apply a default of 8192 — long extracts are
+ // truncated mid-JSON with finish_reason=length, and parse fails with
+ // "unexpected end of JSON input" (EXTRACT_FAILED). Raising the budget to
+ // 32768 matches verified complete outputs for large Chinese policy docs;
+ // shorter replies still stop early via finish_reason=stop. See #2604.
+ wikiLLMMaxTokens = 32768
+
// wikiLLMBackoffBase is the base delay for the exponential backoff
// between retry attempts. The nth retry waits base << (n-1) — so with
// a 2s base we wait 2s, 4s, 8s between attempts.
@@ -2467,7 +2478,7 @@ func (s *wikiIngestService) generateWithTemplate(ctx context.Context, chatModel
)
}
thinking := false
- opts := &chat.ChatOptions{Temperature: 0.3, Thinking: &thinking}
+ opts := &chat.ChatOptions{Temperature: 0.3, Thinking: &thinking, MaxTokens: wikiLLMMaxTokens}
prefixFingerprint := chat.PromptPrefixFingerprint(messages, opts)
warmupKey := ""
if promptTpl == agent.WikiPageModifyUserPrompt {
diff --git a/internal/application/service/wiki_ingest_test.go b/internal/application/service/wiki_ingest_test.go
index 71712e7ad..091927df9 100644
--- a/internal/application/service/wiki_ingest_test.go
+++ b/internal/application/service/wiki_ingest_test.go
@@ -611,3 +611,26 @@ func (r *wikiPendingRepoForCleanupTest) DeleteByDedupKey(
) error {
return nil
}
+
+// TestGenerateWithTemplateSetsMaxTokens is a regression for #2604: without an
+// explicit MaxTokens, DeepSeek-class providers default to 8192 completion
+// tokens and truncate combined wiki extraction JSON mid-field.
+func TestGenerateWithTemplateSetsMaxTokens(t *testing.T) {
+ model := &templateCaptureChatModel{response: `{"entities":[],"concepts":[]}`}
+ service := &wikiIngestService{}
+ _, err := service.generateWithTemplate(
+ context.Background(),
+ model,
+ `Content={{.Content}}`,
+ map[string]string{"Content": "hello"},
+ )
+ if err != nil {
+ t.Fatalf("generateWithTemplate() error = %v", err)
+ }
+ if model.options.MaxTokens != wikiLLMMaxTokens {
+ t.Fatalf("MaxTokens = %d, want %d", model.options.MaxTokens, wikiLLMMaxTokens)
+ }
+ if model.options.Thinking == nil || *model.options.Thinking {
+ t.Fatalf("Thinking should be non-nil false, got %#v", model.options.Thinking)
+ }
+}