Tencent/WeKnora · error

failed to generate questions: %w

Error message

failed to generate questions: %w

What it means

Returned by generateQuestionsWithContext when the chatModel.Chat call itself fails after the prompt was built and sent (temperature 0.7, MaxTokens 512, thinking disabled). The LLM/provider error is wrapped with %w — network timeouts, provider 4xx/5xx, context-length overflow, or content-filter rejections all land here. Callers typically warn and skip the chunk rather than aborting the whole run.

Source

Thrown at internal/application/service/knowledge_process.go:2144

		"doc_name":       docName,
		"language":       langName,
	})
	prompt = types.AppendCustomPromptInstructions(prompt, customInstructions, "question_generation")

	thinking := false
	modelCtx := types.WithLLMCallMetadata(ctx, "question_generation", "")
	response, err := chatModel.Chat(modelCtx, []chat.Message{
		{
			Role:    "user",
			Content: prompt,
		},
	}, &chat.ChatOptions{
		Temperature: 0.7,
		MaxTokens:   512,
		Thinking:    &thinking,
	})
	if err != nil {
		return nil, fmt.Errorf("failed to generate questions: %w", err)
	}

	// Parse response
	lines := strings.Split(response.Content, "\n")
	questions := make([]string, 0, questionCount)
	for _, line := range lines {
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}
		line = strings.TrimLeft(line, "0123456789.-*) ")
		line = strings.TrimSpace(line)
		if line != "" && len(line) > 5 {
			questions = append(questions, line)
			if len(questions) >= questionCount {
				break
			}
		}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Inspect the wrapped error: timeout vs auth vs rate-limit vs context-length
  2. For context-length errors, truncate the chunk/context section or reduce MaxTokens usage and retry
  3. Check provider status, API key validity, and rate-limit quota
  4. Add per-call retry with backoff for transient provider errors (callers currently just skip the chunk)
  5. Reduce parallelism of batch tasks if hitting provider rate limits

Example fix

// before: single attempt, chunk skipped on any error
response, err := chatModel.Chat(modelCtx, []chat.Message{{Role: "user", Content: prompt}}, opts)
if err != nil {
	return nil, fmt.Errorf("failed to generate questions: %w", err)
}
// after: bounded retry for transient provider errors
var response *chat.Response
var lastErr error
for attempt := 0; attempt < 3; attempt++ {
	response, lastErr = chatModel.Chat(modelCtx, []chat.Message{{Role: "user", Content: prompt}}, opts)
	if lastErr == nil {
		break
	}
	if !isTransientLLMError(lastErr) {
		break
	}
	time.Sleep(time.Duration(1<<attempt) * time.Second)
}
if lastErr != nil {
	return nil, fmt.Errorf("failed to generate questions: %w", lastErr)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight the call: prompt size and model availability
if len(prompt) > maxPromptChars {
	return fmt.Errorf("prompt too large for model %s: %d chars", modelID, len(prompt))
}
if chatModel == nil {
	return fmt.Errorf("chat model not initialized")
}

Type guard

func llmCallReady(chatModel chat.Chat, prompt string) bool {
	return chatModel != nil && strings.TrimSpace(prompt) != ""
}

Try / catch

response, err := chatModel.Chat(modelCtx, msgs, &chat.ChatOptions{Temperature: 0.7, MaxTokens: 512, Thinking: &thinking})
if err != nil {
	switch {
	case isContextLengthError(err):
		// shrink context section and retry once
		return s.generateQuestionsWithContext(ctx, chatModel, truncate(content, half), "", "", docName, questionCount, custom)
	case isTransientLLMError(err): // timeout, 5xx, rate limit
		return nil, fmt.Errorf("failed to generate questions (retryable): %w", err)
	default:
		return nil, fmt.Errorf("failed to generate questions: %w", err)
	}
}

Prevention

When it happens

Trigger: chatModel.Chat(modelCtx, []chat.Message{{Role: "user", Content: prompt}}, opts) returns an error — provider API unreachable/timeout, invalid API key, rate limit, prompt exceeds model context window (chunk + surrounding context + template), or provider rejected the request.

Common situations: LLM provider outage or degraded latency causing timeouts; API key rotated/revoked; large chunks with image OCR enrichment push the prompt over the context limit; provider rate limits under parallel batch load; model temporarily unavailable.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/cf2d437d7dd2187a. Report an issue: GitHub.