agent.generate() with structuredOutput.model throws "promise 'text' was not resolved or rejected

Description: Seeing an error when using agent.generate() with structured output that includes a model field. The stream finishes before the "finish" chunk resolves the text promise, causing a rejection. Error:
Error: promise 'text' was not resolved or rejected when stream finished
Error: promise 'text' was not resolved or rejected when stream finished
Code Sample:
import { Agent } from "@mastra/core/agent";
import { z } from "zod";

// Agent creation
const agent = new Agent({
id: "my-agent",
name: "my-agent",
description: "Test agent",
instructions: "You are a helpful assistant",
model: "openai/gpt-4o",
tools: {
// ... tools if any
},
memory: undefined, // or memory instance
});

const outputSchema = z.object({
response: z.string(),
status: z.enum(["success", "error"]),
});

// Calling generate with structuredOutput.model
async function testGenerate() {
try {
const response = await agent.generate(
[{ role: "user", content: "Hello" }],
{
structuredOutput: {
schema: outputSchema,
model: "openai/gpt-4o-mini",
},
resourceId: "test-resource",
threadId: "test-thread",
modelSettings: {
temperature: 0.7,
},
}
);

console.log(response.object);
} catch (error) {
console.error(error);
// Error: promise 'text' was not resolved or rejected when stream finished
}
}
import { Agent } from "@mastra/core/agent";
import { z } from "zod";

// Agent creation
const agent = new Agent({
id: "my-agent",
name: "my-agent",
description: "Test agent",
instructions: "You are a helpful assistant",
model: "openai/gpt-4o",
tools: {
// ... tools if any
},
memory: undefined, // or memory instance
});

const outputSchema = z.object({
response: z.string(),
status: z.enum(["success", "error"]),
});

// Calling generate with structuredOutput.model
async function testGenerate() {
try {
const response = await agent.generate(
[{ role: "user", content: "Hello" }],
{
structuredOutput: {
schema: outputSchema,
model: "openai/gpt-4o-mini",
},
resourceId: "test-resource",
threadId: "test-thread",
modelSettings: {
temperature: 0.7,
},
}
);

console.log(response.object);
} catch (error) {
console.error(error);
// Error: promise 'text' was not resolved or rejected when stream finished
}
}
Observations: - Occurs when structuredOutput.model is set (processor mode) - Does not occur when structuredOutput.model is omitted (direct mode) - The stream's flush() method rejects pending promises before the "finish" chunk resolves them - Using agent.stream() + consumeStream() + getFullOutput() works as a workaround Environment: - @mastra/core version: 0.24.6 - Node.js version: 22 Is this a known issue or am I missing something?
5 Replies
Navayuvan Subramanian
Navayuvan SubramanianOP•4d ago
I've tried removing the model on structuredOutput too, still getting the same error
_roamin_
_roamin_•4d ago
Hey @Navayuvan Subramanian ! I was able to run the code you pasted without running into any error. (@mastra/core@0.24.6, node v22.21.1) Is it still erroring out on your end?
Navayuvan Subramanian
Navayuvan SubramanianOP•4d ago
Yes, but for now I've downgraded to 0.21 and it got fixed. Will check again.
Mastra Triager
Mastra Triager•3d ago
šŸ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10857 šŸ” If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly. šŸ™ Thank you for helping us improve Mastra!
caleb
caleb•3d ago
Hey @Navayuvan Subramanian I wasn't able to repro this exactly with the code you shared but I was able to see this error when there was a previous error in the stream. Did you see any other error message above this one? For example if I called agent.generate with toolChoice trying to call a tool that doesn't exist, the stream will end with an error like this
Error executing step toolCallStep: Error: Tool getGreeting not found <-- the actual error that caused the stream to end early
at Object.execute (../node_modules/.pnpm/@mastra+core@0.24.6_openapi-...

Error: promise 'text' was not resolved or rejected when stream finished
at <anonymous> (../node_modules/.pnpm/@mastra+core@0.24.6_openapi-...
Error executing step toolCallStep: Error: Tool getGreeting not found <-- the actual error that caused the stream to end early
at Object.execute (../node_modules/.pnpm/@mastra+core@0.24.6_openapi-...

Error: promise 'text' was not resolved or rejected when stream finished
at <anonymous> (../node_modules/.pnpm/@mastra+core@0.24.6_openapi-...
The error message Error: promise 'text' was not resolved or rejected when stream finished would be thrown from agent.generate since it was awaiting the text promise. This promise should've been rejected earlier in the loop with the first error though so now I'm looking into why that wasn't happening.

Did you find this page helpful?