Skip to content
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
25 changes: 15 additions & 10 deletions provider/geminiprovider/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,22 @@ func buildResponsePart(part *genai.Part, contents []message.Content) ([]message.
if part.Thought {
// Thinking model: emit TextReasoningContent. Encode ThoughtSignature as
// base64 in ProtectedData so it can be passed back in multi-turn requests.
protectedData := ""
if len(part.ThoughtSignature) > 0 {
protectedData = base64.StdEncoding.EncodeToString(part.ThoughtSignature)
// Skip a thought part that carries neither thinking text nor a signature,
// consistent with how empty text parts are skipped below; such a part
// would otherwise add an empty, information-free reasoning content.
if part.Text != "" || len(part.ThoughtSignature) > 0 {
protectedData := ""
if len(part.ThoughtSignature) > 0 {
protectedData = base64.StdEncoding.EncodeToString(part.ThoughtSignature)
}
contents = append(contents, &message.TextReasoningContent{
Text: part.Text,
ProtectedData: protectedData,
ContentHeader: message.ContentHeader{
RawRepresentation: part,
},
})
}
contents = append(contents, &message.TextReasoningContent{
Text: part.Text,
ProtectedData: protectedData,
ContentHeader: message.ContentHeader{
RawRepresentation: part,
},
})
} else if part.Text != "" {
contents = append(contents, &message.TextContent{
Text: part.Text,
Expand Down
52 changes: 51 additions & 1 deletion provider/geminiprovider/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,55 @@ func TestFunctionResultMissingCallID(t *testing.T) {
}
}

// TestResponseWithEmptyThoughtPartOmitted verifies that a thought part carrying
// neither text nor a signature does not produce an empty TextReasoningContent.
func TestResponseWithEmptyThoughtPartOmitted(t *testing.T) {
resp := map[string]any{
"candidates": []any{
map[string]any{
"content": map[string]any{
"role": "model",
"parts": []any{
map[string]any{"thought": true}, // empty thought: no text, no signature
map[string]any{"text": "The answer is 42."},
},
},
"finishReason": "STOP",
},
},
"usageMetadata": map[string]any{"promptTokenCount": 10, "candidatesTokenCount": 15, "totalTokenCount": 25},
}
respBody, _ := json.Marshal(resp)

server := httptest.NewServer(captureAndRespond(t, make(chan []byte, 1), "application/json", string(respBody)))
defer server.Close()

a := newTestClient(t, server)

result, err := a.RunText(t.Context(), "hi").Collect()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

var foundText string
for _, msg := range result.Messages {
for _, c := range msg.Contents {
switch content := c.(type) {
case *message.TextReasoningContent:
t.Errorf("expected no reasoning content for an empty thought part, got Text=%q ProtectedData=%q", content.Text, content.ProtectedData)
case *message.TextContent:
foundText += content.Text
}
}
}
// The normal (non-thought) text part must still be emitted; asserting it
// guards against a regression that drops all parts rather than just the
// empty thought.
if foundText != "The answer is 42." {
t.Errorf("text content = %q, want %q", foundText, "The answer is 42.")
}
}

// TestResponseWithThinkingContent verifies that response parts with thought=true
// are translated into TextReasoningContent.
func TestResponseWithThinkingContent(t *testing.T) {
Expand Down Expand Up @@ -1592,7 +1641,8 @@ func TestGenerateContentConfigOption(t *testing.T) {
temp := float32(0.5)
topP := float32(0.9)
topK := float32(20)
_, err := a.RunText(t.Context(), "Test",
_, err := a.RunText(
t.Context(), "Test",
geminiprovider.GenerateContentConfig(genai.GenerateContentConfig{
Temperature: &temp,
TopP: &topP,
Expand Down