Skip to content

Commit

Permalink
OpenAI: fix accessing choices when using completion
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxLeiter committed Aug 18, 2023
1 parent 4da195b commit 7b389a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-toes-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix: improve safety for type check in openai-stream
14 changes: 12 additions & 2 deletions packages/core/streams/openai-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,21 @@ type OpenAIStreamReturnTypes = ExtractType<AsyncIterableOpenAIStreamReturnTypes>
function isChatCompletionChunk(
data: OpenAIStreamReturnTypes
): data is ChatCompletionChunk {
return 'choices' in data && 'delta' in data.choices[0]
return (
'choices' in data &&
data.choices &&
data.choices[0] &&
'delta' in data.choices[0]
)
}

function isCompletion(data: OpenAIStreamReturnTypes): data is Completion {
return 'choices' in data && 'text' in data.choices[0]
return (
'choices' in data &&
data.choices &&
data.choices[0] &&
'text' in data.choices[0]
)
}

export function OpenAIStream(
Expand Down

0 comments on commit 7b389a7

Please sign in to comment.