Skip to content

Commit 27915f7

Browse files
authored
feat: #561 support both zod3 and zod4 (#609)
1 parent 0e01da0 commit 27915f7

31 files changed

+960
-101
lines changed

.changeset/goofy-llamas-listen.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@openai/agents-extensions': minor
3+
'@openai/agents-realtime': minor
4+
'@openai/agents-openai': minor
5+
'@openai/agents-core': minor
6+
'@openai/agents': minor
7+
---
8+
9+
feat: #561 support both zod3 and zod4

integration-tests/deno.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Deno', () => {
1313
await execa`deno install`;
1414
}, 60000);
1515

16-
test('should be able to run', async () => {
16+
test('should be able to run', { timeout: 60000 }, async () => {
1717
const { stdout } = await execa`deno --allow-net --allow-env main.ts`;
1818
expect(stdout).toContain('[RESPONSE]Hello there![/RESPONSE]');
1919
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, test, expect, beforeAll } from 'vitest';
2+
import { execa as execaBase } from 'execa';
3+
4+
const execa = execaBase({
5+
cwd: './integration-tests/node-zod3',
6+
env: {
7+
...process.env,
8+
NODE_OPTIONS: '',
9+
TS_NODE_PROJECT: '',
10+
TS_NODE_COMPILER_OPTIONS: '',
11+
},
12+
});
13+
14+
describe('Node.js', () => {
15+
beforeAll(async () => {
16+
// remove lock file to avoid errors
17+
console.log('[node] Removing node_modules');
18+
await execa`rm -rf node_modules`;
19+
console.log('[node] Installing dependencies');
20+
await execa`npm install`;
21+
}, 60000);
22+
23+
test('should be able to run using CommonJS', async () => {
24+
const { stdout } = await execa`npm run start:cjs`;
25+
expect(stdout).toContain('[RESPONSE]Hello there![/RESPONSE]');
26+
});
27+
28+
test('should be able to run using ESM', async () => {
29+
const { stdout } = await execa`npm run start:esm`;
30+
expect(stdout).toContain('[RESPONSE]Hello there![/RESPONSE]');
31+
});
32+
});

integration-tests/node-zod3/.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@openai:registry=http://localhost:4873
2+
package-lock=false
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @ts-check
2+
3+
const {
4+
Agent,
5+
run,
6+
tool,
7+
setTraceProcessors,
8+
ConsoleSpanExporter,
9+
BatchTraceProcessor,
10+
} = require('@openai/agents');
11+
12+
const { z } = require('zod');
13+
14+
setTraceProcessors([new BatchTraceProcessor(new ConsoleSpanExporter())]);
15+
16+
const getWeatherTool = tool({
17+
name: 'get_weather',
18+
description: 'Get the weather for a given city',
19+
parameters: z.object({ city: z.string() }),
20+
execute: async (input) => {
21+
return `The weather in ${input.city} is sunny`;
22+
},
23+
});
24+
25+
const agent = new Agent({
26+
name: 'Test Agent',
27+
instructions:
28+
'You will always only respond with "Hello there!". Not more not less.',
29+
tools: [getWeatherTool],
30+
});
31+
32+
async function main() {
33+
const result = await run(agent, 'Hey there!');
34+
console.log(`[RESPONSE]${result.finalOutput}[/RESPONSE]`);
35+
}
36+
37+
main().catch(console.error);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @ts-check
2+
3+
import { z } from 'zod';
4+
5+
import {
6+
Agent,
7+
run,
8+
tool,
9+
setTraceProcessors,
10+
ConsoleSpanExporter,
11+
BatchTraceProcessor,
12+
} from '@openai/agents';
13+
14+
setTraceProcessors([new BatchTraceProcessor(new ConsoleSpanExporter())]);
15+
16+
const getWeatherTool = tool({
17+
name: 'get_weather',
18+
description: 'Get the weather for a given city',
19+
parameters: z.object({ city: z.string() }),
20+
execute: async (input) => {
21+
return `The weather in ${input.city} is sunny`;
22+
},
23+
});
24+
25+
const agent = new Agent({
26+
name: 'Test Agent',
27+
instructions:
28+
'You will always only respond with "Hello there!". Not more not less.',
29+
tools: [getWeatherTool],
30+
});
31+
32+
const result = await run(agent, 'What is the weather in San Francisco?');
33+
console.log(`[RESPONSE]${result.finalOutput}[/RESPONSE]`);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"private": true,
3+
"type": "commonjs",
4+
"scripts": {
5+
"start:cjs": "node --no-experimental-require-module index.cjs",
6+
"start:esm": "node --no-experimental-require-module index.mjs"
7+
},
8+
"dependencies": {
9+
"@openai/agents": "latest",
10+
"typescript": "^5.9.3",
11+
"zod": "^3.25.40"
12+
}
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, test, expect, beforeAll } from 'vitest';
2+
import { execa as execaBase } from 'execa';
3+
4+
const execa = execaBase({
5+
cwd: './integration-tests/node-zod4',
6+
env: {
7+
...process.env,
8+
NODE_OPTIONS: '',
9+
TS_NODE_PROJECT: '',
10+
TS_NODE_COMPILER_OPTIONS: '',
11+
},
12+
});
13+
14+
describe('Node.js', () => {
15+
beforeAll(async () => {
16+
// remove lock file to avoid errors
17+
console.log('[node] Removing node_modules');
18+
await execa`rm -rf node_modules`;
19+
console.log('[node] Installing dependencies');
20+
await execa`npm install`;
21+
}, 60000);
22+
23+
test('should be able to run using CommonJS', async () => {
24+
const { stdout } = await execa`npm run start:cjs`;
25+
expect(stdout).toContain('[RESPONSE]Hello there![/RESPONSE]');
26+
});
27+
28+
test('should be able to run using ESM', async () => {
29+
const { stdout } = await execa`npm run start:esm`;
30+
expect(stdout).toContain('[RESPONSE]Hello there![/RESPONSE]');
31+
});
32+
});

integration-tests/node-zod4/.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@openai:registry=http://localhost:4873
2+
package-lock=false
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @ts-check
2+
3+
const {
4+
Agent,
5+
run,
6+
tool,
7+
setTraceProcessors,
8+
ConsoleSpanExporter,
9+
BatchTraceProcessor,
10+
} = require('@openai/agents');
11+
12+
const { z } = require('zod');
13+
14+
setTraceProcessors([new BatchTraceProcessor(new ConsoleSpanExporter())]);
15+
16+
const getWeatherTool = tool({
17+
name: 'get_weather',
18+
description: 'Get the weather for a given city',
19+
parameters: z.object({ city: z.string() }),
20+
execute: async (input) => {
21+
return `The weather in ${input.city} is sunny`;
22+
},
23+
});
24+
25+
const agent = new Agent({
26+
name: 'Test Agent',
27+
instructions:
28+
'You will always only respond with "Hello there!". Not more not less.',
29+
tools: [getWeatherTool],
30+
});
31+
32+
async function main() {
33+
const result = await run(agent, 'Hey there!');
34+
console.log(`[RESPONSE]${result.finalOutput}[/RESPONSE]`);
35+
}
36+
37+
main().catch(console.error);

0 commit comments

Comments
 (0)