Skip to content

Chat.Send does not seem to support function responses #413

@rhartert

Description

@rhartert

Chat.Send does not seem to correctly set the role when responding with FunctionResponse. Error returned:

Error 400, Message: Please ensure that function response turn comes immediately after a function call turn., Status: INVALID_ARGUMENT, Details: []

One working solution is to update the Send method to set the role to "function" instead of "user".

Environment details

  • Programming language: Go
  • OS: Macos 15.5
  • Language runtime version: 1.23
  • Package version: v1.14

Steps to reproduce

Here's an example to reproduce the issue:

package main

import (
	"context"
	"fmt"
	"log"

	"google.golang.org/genai"
)

func main() {
	ctx := context.Background()

	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		Backend: genai.BackendGeminiAPI,
	})
	if err != nil {
		log.Fatalf("Error creating client: %v\n", err)
	}

	fridgeContentFunc := &genai.FunctionDeclaration{
		Name:        "fridgeContent",
		Description: "Get the current content of the fridge.",
	}

	modelCfg := &genai.GenerateContentConfig{
		Tools: []*genai.Tool{{
			FunctionDeclarations: []*genai.FunctionDeclaration{
				fridgeContentFunc,
			}},
		},
		ToolConfig: &genai.ToolConfig{
			FunctionCallingConfig: &genai.FunctionCallingConfig{
				Mode: genai.FunctionCallingConfigModeAuto,
			},
		},
	}

	chat, err := client.Chats.Create(ctx, "gemini-2.5-flash", modelCfg, []*genai.Content{})
	if err != nil {
		log.Fatalf("Error creating chat: %v\n", err)
	}

	nextParts := []*genai.Part{genai.NewPartFromText("What's in the fridge?")}
	for {
		resp, err := chat.Send(ctx, nextParts...)
		if err != nil {
			log.Fatalf("Error sending message: %v\n", err)
		}

		if len(resp.FunctionCalls()) == 0 {
			fmt.Println(resp.Text())
			break
		}

		nextParts = nextParts[:0] // reset
		for _, fc := range resp.FunctionCalls() {
			nextParts = append(nextParts, genai.NewPartFromFunctionResponse(
				fc.Name,
				map[string]any{"content": "pizza"},
			))
		}
	}
}

Metadata

Metadata

Labels

api:gemini-apiIssues related to Gemini APIpriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions