{"record":{"id":"f798d6d4ea194216","repo":"mastra-ai/mastra","slug":"threadid-is-required","errorCode":null,"errorMessage":"threadId is required","messagePattern":"threadId is required","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"packages/server/src/server/handlers/agents.ts","lineNumber":2167,"sourceCode":"  summary: 'Abort active agent thread run',\n  description: 'Aborts the currently active stream run for a memory thread without changing thread subscriptions',\n  tags: ['Agents', 'Streaming'],\n  requiresAuth: true,\n  requiresPermission: 'agents:execute',\n  handler: async ({ mastra, agentId, resourceId, threadId, requestContext: serverRequestContext }) => {\n    try {\n      const agent = await getAgentFromSystem({ mastra, agentId, requestContext: serverRequestContext });\n      if (typeof (agent as { abortThreadStream?: unknown }).abortThreadStream !== 'function') {\n        throw new HTTPException(501, {\n          message: 'agent thread aborts are not supported by this Mastra core version',\n        });\n      }\n\n      const effectiveResourceId = getEffectiveResourceId(serverRequestContext, resourceId);\n      const effectiveThreadId = getEffectiveThreadId(serverRequestContext, threadId);\n\n      if (!effectiveThreadId) {\n        throw new HTTPException(400, { message: 'threadId is required' });\n      }\n\n      if (effectiveResourceId) {\n        const memory = await agent.getMemory({ requestContext: serverRequestContext });\n        if (memory) {\n          const thread = await memory.getThreadById({ threadId: effectiveThreadId });\n          await validateThreadOwnership(thread, effectiveResourceId);\n        }\n      }\n\n      const aborted = await agent.abortThreadStream({ resourceId: effectiveResourceId, threadId: effectiveThreadId });\n      return { aborted };\n    } catch (error) {\n      return handleError(error, 'error aborting agent thread');\n    }\n  },\n});\n","sourceCodeStart":2149,"sourceCodeEnd":2185,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/agents.ts#L2149-L2185","documentation":"The abort endpoint needs a threadId to know which agent thread's stream to abort. After resolving effective values (including requestContext overrides), a missing threadId throws HTTP 400. resourceId is optional here — it only triggers extra ownership validation when present.","triggerScenarios":"Calling the thread abort route without threadId in params/body and without a requestContext override supplying it.","commonSituations":"Client aborting a run-level stream but passing no threadId; typo'd field name (thread_id vs threadId); thread created client-side but its ID never persisted/sent.","solutions":["Pass threadId in the request","Ensure the thread ID from stream metadata (e.g. stream.threadId) is captured and reused","Check requestContext overrides if you rely on context to inject threadId"],"exampleFix":"// before\nawait client.getAgent('a').abortThread({ resourceId: 'u1' });\n// after\nawait client.getAgent('a').abortThread({ resourceId: 'u1', threadId: 'thread-42' });","handlingStrategy":"validation","validationCode":"function assertThreadId(p: { threadId?: string | null }): asserts p is { threadId: string } {\n  if (!p.threadId) throw new Error('threadId is required');\n}","typeGuard":"function hasThreadId(p: { threadId?: string | null }): p is typeof p & { threadId: string } {\n  return typeof p.threadId === 'string' && p.threadId.length > 0;\n}","tryCatchPattern":"try {\n  await abortThread(params);\n} catch (e) {\n  if (e?.status === 400 && e.message === 'threadId is required') {\n    console.error('Persist the threadId from the stream before aborting');\n  }\n  throw e;\n}","preventionTips":["Store threadId from stream start events immediately","Use one consistent field name (threadId) across client and API calls","Disable abort actions in the UI until a threadId exists in state"],"tags":["http-400","missing-parameter","abort","server"],"backgroundTag":"missing-required-parameter","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}