Skip to content

Commit 205c6cb

Browse files
authored
updates and test for supporting zod defaults (#86)
1 parent 378f8a9 commit 205c6cb

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

.changeset/three-swans-push.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@instructor-ai/instructor": patch
3+
---
4+
5+
update zodstream and schema stream to support zod defaults

bun.lockb

0 Bytes
Binary file not shown.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"homepage": "https://github.com/instructor-ai/instructor-js#readme",
5252
"dependencies": {
53-
"zod-stream": "^0.0.1",
53+
"zod-stream": "^0.0.4",
5454
"zod-to-json-schema": "^3.22.3",
5555
"zod-validation-error": "^2.1.0"
5656
},

tests/zod-type.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Instructor from "@/instructor"
2+
import { describe, expect, test } from "bun:test"
3+
import OpenAI from "openai"
4+
import { z } from "zod"
5+
6+
async function extractUser({
7+
schema
8+
}) {
9+
10+
const oai = new OpenAI({
11+
apiKey: process.env.OPENAI_API_KEY ?? undefined,
12+
organization: process.env.OPENAI_ORG_ID ?? undefined
13+
})
14+
15+
const client = Instructor({
16+
client: oai,
17+
mode: "TOOLS"
18+
})
19+
20+
const user = await client.chat.completions.create({
21+
messages: [{ role: "user", content: "do nothing" }],
22+
model: "gpt-3.5-turbo",
23+
response_model: { schema: schema, name: "User" },
24+
seed: 1
25+
})
26+
27+
return user
28+
}
29+
30+
describe("zod-schema test", () => {
31+
test("Should return default values", async () => {
32+
const UserSchema = z.object({
33+
age: z.number().default(30),
34+
name: z.string().default("Jason Liu"),
35+
favoriteFood: z.string().default("Pizza"),
36+
favoriteColor: z.string().default("Blue")
37+
})
38+
39+
const user = await extractUser({
40+
schema: UserSchema
41+
})
42+
console.log("test", user)
43+
44+
expect(user.name).toEqual("Jason Liu")
45+
expect(user.age).toEqual(30)
46+
expect(user.favoriteFood).toEqual("Pizza")
47+
expect(user.favoriteColor).toEqual("Blue")
48+
})
49+
})

0 commit comments

Comments
 (0)