Skip to content

Commit

Permalink
streams/openai-stream: don't call onStart/onCompletion when recursing (
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxLeiter committed Jul 21, 2023
1 parent 74e1f35 commit 0ebc2f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-cows-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

streams/openai-stream: don't call onStart/onCompletion when recursing
6 changes: 3 additions & 3 deletions packages/core/streams/ai-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export interface FunctionCallPayload {
* @interface
*/
export interface AIStreamCallbacks {
onStart?: () => Promise<void>
onCompletion?: (completion: string) => Promise<void>
onToken?: (token: string) => Promise<void>
onStart?: () => Promise<void> | void
onCompletion?: (completion: string) => Promise<void> | void
onToken?: (token: string) => Promise<void> | void
}

/**
Expand Down
14 changes: 12 additions & 2 deletions packages/core/streams/openai-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,19 @@ function createFunctionCallTransformer(
return
}

// Recursively
const openAIStream = OpenAIStream(functionResponse, {
// Recursively:

// We don't want to trigger onStart or onComplete recursively
// so we remove them from the callbacks
// see https://github.com/vercel-labs/ai/issues/351
const filteredCallbacks: OpenAIStreamCallbacks = {
...callbacks,
onStart: undefined,
onCompletion: undefined
}

const openAIStream = OpenAIStream(functionResponse, {
...filteredCallbacks,
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
} as AIStreamCallbacks)

Expand Down

0 comments on commit 0ebc2f0

Please sign in to comment.