Skip to content

Commit 9e08bcc

Browse files
authored
fix(zod): ensure only zod/v4 types are used (#992)
Closes [#1035](#1035)
1 parent e3bcdd4 commit 9e08bcc

14 files changed

Lines changed: 21 additions & 23 deletions

examples/structured-outputs-streaming.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { zodOutputFormat } from '@anthropic-ai/sdk/helpers/zod';
44
import Anthropic from '@anthropic-ai/sdk';
5-
import { z } from 'zod';
5+
import { z } from 'zod/v4';
66

77
const WeatherResponse = z.object({
88
city: z.string(),

examples/structured-outputs-zod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { zodOutputFormat } from '@anthropic-ai/sdk/helpers/zod';
44
import Anthropic from '@anthropic-ai/sdk';
5-
import { z } from 'zod';
5+
import { z } from 'zod/v4';
66

77
const NumbersResponse = z.object({
88
primes: z.array(z.number()),

examples/tools-helpers-advanced-streaming.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Anthropic from '@anthropic-ai/sdk';
44
import { betaZodTool } from '@anthropic-ai/sdk/helpers/beta/zod';
55
import { BetaToolUseBlock } from '@anthropic-ai/sdk/resources/beta';
6-
import { z } from 'zod';
6+
import { z } from 'zod/v4';
77

88
const client = new Anthropic();
99

examples/tools-helpers-advanced.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Anthropic from '@anthropic-ai/sdk';
44
import { betaZodTool } from '@anthropic-ai/sdk/helpers/beta/zod';
55
import { BetaToolUseBlock } from '@anthropic-ai/sdk/resources/beta';
6-
import { z } from 'zod';
6+
import { z } from 'zod/v4';
77

88
const client = new Anthropic();
99

examples/tools-helpers-zod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import Anthropic from '@anthropic-ai/sdk';
44
import { betaZodTool } from '@anthropic-ai/sdk/helpers/beta/zod';
5-
import { z } from 'zod';
5+
import { z } from 'zod/v4';
66

77
const client = new Anthropic();
88

helpers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The SDK provides helpers for parsing structured JSON outputs from Claude using J
108108
```ts
109109
import { zodOutputFormat } from '@anthropic-ai/sdk/helpers/zod';
110110
import Anthropic from '@anthropic-ai/sdk';
111-
import { z } from 'zod';
111+
import { z } from 'zod/v4';
112112

113113
const client = new Anthropic();
114114

@@ -183,7 +183,7 @@ The SDK provides helper functions to create runnable tools that can be automatic
183183
184184
```ts
185185
import { betaZodTool } from '@anthropic-ai/sdk/helpers/beta/zod';
186-
import { z } from 'zod';
186+
import { z } from 'zod/v4';
187187

188188
const weatherTool = betaZodTool({
189189
name: 'get_weather',

src/helpers/beta/zod.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { transformJSONSchema } from '../..//lib/transform-json-schema';
2-
import type { infer as zodInfer, ZodType } from 'zod';
32
import * as z from 'zod/v4';
43
import { AnthropicError } from '../../core/error';
54
import { AutoParseableBetaOutputFormat } from '../../lib/beta-parser';
@@ -14,9 +13,9 @@ import { BetaToolResultContentBlockParam } from '../../resources/beta';
1413
* This can be passed directly to the `.create()` method but will not
1514
* result in any automatic parsing, you'll have to parse the response yourself.
1615
*/
17-
export function betaZodOutputFormat<ZodInput extends ZodType>(
16+
export function betaZodOutputFormat<ZodInput extends z.ZodType>(
1817
zodObject: ZodInput,
19-
): AutoParseableBetaOutputFormat<zodInfer<ZodInput>> {
18+
): AutoParseableBetaOutputFormat<z.infer<ZodInput>> {
2019
let jsonSchema = z.toJSONSchema(zodObject, { reused: 'ref' });
2120

2221
jsonSchema = transformJSONSchema(jsonSchema);
@@ -46,15 +45,15 @@ export function betaZodOutputFormat<ZodInput extends ZodType>(
4645
* converted into JSON Schema when passed to the API. The provided function's
4746
* input arguments will also be validated against the provided schema.
4847
*/
49-
export function betaZodTool<InputSchema extends ZodType>(options: {
48+
export function betaZodTool<InputSchema extends z.ZodType>(options: {
5049
name: string;
5150
inputSchema: InputSchema;
5251
description: string;
5352
run: (
54-
args: zodInfer<InputSchema>,
53+
args: z.infer<InputSchema>,
5554
context?: BetaToolRunContext,
5655
) => Promisable<string | Array<BetaToolResultContentBlockParam>>;
57-
}): BetaRunnableTool<zodInfer<InputSchema>> {
56+
}): BetaRunnableTool<z.infer<InputSchema>> {
5857
const jsonSchema = z.toJSONSchema(options.inputSchema, { reused: 'ref' });
5958

6059
if (jsonSchema.type !== 'object') {
@@ -70,6 +69,6 @@ export function betaZodTool<InputSchema extends ZodType>(options: {
7069
input_schema: objectSchema,
7170
description: options.description,
7271
run: options.run,
73-
parse: (args: unknown) => options.inputSchema.parse(args) as zodInfer<InputSchema>,
72+
parse: (args: unknown) => options.inputSchema.parse(args) as z.infer<InputSchema>,
7473
};
7574
}

src/helpers/zod.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { transformJSONSchema } from '../lib/transform-json-schema';
2-
import type { infer as zodInfer, ZodType } from 'zod';
32
import * as z from 'zod/v4';
43
import { AnthropicError } from '../core/error';
54
import { AutoParseableOutputFormat } from '../lib/parser';
@@ -13,9 +12,9 @@ import { AutoParseableOutputFormat } from '../lib/parser';
1312
* This can be passed directly to the `.create()` method but will not
1413
* result in any automatic parsing, you'll have to parse the response yourself.
1514
*/
16-
export function zodOutputFormat<ZodInput extends ZodType>(
15+
export function zodOutputFormat<ZodInput extends z.ZodType>(
1716
zodObject: ZodInput,
18-
): AutoParseableOutputFormat<zodInfer<ZodInput>> {
17+
): AutoParseableOutputFormat<z.infer<ZodInput>> {
1918
let jsonSchema = z.toJSONSchema(zodObject, { reused: 'ref' });
2019

2120
jsonSchema = transformJSONSchema(jsonSchema);

tests/helpers/beta/zod.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as z from 'zod';
1+
import * as z from 'zod/v4';
22
import { betaZodTool } from '../../../src/helpers/beta/zod';
33

44
describe('zod helpers', () => {

tests/helpers/zod.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { z } from 'zod';
1+
import { z } from 'zod/v4';
22
import { betaZodOutputFormat } from '../../src/helpers/beta/zod';
33
import { zodOutputFormat } from '../../src/helpers/zod';
44

0 commit comments

Comments
 (0)