mastra-ai/mastra · error · Error
Could not identify provider from model id ${modelId}
Error message
Could not identify provider from model id ${modelId} What it means
getApiKey parses the model id as 'provider/model'; if either part is missing after splitting on '/', the provider cannot be determined and a plain Error is thrown. The models.dev gateway keys its provider configs by the id prefix, so a malformed id is unusable.
Source
Thrown at packages/core/src/llm/model/gateways/models-dev.ts:313
// provider default; prefer it when present.
const perModelApi = config?.modelOverrides?.[modelId]?.api;
const template = perModelApi ?? config?.url;
if (!template) {
return;
}
// Check for custom base URL from env vars (explicit override still wins)
const baseUrlEnvVar = `${providerId.toUpperCase().replace(/-/g, '_')}_BASE_URL`;
const customBaseUrl = envVars?.[baseUrlEnvVar] || process.env[baseUrlEnvVar];
return customBaseUrl || interpolateUrlTemplate(template, envVars);
}
getApiKey(modelId: string): Promise<string> {
const [provider, model] = modelId.split('/');
if (!provider || !model) {
throw new Error(`Could not identify provider from model id ${modelId}`);
}
const config = this.providerConfigs[provider];
if (!config) {
throw new Error(`Could not find config for provider ${provider} with model id ${modelId}`);
}
const apiKey = resolveApiKeyFromEnv(config.apiKeyEnvVar);
if (!apiKey) {
const envVarDisplay = Array.isArray(config.apiKeyEnvVar) ? config.apiKeyEnvVar.join(' or ') : config.apiKeyEnvVar;
throw new Error(`Could not find API key process.env.${envVarDisplay} for model id ${modelId}`);
}
return Promise.resolve(apiKey);
}
async resolveLanguageModel({View on GitHub (pinned to 75dd419e61)
Solutions
- Pass the full router-style id: 'provider/model' (e.g. 'openai/gpt-4o').
- Validate the id contains a non-empty provider and model segment before calling.
- Normalize programmatic id construction to guard against undefined segments.
Example fix
// before
await gw.getApiKey('gpt-4o');
// after
await gw.getApiKey('openai/gpt-4o'); Defensive patterns
Strategy: validation
Validate before calling
function parseModelId(modelId) {
const [provider, model] = modelId.split('/');
if (!provider || !model) throw new Error(`Model id must be 'provider/model', got: ${modelId}`);
return { provider, model };
}
parseModelId(id); // call before getApiKey Type guard
function isValidModelId(id) { return typeof id === 'string' && /^[^/]+\/[^/]+$/.test(id); } Prevention
- Always use router-style ids: 'provider/model'.
- Normalize/trim ids coming from user input or other SDKs.
- Add form validation rejecting ids without a '/' or with empty segments.
- Write a unit test for id parsing with edge cases.
When it happens
Trigger: Calling getApiKey with ids like 'gpt-4o' (no slash), 'openai/' (empty model), '/gpt-4o' (empty provider), or ids containing extra text where the split yields an empty segment.
Common situations: Users passing bare model names from OpenAI SDK habits instead of 'openai/gpt-4o'; building ids programmatically with undefined segments; copying ids from other tools with different id formats.
Related errors
- MODEL_ROUTER_NO_GATEWAY_FOUND
- Could not find config for provider ${provider} with model id
- Invalid model id: ${modelId}
- Could not generate title from input ${JSON.stringify(message
- Expected ${gatewayPrefix}/ in model router ID ${routerId}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/ea5601d4ab4ebcb2.
Report an issue: GitHub.