mastra-ai/mastra · error · MastraError

INVALID_METHOD_TYPE

INVALID_METHOD_TYPE

Error message

INVALID_METHOD_TYPE

What it means

getModelMethodFromAgentMethod converts an agent method type ('generate' | 'generateLegacy' | 'stream' | 'streamLegacy') into a ModelMethodType. Any other value is unsupported, and an AGENT/USER MastraError with id INVALID_METHOD_TYPE is thrown. Note the message is exactly 'INVALID_METHOD_TYPE' with no context about the received value.

Source

Thrown at packages/core/src/llm/model/model-method-from-agent.ts:11

import type { AgentMethodType } from '../../agent';
import { ErrorCategory, ErrorDomain, MastraError } from '../../error';
import type { ModelMethodType } from './model.loop.types';

export function getModelMethodFromAgentMethod(methodType: AgentMethodType): ModelMethodType {
  if (methodType === 'generate' || methodType === 'generateLegacy') {
    return 'generate';
  } else if (methodType === 'stream' || methodType === 'streamLegacy') {
    return 'stream';
  } else {
    throw new MastraError({
      id: 'INVALID_METHOD_TYPE',
      domain: ErrorDomain.AGENT,
      category: ErrorCategory.USER,
    });
  }
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Only call with 'generate', 'generateLegacy', 'stream', or 'streamLegacy'
  2. Validate/normalize the method string before invoking; log the offending value since the error text omits it
  3. Check for version drift if an agent method was renamed
  4. Use TypeScript literal types so invalid values fail at compile time

Example fix

// before
const m = getModelMethodFromAgentMethod(userInput as string);
// after
type AgentMethod = 'generate' | 'generateLegacy' | 'stream' | 'streamLegacy';
const method: AgentMethod = userInput === 'stream' ? 'stream' : 'generate';
const m = getModelMethodFromAgentMethod(method);
Defensive patterns

Strategy: type-guard

Validate before calling

const VALID = ['generate', 'generateLegacy', 'stream', 'streamLegacy'] as const;
if (!VALID.includes(methodType as any)) {
  throw new Error(`Unsupported agent method: ${String(methodType)}`);
}

Type guard

type AgentMethodType = 'generate' | 'generateLegacy' | 'stream' | 'streamLegacy';
function isAgentMethodType(v: unknown): v is AgentMethodType {
  return v === 'generate' || v === 'generateLegacy' || v === 'stream' || v === 'streamLegacy';
}

Try / catch

try {
  const m = getModelMethodFromAgentMethod(raw as AgentMethodType);
} catch (e) {
  if (e instanceof MastraError && e.id === 'INVALID_METHOD_TYPE') {
    console.error(`Invalid method value received: ${String(raw)}`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing any methodType other than the four accepted strings into getModelMethodFromAgentMethod (e.g. 'text', 'object', undefined, null, a misspelled variant).

Common situations: Custom agent wrappers or middleware passing through a wrong method name; API/version changes renaming agent methods; calling internal functions directly with untyped (string) input.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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