mastra-ai/mastra · error · Error

Received input message with wrong threadId. Input ${message.

Error message

Received input message with wrong threadId. Input ${message.threadId}, expected ${context.memoryInfo.threadId}

What it means

Error thrown by inputToMastraDBMessage when an input message carries a threadId that does not match the threadId of the memory context the agent is operating in. Memory-sourced messages (messageSource === 'memory') are exempt, but user/input messages must belong to the current thread to prevent cross-thread message injection.

Source

Thrown at packages/core/src/agent/message-list/conversion/input-converter.ts:44

/**
 * Convert any supported message input format to MastraDBMessage.
 * Routes to the appropriate converter based on message type detection.
 */
export function inputToMastraDBMessage(
  message: MessageInput,
  messageSource: MessageSource,
  context: InputConversionContext,
): MastraDBMessage {
  // Validate threadId matches (except for memory messages which can come from other threads)
  if (
    messageSource !== `memory` &&
    `threadId` in message &&
    message.threadId &&
    context.memoryInfo &&
    message.threadId !== context.memoryInfo.threadId
  ) {
    throw new Error(
      `Received input message with wrong threadId. Input ${message.threadId}, expected ${context.memoryInfo.threadId}`,
    );
  }

  // Validate resourceId matches (except for memory messages, which can carry a
  // system resourceId — e.g. observational-memory continuation messages)
  if (
    messageSource !== `memory` &&
    `resourceId` in message &&
    message.resourceId &&
    context.memoryInfo?.resourceId &&
    message.resourceId !== context.memoryInfo.resourceId
  ) {
    throw new Error(
      `Received input message with wrong resourceId. Input ${message.resourceId}, expected ${context.memoryInfo.resourceId}`,
    );
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Remove the threadId field from input messages and let the agent bind them to the current thread via memory options.
  2. Set the message's threadId to match the thread the agent is running with (context.memoryInfo.threadId).
  3. If intentionally reusing messages across threads, pass them with messageSource 'memory' or strip thread metadata before input.
  4. Audit client code for stale threadId state after thread switches.

Example fix

// before
await agent.generate(messages, { memory: { thread: otherThreadId, resource } }); // messages carry threadId: 'thread-A'

// after
const cleaned = messages.map(({ threadId, resourceId, ...m }) => m);
await agent.generate(cleaned, { memory: { thread: currentThreadId, resource } });
Defensive patterns

Strategy: validation

Validate before calling

function assertThreadMatches(messages: Array<{ threadId?: string }>, expectedThreadId: string): void {
  for (const m of messages) {
    if (m.threadId && m.threadId !== expectedThreadId) {
      throw new Error(`Message threadId ${m.threadId} != expected ${expectedThreadId}`);
    }
  }
}

Try / catch

try {
  await agent.generate(messages, { memory: { thread: threadId, resource } });
} catch (e) {
  if (e instanceof Error && e.message.includes('wrong threadId')) {
    const stripped = messages.map(({ threadId, ...m }) => m);
    return agent.generate(stripped, { memory: { thread: threadId, resource } });
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling agent.generate/stream with a messages array where a message has threadId set to a different thread than context.memoryInfo.threadId (e.g. passing remembered messages from another thread as direct input, or a stale client-side threadId after the agent switched threads).

Common situations: Client caching old thread IDs across conversations; copying messages between threads in custom code; resuming a stored conversation but passing the original thread's messages; multi-agent setups sharing message arrays across agents with different memory threads.

Related errors


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