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 exampleUsage example的直接链接
await agent.generateLegacy('message for agent')
Processor retry supportProcessor 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.
ParametersParameters的直接链接
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?:
ReturnsReturns的直接链接
text?:
object?:
output, structuredOutput, or experimental_output.toolCalls?:
toolName:
args:
Migration to new APIMigration to new API的直接链接
The new .generate() method offers enhanced capabilities including compatibility with AI SDK v5 and later, better structured output handling, and improved streaming support. See the migration guide for detailed migration instructions.
Quick Migration ExampleQuick Migration Example的直接链接
Before (Legacy)Before (Legacy)的直接链接
const result = await agent.generateLegacy('message', {
temperature: 0.7,
maxSteps: 3,
})
After (New API)After (New API)的直接链接
const result = await agent.generate('message', {
modelSettings: {
temperature: 0.7,
},
maxSteps: 3,
})
Extended usage exampleExtended 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.6-sol',
errorStrategy: 'warn',
},
// Output processors for response validation
outputProcessors: [
new ModerationProcessor({ model: 'openai/gpt-5-mini' }),
new TokenLimiterProcessor({ maxTokens: 1000 }),
],
},
)