Skip to content

fix(client): check for empty ToolCalls before accessing #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions packages/ssestream/ssestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,6 @@ func NewStream[T any](decoder Decoder, err error) *Stream[T] {
}
}

var eventPrefixesToBeParsed = []string{
"response.",
"image_generation.",
"image_generation.",
"transcript.",
}

// Next returns false if the stream has ended or an error occurred.
// Call Stream.Current() to get the current value.
// Call Stream.Err() to get the error.
Expand Down Expand Up @@ -172,15 +165,7 @@ func (s *Stream[T]) Next() bool {

var nxt T

hasPrefixToParse := false
for _, prefix := range eventPrefixesToBeParsed {
if strings.HasPrefix(s.decoder.Event().Type, prefix) {
hasPrefixToParse = true
break
}
}

if s.decoder.Event().Type == "" || hasPrefixToParse {
if s.decoder.Event().Type == "" || !strings.HasPrefix(s.decoder.Event().Type, "thread.") {
ep := gjson.GetBytes(s.decoder.Event().Data, "error")
if ep.Exists() {
s.err = fmt.Errorf("received error while streaming: %s", ep.String())
Expand Down
4 changes: 3 additions & 1 deletion streamaccumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ func (prev *chatCompletionResponseState) update(chunk ChatCompletionChunk) (just
new.state = refusalResponseState
case delta.JSON.ToolCalls.Valid():
new.state = toolResponseState
new.index = int(delta.ToolCalls[0].Index)
if len(delta.ToolCalls) > 0 {
new.index = int(delta.ToolCalls[0].Index)
}
default:
new.state = finishedResponseState
}
Expand Down