mastra-ai/mastra · error · Error

Could not find config for provider ${provider} with model id

Error message

Could not find config for provider ${provider} with model id ${modelId}

What it means

The model id parsed to a provider segment that has no entry in providerConfigs — the models.dev registry either wasn't fetched/synced, or the provider isn't in models.dev. The gateway can't determine which env var holds the API key, so it throws. Related to but distinct from 1378: here the id format is valid, but the provider is unknown.

Source

Thrown at packages/core/src/llm/model/gateways/models-dev.ts:318

      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({
    modelId,
    providerId,
    apiKey,
    headers,
  }: {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Ensure fetchProviders() ran successfully before resolving API keys (or the gateway auto-syncs at startup).
  2. Fix the provider prefix spelling in the model id.
  3. Inject a ProviderConfig for custom providers via the providerConfigs constructor option.
  4. Check for an accompanying 1377 error — a failed registry fetch leaves providerConfigs empty.

Example fix

// before
await gw.getApiKey('oepnai/gpt-4o'); // typo provider
// after
await gw.getApiKey('openai/gpt-4o');
// or for custom providers:
new ModelsDevGateway({ providerConfigs: { myprovider: { apiKeyEnvVar: 'MY_KEY', npm: '...', url: '...' } } })
Defensive patterns

Strategy: try-catch

Validate before calling

const [provider] = id.split('/');
if (gw.providerConfigs && !gw.providerConfigs[provider]) {
  throw new Error(`Provider '${provider}' not in registry — check spelling or inject a providerConfig`);
}

Type guard

function isKnownProvider(modelId, configs) {
  const [provider] = modelId.split('/');
  return Boolean(configs && Object.prototype.hasOwnProperty.call(configs, provider));
}

Try / catch

try {
  const key = await gw.getApiKey(modelId);
} catch (e) {
  if (/Could not find config for provider/.test(e.message)) {
    const [provider] = modelId.split('/');
    throw new Error(`Unknown provider '${provider}'. Sync models.dev registry or add a custom providerConfig.`);
  }
  throw e;
}

Prevention

When it happens

Trigger: getApiKey('someprovider/some-model') where this.providerConfigs['someprovider'] is undefined — registry not yet synced (fetchProviders not called/failed), provider missing from models.dev, or a custom provider not injected via providerConfigs.

Common situations: Calling getApiKey before fetchProviders(); typo'd provider prefix ('oepnai/...'); niche or brand-new provider not yet in models.dev; custom/self-hosted providers without a provided config; 1377's fetch failed leaving configs empty.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/82bb79526aea6373. Report an issue: GitHub.