Skip to content

Commit c7aec7c

Browse files
authored
extra types (#97)
1 parent 89ae3a9 commit c7aec7c

File tree

6 files changed

+13
-7
lines changed

6 files changed

+13
-7
lines changed

.changeset/stale-parents-rhyme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@instructor-ai/instructor": patch
3+
---
4+
5+
Fixing inference on stream types when using npm or pnpm

bun.lockb

-17.7 KB
Binary file not shown.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"files": [
2020
"dist/**"
2121
],
22-
"typings": "./dist/index.d.ts",
22+
"types": "./dist/index.d.ts",
2323
"scripts": {
2424
"test": "echo \"Error: no test specified\" && exit 1",
2525
"build": "tsup",

src/instructor.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ class Instructor {
202202
): Promise<ReturnTypeBasedOnParams<P>> => {
203203
if (this.isChatCompletionCreateParamsWithModel(params)) {
204204
if (params.stream) {
205-
return this.chatCompletionStream(params) as ReturnTypeBasedOnParams<P>
205+
return this.chatCompletionStream(params) as ReturnTypeBasedOnParams<
206+
P & { stream: true }
207+
>
206208
} else {
207209
return this.chatCompletionStandard(params) as ReturnTypeBasedOnParams<P>
208210
}

src/types/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import OpenAI from "openai"
2-
import type { ChatCompletionCreateParams } from "openai/resources/index.mjs"
3-
import { Stream } from "openai/streaming.mjs"
2+
import { Stream } from "openai/streaming"
43
import { z } from "zod"
54

65
import { MODE } from "@/constants/modes"
@@ -27,10 +26,10 @@ export type InstructorChatCompletionParams<T extends z.AnyZodObject> = {
2726
}
2827

2928
export type ChatCompletionCreateParamsWithModel<T extends z.AnyZodObject> =
30-
InstructorChatCompletionParams<T> & ChatCompletionCreateParams
29+
InstructorChatCompletionParams<T> & OpenAI.ChatCompletionCreateParams
3130

3231
export type ReturnWithoutModel<P> =
33-
P extends { stream: true } ? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
32+
P extends { stream: true } ? OpenAI.Chat.Completions.ChatCompletion
3433
: OpenAI.Chat.Completions.ChatCompletion
3534

3635
export type ReturnTypeBasedOnParams<P> =

tests/inference.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Instructor from "@/instructor"
1010
import { describe, expect, test } from "bun:test"
1111
import OpenAI from "openai"
12-
import { Stream } from "openai/streaming.mjs"
12+
import { Stream } from "openai/streaming"
1313
import { type } from "ts-inference-check"
1414
import { z } from "zod"
1515

0 commit comments

Comments
 (0)