google / generative-ai-go

Go SDK for Google Generative AI
Apache License 2.0
592 stars 59 forks source link

Return error early in SendMessageStream #214

Closed Bin-Huang closed 2 months ago

Bin-Huang commented 2 months ago

This PR modifies SendMessageStream to return errors early for pre-flight checks like API errors and parameter format errors (400 status codes). This change allows developers to handle these errors more efficiently and write more responsive error-handling code. This enhancement is not to replace error checking during the iteration but to provide the ability to address certain errors upfront.

Previous:

iter := cs.SendMessageStream(ctx, genai.Text("something"))
for {
    resp, err := iter.Next()
    if err == iterator.Done {
        break
    }
    if err != nil {
        log.Fatal(err) // API Key not found
    }
    printResponse(resp)
}

Now:

iter, err := cs.SendMessageStream(ctx, genai.Text("something"))
if err != nil {
    log.Fatal(err) // API Key not found. Now I can handle these errors before iterating
}
eliben commented 2 months ago

Thanks for the contribution. This will be a breaking API change, which I'm not sure the nature of the PR justifies at this point

jba commented 2 months ago

In addition to what Eli said, our convention for the function that creates the iterator is not to return an error. You'll find out as soon as you start iterating. There's one less place in the code that checks for errors.

I disagree that this "allows developers to handle these errors more efficiently and write more responsive error-handling code." I think both designs are about the same.

Sorry for the "no" on this, but we are glad that your are thinking of ways to improve the SDK!