{"record":{"id":"39882a2a171d3eb5","repo":"mastra-ai/mastra","slug":"failed-to-load-models-res-status","errorCode":null,"errorMessage":"Failed to load models (${res.status})","messagePattern":"Failed to load models \\((.+?)\\)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory-ui/src/hooks/useAvailableModels.ts","lineNumber":28,"sourceCode":"  modelName: string;\n  hasApiKey: boolean;\n}\n\n/**\n * Session-independent model catalog for settings pickers (Factory default\n * model, pack editors, OM models). Server-filtered to providers with a\n * credential, so pickers never offer models that cannot run.\n */\nexport function useAvailableModelsQuery() {\n  const { baseUrl } = useApiConfig();\n  return useQuery({\n    queryKey: queryKeys.availableModels(),\n    queryFn: async () => {\n      const res = await fetch(`${baseUrl}/web/config/models`, {\n        credentials: 'include',\n        headers: { Accept: 'application/json' },\n      });\n      if (!res.ok) throw new Error(`Failed to load models (${res.status})`);\n      const data = (await res.json()) as { models: AvailableModelOption[] };\n      return data.models;\n    },\n  });\n}\n","sourceCodeStart":10,"sourceCodeEnd":34,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory-ui/src/hooks/useAvailableModels.ts#L10-L34","documentation":"The `useAvailableModels` hook fetches `/web/config/models` and throws this error for any non-OK HTTP status. The status code is embedded in the message so the developer can distinguish auth problems (401/403) from server failures (500).","triggerScenarios":"`fetch(`${baseUrl}/web/config/models`)` with `credentials: 'include'` resolves with `res.ok === false` — any 4xx/5xx from the config endpoint.","commonSituations":"Not authenticated (session cookie missing/expired → 401); server misconfiguration or config file missing (500); baseUrl pointing at wrong port/host; reverse proxy blocking the route; UI/server version mismatch after an upgrade.","solutions":["Check the status in the message: 401/403 → re-authenticate / fix cookie credentials; 500 → check server config for models","Hit `${baseUrl}/web/config/models` directly (curl with cookie) to confirm the server response","Verify `baseUrl` in useApiConfig points at the correct Factory server","Restart/redeploy the server if the models config failed to load"],"exampleFix":"// caller-side\nconst { data: models, isError, error } = useAvailableModels();\nif (isError) {\n  console.error(error.message); // \"Failed to load models (401)\"\n  if (error.message.includes('401')) await reauthenticate();\n}","handlingStrategy":"try-catch","validationCode":"// pre-flight\nconst probe = await fetch(`${baseUrl}/web/config/models`, { credentials: 'include', headers: { Accept: 'application/json' } });\nif (!probe.ok) throw new Error(`Models endpoint unhealthy (${probe.status})`);","typeGuard":null,"tryCatchPattern":"try {\n  const models = await queryFn();\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Failed to load models')) {\n    const status = e.message.match(/\\((\\d+)\\)/)?.[1];\n    if (status === '401' || status === '403') await reauthenticate();\n    else if (status?.startsWith('5')) showServerErrorBanner();\n  } else throw e;\n}","preventionTips":["Check the HTTP status embedded in the message before choosing a fix path","Verify authentication cookie state before mounting model-dependent UI","Confirm baseUrl and server health in deployment checks","Keep models config valid on the server (validate /web/config/models after deploys)"],"tags":["http","models","react-query","factory-ui"],"backgroundTag":"http-request-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}