linshenkx/prompt-optimizer · error · Error

Unable to build model ${selectedModelId.value}: ${error inst

Error message

Unable to build model ${selectedModelId.value}: ${error instanceof Error ? error.message : String(error)}

What it means

During saveConfig, if the selected model ID isn't in the cached models list, useImageModelManager builds it via the provider adapter's buildDefaultModel. A builder failure (unknown/invalid model ID for that provider) is wrapped as 'Unable to build model <id>' with the original error attached as cause.

Source

Thrown at packages/ui/src/composables/model/useImageModelManager.ts:621

    isSaving.value = true

    try {
      const cachedProvider = providers.value.find(p => p.id === selectedProviderId.value)

      if (!cachedProvider) {
        throw new Error(`Provider not found: ${selectedProviderId.value}`)
      }

      // 获取模型信息:优先使用缓存,不存在时通过registry构建
      let cachedModel = models.value.find(m => m.id === selectedModelId.value)
      if (!cachedModel) {
        // 对于自定义模型ID,使用adapter的buildDefaultModel方法构建
        try {
          const adapter = registry.getAdapter(selectedProviderId.value)
          cachedModel = adapter.buildDefaultModel(selectedModelId.value)
        } catch (error) {
          throw new Error(
            `Unable to build model ${selectedModelId.value}: ${error instanceof Error ? error.message : String(error)}`,
            { cause: error }
          )
        }
      }

      // Manager owns authoritative provider/model metadata resolution.
      const completeConfig: ImageModelConfigInput = {
        ...configForm.value,
        providerId: selectedProviderId.value,
        modelId: selectedModelId.value
      }

      if (configForm.value.id) {
        await imageModelManager.updateConfig(configForm.value.id, completeConfig)
        toast.success(t('image.config.updateSuccess'))
      } else {
        const newConfig = { ...completeConfig, id: generateConfigId() }

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Verify the model ID against the provider's current model list and fix it.
  2. Refresh the model list (so cachedModel exists) before saving.
  3. Read error.cause to identify the exact builder rejection and update the adapter/package if the model is legitimately new.
Defensive patterns

Strategy: try-catch

Validate before calling

if (!models.value.some(m => m.id === selectedModelId.value)) { await loadModels() /* or verify ID with adapter */ }

Try / catch

catch (e) { if (e instanceof Error && e.message.startsWith('Unable to build model')) { console.error(e.cause); alertInvalidModelId(selectedModelId.value); return } throw e }

Prevention

When it happens

Trigger: Saving a config whose model ID is custom (not in models.value) and not constructible by the adapter — wrong model name, unsupported model on that provider, or an adapter that doesn't implement defaults for that ID.

Common situations: Custom model ID typos; provider renamed/deprecated the model; adapter version older than the model catalog.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/c85667a82cf43e93. Report an issue: GitHub.