Mintplex-Labs/anything-llm · error
ApiPie chat: is not valid for chat completion!
Error message
ApiPie chat: ${this.model} is not valid for chat completion! What it means
getChatCompletion first calls isValidChatCompletionModel(this.model), which compares the configured model id against the model list ApiPie returns for your key; a mismatch throws this. ApiPie is an aggregator and model ids are namespaced (e.g. "openrouter/mistral-7b-instruct"), so bare names, typos, retired ids, and keys without upstream access all fail this preflight.
Solutions
- Re-fetch the model list in the ApiPie provider settings and pick a currently listed id
- Use the fully-qualified namespace/model id exactly as ApiPie lists it
- Confirm the key has access to that upstream provider
- Clear any stale APIPIE_LLM_MODEL_PREF env override after catalog changes
Example fix
# before export APIPIE_LLM_MODEL_PREF=mistral-7b-instruct # after - exact id from ApiPie's /v1/models export APIPIE_LLM_MODEL_PREF=openrouter/mistral-7b-instruct
Defensive patterns
Strategy: validation
Validate before calling
const models = await apipie.openai.models.list();
const ids = models.data.map((m) => m.id);
if (!ids.includes(modelPref)) {
return res.status(400).json({ error: `Model ${modelPref} unavailable. Pick one of: ${ids.slice(0, 20).join(", ")}...` });
} Try / catch
try {
await apipie.getChatCompletion(messages);
} catch (e) {
if (/not valid for chat completion/i.test(e.message)) {
// refresh the model list and repick before retrying once
}
throw e;
} Prevention
- Select models from the provider's live list, never free-typed ids
- Copy namespaced ids exactly (provider/model)
- Re-validate saved preferences after changing API keys
When it happens
Trigger: The default "openrouter/mistral-7b-instruct" removed from ApiPie's catalog; a user-entered APIPIE_LLM_MODEL_PREF without the provider prefix; the key lacking access to the upstream provider; a model id copied from a different provider's docs.
Common situations: Aggregator catalogs churning as upstream models are added or removed; switching keys with different entitlements; stale saved preferences after long idle periods.
Related errors
- GroqAI:chatCompletion
- No AZURE_OPENAI_MODEL_PREF ENV defined. This must the name…
- addToWorkspaces must be a string of comma-separated…
- AnthropicLLM::getChatCompletion failed to communicate with…
- " " is not a valid URL. Check your settings for the Azure…
AI-assisted analysis of Mintplex-Labs/anything-llm@3aec848f28 (2026-08-18).
Data as JSON: /api/errors/4b1195d8c4d28516.
Report an issue: GitHub.
Appendix: source
Thrown at server/utils/AiProviders/apipie/index.js:192
attachments = [],
}) {
const prompt = {
role: "system",
content: `${systemPrompt}${this.#appendContext(contextTexts)}`,
};
return [
prompt,
...formatChatHistory(chatHistory, this.#generateContent),
{
role: "user",
content: this.#generateContent({ userPrompt, attachments }),
},
];
}
async getChatCompletion(messages = null, { temperature = 0.7 }) {
if (!(await this.isValidChatCompletionModel(this.model)))
throw new Error(
`ApiPie chat: ${this.model} is not valid for chat completion!`
);
const result = await LLMPerformanceMonitor.measureAsyncFunction(
this.openai.chat.completions
.create({
model: this.model,
messages,
temperature,
})
.catch((e) => {
throw new Error(e.message);
})
);
if (
!result.output.hasOwnProperty("choices") ||
result.output.choices.length === 0View on GitHub (pinned to 3aec848f28)