Skip to content
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

Null Pointer Exception in addToHistory #95

Closed
ack233 opened this issue Apr 26, 2024 · 2 comments
Closed

Null Pointer Exception in addToHistory #95

ack233 opened this issue Apr 26, 2024 · 2 comments
Labels
status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working

Comments

@ack233
Copy link

ack233 commented Apr 26, 2024

A null pointer exception occurs in the addToHistory method of the ChatSession struct. This issue is triggered when the Content field of a Candidate struct's first element is nil, and the method attempts to access its Role property (c.Role).

//  github.com/google/generative-ai-go@v0.8.0/genai/client.go:183
func (iter *GenerateContentResponseIterator) Next() (*GenerateContentResponse, error) {
...
    resp, err := iter.sc.Recv()
    iter.err = err
    if err == io.EOF {
        if iter.cs != nil && iter.merged != nil {
            iter.cs.addToHistory(iter.merged.Candidates)
        }
        return nil, iterator.Done
    }
...
}

//  cloud.google.com/go/ai@v0.3.0/generativelanguage/apiv1/generative_client.go:746
func (c *streamGenerateContentRESTClient) Recv() (*generativelanguagepb.GenerateContentResponse, error) {
    if err := c.ctx.Err(); err != nil {
        defer c.stream.Close()
        return nil, err
    }
    msg, err := c.stream.Recv()    // msg.Candidates[0].Content is nil
    if err != nil {
        defer c.stream.Close()
        return nil, err
    }
    res := msg.(*generativelanguagepb.GenerateContentResponse)
    return res, nil
}

// Cause the following function to throw a NullPointerException
// github.com/google/generative-ai-go@v0.8.0/genai/chat.go:63
func (cs *ChatSession) addToHistory(cands []*Candidate) bool {
	if len(cands) > 0 {
		c := cands[0].Content // c is nil
		c.Role = roleModel 
		cs.History = append(cs.History, c)
		return true
	}
	return false
}

截屏2024-04-26 10 32 52

@singhniraj08 singhniraj08 added type:bug Something isn't working status:triaged Issue/PR triaged to the corresponding sub-team labels Apr 26, 2024
@jba
Copy link
Collaborator

jba commented Apr 27, 2024

This bug is fixed in the latest version of github.com/google/generative-ai-go (v0.11.0).
The fix for the vertex client is at googleapis/google-cloud-go#10057.

@ack233
Copy link
Author

ack233 commented Apr 28, 2024

This bug is fixed in the latest version of github.com/google/generative-ai-go (v0.11.0). The fix for the vertex client is at googleapis/google-cloud-go#10057.

Thanks for the update. I've upgraded to the latest version and the issue has been resolved.

@ack233 ack233 closed this as completed Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants