Billionmail/BillionMail · error

failed to initialize AI client

Error message

failed to initialize AI client

What it means

Chat constructs the OpenAI-compatible client with NewOpenAI and returns this error if the constructor returns nil. A nil client means required configuration (API key, base URL, model metadata) was insufficient to build the client, so the request cannot proceed.

Source

Thrown at core/internal/service/askai/chat.go:279

	if err != nil {
		return errors.New("Chat not found")
	}
	// set the chat information
	chatInfo.SupplierName = supplierName
	chatInfo.ModelId = modelId
	err = SaveChat(chatId, chatInfo)
	if err != nil {
		return errors.New("Failed to save chat")
	}

	ChatStatus[chatId] = true // Set chat status to active
	defer func() {
		delete(ChatStatus, chatId) // Ensure chat status is removed after processing
	}()

	aiObj := NewOpenAI(ctx, supplierInfo.ApiKey, supplierInfo.BaseUrl, modelId, chatId, supplierName, modelInfo.MaxTokens)
	if aiObj == nil {
		return errors.New("failed to initialize AI client")
	}
	aiObj.GetClient()
	return aiObj.Chat(content, isText)
}

// RemoveChat removes a chat by its ID
// This function should handle the logic for removing a chat based on the provided chat ID
func RemoveChat(chatId string) error {
	chatPath := CHAT_CONFIG_PATH + "/" + chatId
	if !public.FileExists(chatPath) {
		return os.ErrNotExist
	}
	err := os.RemoveAll(chatPath)
	if err != nil {
		return err
	}
	return nil
}

View on GitHub (pinned to fc36c76c05)

Solutions

  1. Verify supplierInfo.ApiKey is set for the supplier (re-enter the API key in settings)
  2. Verify supplierInfo.BaseUrl is a valid reachable URL (e.g., https://api.openai.com/v1)
  3. Check modelInfo.MaxTokens is a sane positive value for the chosen model
  4. Inspect NewOpenAI to see which condition returns nil and add logging there

Example fix

// before
err := askai.Chat(ctx, chatId, supplier, modelId, content, true) // opaque nil-client error
// after
sup, _ := askai.GetSupplierConfig(supplier)
if sup.ApiKey == "" || sup.BaseUrl == "" {
    return errors.New("supplier api key/base url must be configured")
}
err = askai.Chat(ctx, chatId, supplier, modelId, content, true)
Defensive patterns

Strategy: validation

Validate before calling

sup, err := askai.GetSupplierConfig(supplierName)
if err != nil || sup.ApiKey == "" || sup.BaseUrl == "" {
    return errors.New("supplier api key and base url must be set")
}
if m := askai.GetModelInfo(supplierName, modelId); m == nil || m.MaxTokens <= 0 {
    return errors.New("model metadata (max tokens) missing")
}

Prevention

When it happens

Trigger: Calling Chat when supplierInfo.ApiKey is empty, BaseUrl is malformed/empty, or modelInfo.MaxTokens is invalid such that NewOpenAI returns nil.

Common situations: Supplier saved without an API key; BaseUrl pointing at a wrong or unreachable proxy URL; model metadata missing MaxTokens after a partial model import.

Related errors


AI-assisted analysis of Billionmail/BillionMail@fc36c76c05 (2026-09-05). Data as JSON: /api/errors/136049c382b9ee1b. Report an issue: GitHub.