This code sample shows how to execute multiple function calls in parallel and return their results to the model for generating a complete response.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Introduction to function calling](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/tools/function-calling)

## Code sample

### Go


Before trying this sample, follow the Go setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Go API
reference documentation](https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    import (
    	"context"
    	"encoding/json"
    	"errors"
    	"fmt"
    	"io"

    	"cloud.google.com/go/vertexai/genai"
    )

    // parallelFunctionCalling shows how to execute multiple function calls in parallel
    // and return their results to the model for generating a complete response.
    func parallelFunctionCalling(w io.Writer, projectID, location, modelName string) error {
    	// location = "us-central1"
    	// modelName = "gemini-2.0-flash-001"
    	ctx := context.Background()
    	client, err := genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_NewClient(ctx, projectID, location)
    	if err != nil {
    		return fmt.Errorf("failed to create GenAI client: %w", err)
    	}
    	defer client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_Close()

    	model := client.GenerativeModel(modelName)
    	// Set temperature to 0.0 for maximum determinism in function calling.
    	model.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerationConfig_SetTemperature(0.0)

    	funcName := "getCurrentWeather"
    	funcDecl := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionDeclaration{
    		Name:        funcName,
    		Description: "Get the current weather in a given location",
    		Parameters: &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{
    			Type: genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,
    			Properties: map[string]*genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{
    				"location": {
    					Type: genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,
    					Description: "The location for which to get the weather. " +
    						"It can be a city name, a city name and state, or a zip code. " +
    						"Examples: 'San Francisco', 'San Francisco, CA', '95616', etc.",
    				},
    			},
    			Required: []string{"location"},
    		},
    	}
    	// Add the weather function to our model toolbox.
    	model.Tools = []*genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Tool{
    		{
    			FunctionDeclarations: []*genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionDeclaration{funcDecl},
    		},
    	}

    	prompt := genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Text("Get weather details in New Delhi and San Francisco?")
    	resp, err := model.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_GenerateContent(ctx, prompt)

    	if err != nil {
    		return fmt.Errorf("failed to generate content: %w", err)
    	}
    	if len(resp.Candidates) == 0 {
    		return errors.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New("got empty response from model")
    	} else if len(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Candidate_FunctionCalls()) == 0 {
    		return errors.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New("got no function call suggestions from model")
    	}

    	// In a production environment, consider adding validations for function names and arguments.
    	for _, fnCall := range resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Candidate_FunctionCalls() {
    		fmt.Fprintf(w, "The model suggests to call the function %q with args: %v\n", fnCall.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_Name, fnCall.Args)
    		// Example response:
    		// The model suggests to call the function "getCurrentWeather" with args: map[location:New Delhi]
    		// The model suggests to call the function "getCurrentWeather" with args: map[location:San Francisco]
    	}

    	// Use synthetic data to simulate responses from the external API.
    	// In a real application, this would come from an actual weather API.
    	mockAPIResp1, err := json.Marshal(map[string]string{
    		"location":         "New Delhi",
    		"temperature":      "42",
    		"temperature_unit": "C",
    		"description":      "Hot and humid",
    		"humidity":         "65",
    	})
    	if err != nil {
    		return fmt.Errorf("failed to marshal function response to JSON: %w", err)
    	}

    	mockAPIResp2, err := json.Marshal(map[string]string{
    		"location":         "San Francisco",
    		"temperature":      "36",
    		"temperature_unit": "F",
    		"description":      "Cold and cloudy",
    		"humidity":         "N/A",
    	})
    	if err != nil {
    		return fmt.Errorf("failed to marshal function response to JSON: %w", err)
    	}

    	// Note, that the function calls don't have to be chained. We can obtain both responses in parallel
    	// and return them to Gemini at once.
    	funcResp1 := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionResponse{
    		Name: funcName,
    		Response: map[string]any{
    			"content": mockAPIResp1,
    		},
    	}
    	funcResp2 := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionResponse{
    		Name: funcName,
    		Response: map[string]any{
    			"content": mockAPIResp2,
    		},
    	}

    	// Return both API responses to the model allowing it to complete its response.
    	resp, err = model.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_GenerateContent(ctx, prompt, funcResp1, funcResp2)
    	if err != nil {
    		return fmt.Errorf("failed to generate content: %w", err)
    	}
    	if len(resp.Candidates) == 0 || len(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts) == 0 {
    		return errors.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New("got empty response from model")
    	}

    	fmt.Fprintln(w, resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts[0])
    	// Example response:
    	// The weather in New Delhi is hot and humid with a humidity of 65 and a temperature of 42°C. The weather in San Francisco ...

    	return nil
    }

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=generativeaionvertexai).