Agent.generateLegacy() (Legacy)
Deprecated: This method is deprecated and only works with legacy model adapters. For current model adapters, use .generate() instead.
The .generateLegacy() method is the legacy version of the agent generation API, used with legacy model adapters to produce text or structured responses. This method accepts messages and optional generation options.
Usage exampleDirect link to Usage example
await agent.generateLegacy('message for agent')
Processor retry supportDirect link to Processor retry support
generateLegacy() doesn't run error processors or maxProcessorRetries. It uses the legacy AI SDK generation path instead.
Scorer judges with legacy model adapters call generateLegacy(). They don't receive the coordinated StreamErrorRetryProcessor budget available to scorer judges that use Mastra’s current generation API. Use a current model adapter when you need error-processor retries. The legacy maxRetries option remains separate and defaults to 2.
ParametersDirect link to Parameters
messages:
options?:
abortSignal?:
context?:
structuredOutput?:
schema:
model:
errorStrategy?:
fallbackValue?:
instructions?:
outputProcessors?:
processOutputResult and processOutputStream functions.inputProcessors?:
processInput function.experimental_output?:
structuredOutput property. Enables structured output generation alongside text generation and tool calls. The model will generate responses that conform to the provided schema.instructions?:
output?:
memory?:
thread:
id and optional metadata.resource:
options?:
MemoryConfig below.maxSteps?:
maxRetries?:
onStepFinish?:
runId?:
telemetry?:
isEnabled?:
recordInputs?:
recordOutputs?:
functionId?:
temperature?:
toolChoice?:
'auto':
'none':
'required':
{ type: 'tool'; toolName: string }:
toolsets?:
clientTools?:
hooks?:
beforeToolCall can return { proceed: false, output } to skip the tool call.savePerStep?:
providerOptions?:
{ providerName: { optionKey: value } }. Since Mastra extends AI SDK, see the AI SDK documentation for complete provider options.openai?:
{ reasoningEffort: 'high' }anthropic?:
{ maxTokens: 1000 }google?:
{ safetySettings: [...] }[providerName]?:
requestContext?:
maxTokens?:
topP?:
temperature or topP, but not both.topK?:
presencePenalty?:
frequencyPenalty?:
stopSequences?:
seed?:
headers?:
ReturnsDirect link to Returns
text?:
object?:
output, structuredOutput, or experimental_output.toolCalls?:
toolName:
args:
Migration to new APIDirect link to Migration to new API
The new .generate() method offers enhanced capabilities including AI SDK v5+ compatibility, better structured output handling, and improved streaming support. See the migration guide for detailed migration instructions.
Quick Migration ExampleDirect link to Quick Migration Example
Before (Legacy)Direct link to Before (Legacy)
const result = await agent.generateLegacy('message', {
temperature: 0.7,
maxSteps: 3,
})
After (New API)Direct link to After (New API)
const result = await agent.generate('message', {
modelSettings: {
temperature: 0.7,
},
maxSteps: 3,
})
Extended usage exampleDirect link to Extended usage example
import { z } from 'zod'
import { ModerationProcessor, TokenLimiterProcessor } from '@mastra/core/processors'
await agent.generateLegacy(
[
{ role: 'user', content: 'message for agent' },
{
role: 'user',
content: [
{
type: 'text',
text: 'message for agent',
},
{
type: 'image',
imageUrl: 'https://example.com/image.jpg',
mimeType: 'image/jpeg',
},
],
},
],
{
temperature: 0.7,
maxSteps: 3,
memory: {
thread: 'user-123',
resource: 'test-app',
},
toolChoice: 'auto',
providerOptions: {
openai: {
reasoningEffort: 'high',
},
},
// Structured output with better DX
structuredOutput: {
schema: z.object({
sentiment: z.enum(['positive', 'negative', 'neutral']),
confidence: z.number(),
}),
model: 'openai/gpt-5.5',
errorStrategy: 'warn',
},
// Output processors for response validation
outputProcessors: [
new ModerationProcessor({ model: 'openai/gpt-5-mini' }),
new TokenLimiterProcessor({ maxTokens: 1000 }),
],
},
)