Skip to content

Commit 0bf2d93

Browse files
TheCrowdshenglu-archshenglu zhangabhiaiyer91Mastra Code (anthropic/claude-opus-4-8)
authored
fix(core): AgentExecutionOptions<undefined> no longer requires structuredOutput (#17131)
Co-authored-by: shenglu <shenglu@sapia.ai> Co-authored-by: shenglu zhang <shenglu@sapia.ai@shenglus-MacBook-Pro.local> Co-authored-by: Abhi Aiyer <abhiaiyer91@gmail.com> Co-authored-by: Mastra Code (anthropic/claude-opus-4-8) <noreply@mastra.ai>
1 parent 94dfef6 commit 0bf2d93

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mastra/core': patch
3+
---
4+
5+
Fixed `AgentExecutionOptions<undefined>` (and its public/inner variants) incorrectly requiring a `structuredOutput` property. When the output type is `undefined` or `null` — including a fully-nullish union such as `undefined | null``structuredOutput` is now correctly optional, regardless of the `strictNullChecks` setting. This is the shape produced by `AgentConfig.defaultOptions`, so `defaultOptions: { maxSteps: 50 }` now type-checks without a spurious `structuredOutput` requirement. Object output types still require `structuredOutput` as before.

packages/core/src/agent/agent-types.test-d.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assertType, describe, expectTypeOf, it } from 'vitest';
33
import { z } from 'zod/v4';
44
import type { RequestContext } from '../request-context';
55
import type { PublicSchema } from '../schema';
6-
import type { AgentExecutionOptions } from './agent.types';
6+
import type { AgentExecutionOptions, PublicAgentExecutionOptions } from './agent.types';
77
import type { AgentConfig } from './types';
88

99
/**
@@ -151,4 +151,34 @@ describe('Agent Type Tests', () => {
151151
expectTypeOf(config.id).toEqualTypeOf<'test-agent'>();
152152
});
153153
});
154+
155+
describe('Issue #16732: AgentExecutionOptions<undefined> should not require structuredOutput', () => {
156+
it('should allow AgentExecutionOptions<undefined> without structuredOutput', () => {
157+
assertType<AgentExecutionOptions<undefined>>({ maxSteps: 50 });
158+
});
159+
160+
it('should allow PublicAgentExecutionOptions<undefined> without structuredOutput', () => {
161+
assertType<PublicAgentExecutionOptions<undefined>>({ maxSteps: 50 });
162+
});
163+
164+
it('should allow AgentExecutionOptions<null> without structuredOutput', () => {
165+
assertType<AgentExecutionOptions<null>>({ maxSteps: 50 });
166+
});
167+
168+
it('should still require structuredOutput for an object OUTPUT', () => {
169+
const schema = z.object({ value: z.string() });
170+
// @ts-expect-error structuredOutput is required when OUTPUT is an object
171+
assertType<AgentExecutionOptions<{ value: string }>>({ maxSteps: 50 });
172+
assertType<AgentExecutionOptions<{ value: string }>>({
173+
maxSteps: 50,
174+
structuredOutput: { schema },
175+
});
176+
});
177+
178+
it('should not require structuredOutput for a nullable union (T | undefined)', () => {
179+
// NonNullable<string | undefined> is `string`, which is not an object type,
180+
// so structuredOutput stays optional for the nullable-union case.
181+
assertType<AgentExecutionOptions<string | undefined>>({ maxSteps: 50 });
182+
});
183+
});
154184
});

packages/core/src/agent/agent.types.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,22 @@ export type AgentExecutionOptionsBase<OUTPUT> = {
652652
* Use this type for public method signatures.
653653
*/
654654
export type PublicAgentExecutionOptions<OUTPUT = unknown> = AgentExecutionOptionsBase<OUTPUT> &
655-
(OUTPUT extends {} ? { structuredOutput: PublicStructuredOutputOptions<OUTPUT> } : { structuredOutput?: never });
655+
([NonNullable<OUTPUT>] extends [never]
656+
? { structuredOutput?: never }
657+
: OUTPUT extends {}
658+
? { structuredOutput: PublicStructuredOutputOptions<OUTPUT> }
659+
: { structuredOutput?: never });
656660

657661
/**
658662
* Internal agent execution options that require StandardSchemaWithJSON.
659663
* Use this type internally after converting from PublicSchema.
660664
*/
661665
export type AgentExecutionOptions<OUTPUT = unknown> = AgentExecutionOptionsBase<OUTPUT> &
662-
(OUTPUT extends {} ? { structuredOutput: StructuredOutputOptions<OUTPUT> } : { structuredOutput?: never });
666+
([NonNullable<OUTPUT>] extends [never]
667+
? { structuredOutput?: never }
668+
: OUTPUT extends {}
669+
? { structuredOutput: StructuredOutputOptions<OUTPUT> }
670+
: { structuredOutput?: never });
663671

664672
export type InnerAgentExecutionOptions<OUTPUT = unknown> = AgentExecutionOptionsBase<OUTPUT> & {
665673
outputWriter?: OutputWriter;
@@ -673,4 +681,8 @@ export type InnerAgentExecutionOptions<OUTPUT = unknown> = AgentExecutionOptions
673681
snapshot: WorkflowRunState;
674682
};
675683
toolCallId?: string;
676-
} & (OUTPUT extends {} ? { structuredOutput: StructuredOutputOptions<OUTPUT> } : { structuredOutput?: never });
684+
} & ([NonNullable<OUTPUT>] extends [never]
685+
? { structuredOutput?: never }
686+
: OUTPUT extends {}
687+
? { structuredOutput: StructuredOutputOptions<OUTPUT> }
688+
: { structuredOutput?: never });

0 commit comments

Comments
 (0)