{"record":{"id":"7e8c5746ba036799","repo":"mastra-ai/mastra","slug":"agent-model-list-is-not-found-or-empty","errorCode":null,"errorMessage":"Agent model list is not found or empty","messagePattern":"Agent model list is not found or empty","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"packages/server/src/server/handlers/agents.ts","lineNumber":3361,"sourceCode":"\nexport const REORDER_AGENT_MODEL_LIST_ROUTE = createRoute({\n  method: 'POST',\n  path: '/agents/:agentId/models/reorder',\n  responseType: 'json',\n  pathParamSchema: agentIdPathParams,\n  bodySchema: reorderAgentModelListBodySchema,\n  responseSchema: modelManagementResponseSchema,\n  summary: 'Reorder agent model list',\n  description: 'Reorders the model list for agents with multiple model configurations',\n  tags: ['Agents', 'Models'],\n  requiresAuth: true,\n  handler: async ({ mastra, agentId, reorderedModelIds }) => {\n    try {\n      const agent = await getAgentFromSystem({ mastra, agentId });\n\n      const modelList = await agent.getModelList();\n      if (!modelList || modelList.length === 0) {\n        throw new HTTPException(400, { message: 'Agent model list is not found or empty' });\n      }\n\n      agent.reorderModels(reorderedModelIds);\n\n      return { message: 'Model list reordered' };\n    } catch (error) {\n      return handleError(error, 'error reordering model list');\n    }\n  },\n});\n\nexport const UPDATE_AGENT_MODEL_IN_MODEL_LIST_ROUTE = createRoute({\n  method: 'POST',\n  path: '/agents/:agentId/models/:modelConfigId',\n  responseType: 'json',\n  pathParamSchema: modelConfigIdPathParams,\n  bodySchema: updateAgentModelInModelListBodySchema,\n  responseSchema: modelManagementResponseSchema,","sourceCodeStart":3343,"sourceCodeEnd":3379,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/agents.ts#L3343-L3379","documentation":"Thrown by the model-reorder endpoint when `agent.getModelList()` returns null/undefined or an empty array. The reorder operation needs at least one model config to act on, so the server returns HTTP 400 'Agent model list is not found or empty'.","triggerScenarios":"POSTing to the reorder-models route (`reorderedModelIds`) for an agent whose resolved model list is empty — e.g. agent has no models configured or model resolution fails/returns nothing.","commonSituations":"Agent created without any models; env vars for the model provider missing so the default model fails to resolve; agent pointing at a storage-backed model config that was deleted; multi-model setup cleared out by a bad update.","solutions":["Configure at least one model for the agent (in code: `new Agent({ model: ... })`, or via server model config routes) before reordering.","Check provider env vars/credentials so the default model resolves into the list.","If models are stored remotely, verify they exist and the mastra instance can load them.","Re-fetch the model list (GET route) to confirm the agent actually has models before retrying reorder."],"exampleFix":"// before\nawait client.getAgent('my-agent').reorderModels(['m2', 'm1']); // agent has no models\n// after\nconst agent = new Agent({ name: 'my-agent', model: 'openai/gpt-4o' });\nawait client.getAgent('my-agent').reorderModels(['m2', 'm1']);","handlingStrategy":"validation","validationCode":"const models = await client.getAgent(agentId).getModels();\nif (!models || models.length === 0) throw new Error(`Agent ${agentId} has no models configured; configure one before reordering`);","typeGuard":"function hasModels(models: Array<{ id: string }> | null | undefined): models is Array<{ id: string }> { return Array.isArray(models) && models.length > 0; }","tryCatchPattern":"try { await client.getAgent(agentId).reorderModels(ids); } catch (e) { if (is400(e, 'Agent model list is not found or empty')) { await configureDefaultModel(agentId); } else throw e; }","preventionTips":["Always set a `model` on agents in code or via server config before using model-management routes.","Verify provider credentials/env so default models resolve.","Fetch the model list before mutating it.","Watch for concurrent deletions of model configs in multi-user setups."],"tags":["http-400","agents","configuration","model-list","empty-state"],"backgroundTag":"agent-model-list-empty","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}