Skip to content

Commit

Permalink
fix(helpers/zod): avoid import issue in certain environments (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored and stainless-app[bot] committed Sep 4, 2024
1 parent 195fff8 commit 1e9808a
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 8 deletions.
5 changes: 5 additions & 0 deletions ecosystem-tests/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const projectRunners = {
'node-ts-esm': defaultNodeRunner,
'node-ts-esm-web': defaultNodeRunner,
'node-ts-esm-auto': defaultNodeRunner,
'node-ts-es2020': async () => {
await installPackage();
await run('npm', ['run', 'tsc']);
await run('npm', ['run', 'main']);
},
'node-js': async () => {
await installPackage();
await run('node', ['test.js']);
Expand Down
36 changes: 36 additions & 0 deletions ecosystem-tests/node-ts-es2020/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { zodResponseFormat } from 'openai/helpers/zod';
import OpenAI from 'openai/index';
import { z } from 'zod';

const Step = z.object({
explanation: z.string(),
output: z.string(),
});

const MathResponse = z.object({
steps: z.array(Step),
final_answer: z.string(),
});

async function main() {
const client = new OpenAI();

const completion = await client.beta.chat.completions.parse({
model: 'gpt-4o-2024-08-06',
messages: [
{ role: 'system', content: 'You are a helpful math tutor.' },
{ role: 'user', content: 'solve 8x + 31 = 2' },
],
response_format: zodResponseFormat(MathResponse, 'math_response'),
});

console.dir(completion, { depth: 5 });

const message = completion.choices[0]?.message;
if (message?.parsed) {
console.log(message.parsed.steps);
console.log(`answer: ${message.parsed.final_answer}`);
}
}

main();
193 changes: 193 additions & 0 deletions ecosystem-tests/node-ts-es2020/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ecosystem-tests/node-ts-es2020/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "node-ts-es2020",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"tsc": "tsc && tsc -p tsconfig.nodenext.json && tsc -p node_modules/openai/src/tsconfig.json",
"main": "ts-node index.ts"
},
"dependencies": {
"ts-node": "^10.9.2",
"zod": "^3.23.8"
},
"overrides": {
"@types/node": "20.4.2"
}
}
37 changes: 37 additions & 0 deletions ecosystem-tests/node-ts-es2020/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"lib": ["es2020", "dom"],
"allowJs": false,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"removeComments": true,
"forceConsistentCasingInFileNames": true,
"downlevelIteration": true,
"strict": true,
"moduleResolution": "node",
"paths": {},
"typeRoots": ["node_modules/@types"],
"types": ["node"],
"allowSyntheticDefaultImports": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"incremental": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"preserveSymlinks": true,
"suppressImplicitAnyIndexErrors": true
},
"exclude": ["node_modules"]
}
18 changes: 18 additions & 0 deletions ecosystem-tests/node-ts-es2020/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "./tsconfig.base.json",
"ts-node": {
"swc": true,
"transpileOnly": true
},
"compilerOptions": {
"declaration": false,
"declarationMap": false,
"allowJs": true,
"checkJs": false,
"outDir": "./dist",
"baseUrl": "./",
"types": ["node", "jest"],
"paths": {}
},
"include": ["index.ts", "tsconfig.json", "jest.config.ts", ".eslintrc.js"]
}
55 changes: 55 additions & 0 deletions ecosystem-tests/node-ts-es2020/tsconfig.nodenext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"include": ["tests/*.ts", "index.ts"],
"exclude": ["tests/*-shim-errors.ts"],

"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Projects */
"incremental": true,

/* Language and Environment */
"target": "ES2015",
"lib": ["ES2015"],
"jsx": "react",

/* Modules */
"module": "commonjs",
"rootDir": "./",
"moduleResolution": "NodeNext",
"baseUrl": "./",
"paths": {
"~/*": ["*"]
},
"resolveJsonModule": true,
"composite": true,

/* Emit */
"outDir": "node_modules",
"noEmit": true,

/* Interop Constraints */
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
/* "esModuleInterop": true, */
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"checkJs": true,

/* Experimental Features */
"experimentalDecorators": true,

/* Type Checking */
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"skipLibCheck": false
}
}
Loading

0 comments on commit 1e9808a

Please sign in to comment.