structuredOutput issue with Zod primitives

I have noticed an issue: calling an agent.generate in 0.20.X recently I have noticed that giving a structuredOutput schema option which is a Zod primitive (for example: schema: z.string() instead of schema: z.object({ text: z.string() }). This breaks every time with a validation error for me. String is an example, but it also has issues with number(), etc.
6 Replies
Vulcano
VulcanoOP2w ago
Example unit test I would expect to pass:
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { describe, expect, it } from "vitest";
import { z } from "zod";

const agent = new Agent({
name: 'testAgent',
instructions: () => 'You are a test agent. You should return the same as the user, depending on the chat history.',
model: openai('gpt-4o'),
});

describe('Mastra agent generate with structuredOutput', () => {
it('should work with Zod primitives as structured output schema', async () => {
const result = await agent.generate([{ role: 'user', content: 'Hi, my name is Vulcano and I am 123 years old.' }],
{
structuredOutput: {
schema: z.string().describe('The name of the person'),
},
},
);
expect(result.object.toLowerCase()).toEqual('vulcano');
});
});
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { describe, expect, it } from "vitest";
import { z } from "zod";

const agent = new Agent({
name: 'testAgent',
instructions: () => 'You are a test agent. You should return the same as the user, depending on the chat history.',
model: openai('gpt-4o'),
});

describe('Mastra agent generate with structuredOutput', () => {
it('should work with Zod primitives as structured output schema', async () => {
const result = await agent.generate([{ role: 'user', content: 'Hi, my name is Vulcano and I am 123 years old.' }],
{
structuredOutput: {
schema: z.string().describe('The name of the person'),
},
},
);
expect(result.object.toLowerCase()).toEqual('vulcano');
});
});
But it yields the following error:
FAIL src/example.test.ts > Mastra agent generate with structuredOutput > should work with Zod primitives as structured output schema
Error: Type validation failed: Value: {"name":"Vulcano"}.
Error message: Validation failed
❯ processOutputStream ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ Object.execute ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ DefaultExecutionEngine.executeStep ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/noop.ts:14:12
❯ DefaultExecutionEngine.executeEntry ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/noop.ts:22:13
❯ DefaultExecutionEngine.execute ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/noop.ts:12:3
❯ Run._start ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/index.ts:55:46
❯ Workflow.execute ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/index.ts:17:15
FAIL src/example.test.ts > Mastra agent generate with structuredOutput > should work with Zod primitives as structured output schema
Error: Type validation failed: Value: {"name":"Vulcano"}.
Error message: Validation failed
❯ processOutputStream ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ Object.execute ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/agent/trip-wire.ts:40:26
❯ DefaultExecutionEngine.executeStep ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/noop.ts:14:12
❯ DefaultExecutionEngine.executeEntry ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/noop.ts:22:13
❯ DefaultExecutionEngine.execute ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/noop.ts:12:3
❯ Run._start ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/index.ts:55:46
❯ Workflow.execute ../../../node_modules/.pnpm/@mastra+core@0.20.2_openapi-types@12.1.3_react@19.1.1_zod@3.25.76/node_modules/@mastra/core/src/loop/telemetry/index.ts:17:15
So: it generated { "name": "Vulcano" } for some reason
Mastra Triager
GitHub
[DISCORD:1427287637508620298] structuredOutput issue with Zod primi...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1427287637508620298 I have noticed an issue: calling an agent.generate in 0.20.X recently I have noticed t...
_roamin_
_roamin_7d ago
Hi @Vulcano ! Checking with the team, but I believe the schema has to be defined as an object, not a primitive ok, yeah, so the schema must be defined as an object. In our next release, your code will actually throw an error telling you you need to provide an object 😉
Vulcano
VulcanoOP7d ago
Alright great in that case ill update my code thanks! With error are you guys planning runtime or also typing?
Mastra Triager
GitHub
[DISCORD:1427287637508620298] structuredOutput issue with Zod primi...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1427287637508620298 I have noticed an issue: calling an agent.generate in 0.20.X recently I have noticed t...
_roamin_
_roamin_6d ago
For now it'll be runtime, but we'll look into adding typyng as well 😉

Did you find this page helpful?