Skip to content

Commit f86dda1

Browse files
authored
fix(go/plugins/googlegenai): include thoughtSignature in part metadata (#4006)
1 parent 8348ed6 commit f86dda1

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

go/plugins/googlegenai/gemini.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ func translateCandidate(cand *genai.Candidate) (*ai.ModelResponse, error) {
835835
if part.FileData != nil {
836836
partFound++
837837
p = ai.NewMediaPart(part.FileData.MIMEType, part.FileData.FileURI)
838+
838839
}
839840
if part.FunctionCall != nil {
840841
partFound++
@@ -872,6 +873,13 @@ func translateCandidate(cand *genai.Candidate) (*ai.ModelResponse, error) {
872873
continue
873874
}
874875

876+
if len(part.ThoughtSignature) > 0 {
877+
if p.Metadata == nil {
878+
p.Metadata = make(map[string]any)
879+
}
880+
p.Metadata["signature"] = part.ThoughtSignature
881+
}
882+
875883
msg.Content = append(msg.Content, p)
876884
}
877885
m.Message = msg
@@ -928,37 +936,29 @@ func toGeminiParts(parts []*ai.Part) ([]*genai.Part, error) {
928936

929937
// toGeminiPart converts a [ai.Part] to a [genai.Part].
930938
func toGeminiPart(p *ai.Part) (*genai.Part, error) {
939+
var gp *genai.Part
931940
switch {
932941
case p.IsReasoning():
933-
// NOTE: go-genai does not support genai.NewPartFromThought()
934-
signature := []byte{}
935-
if p.Metadata != nil {
936-
if sig, ok := p.Metadata["signature"].([]byte); ok {
937-
signature = sig
938-
}
939-
}
940-
return &genai.Part{
941-
Thought: true,
942-
Text: p.Text,
943-
ThoughtSignature: signature,
944-
}, nil
942+
gp = genai.NewPartFromText(p.Text)
943+
gp.Thought = true
945944
case p.IsText():
946-
return genai.NewPartFromText(p.Text), nil
945+
gp = genai.NewPartFromText(p.Text)
947946
case p.IsMedia():
948947
if strings.HasPrefix(p.Text, "data:") {
949948
contentType, data, err := uri.Data(p)
950949
if err != nil {
951950
return nil, err
952951
}
953-
return genai.NewPartFromBytes(data, contentType), nil
952+
gp = genai.NewPartFromBytes(data, contentType)
953+
} else {
954+
gp = genai.NewPartFromURI(p.Text, p.ContentType)
954955
}
955-
return genai.NewPartFromURI(p.Text, p.ContentType), nil
956956
case p.IsData():
957957
contentType, data, err := uri.Data(p)
958958
if err != nil {
959959
return nil, err
960960
}
961-
return genai.NewPartFromBytes(data, contentType), nil
961+
gp = genai.NewPartFromBytes(data, contentType)
962962
case p.IsToolResponse():
963963
toolResp := p.ToolResponse
964964
var output map[string]any
@@ -970,7 +970,6 @@ func toGeminiPart(p *ai.Part) (*genai.Part, error) {
970970
"content": toolResp.Output,
971971
}
972972
}
973-
974973
var isMultipart bool
975974
if multiPart, ok := p.Metadata["multipart"].(bool); ok {
976975
isMultipart = multiPart
@@ -1005,8 +1004,16 @@ func toGeminiPart(p *ai.Part) (*genai.Part, error) {
10051004
}
10061005
return fc, nil
10071006
default:
1008-
panic("unknown part type in a request")
1007+
return nil, fmt.Errorf("unknown part in the request: %q", p.Kind)
10091008
}
1009+
1010+
if p.Metadata != nil {
1011+
if sig, ok := p.Metadata["signature"].([]byte); ok {
1012+
gp.ThoughtSignature = sig
1013+
}
1014+
}
1015+
1016+
return gp, nil
10101017
}
10111018

10121019
// validToolName checks whether the provided tool name matches the

0 commit comments

Comments
 (0)