This code sample demonstrates how to use Generative AI model to generate content for a given text input

## Code sample

### C++


Before trying this sample, follow the C++ 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 C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/aiplatform/latest).


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).

    namespace vertex_ai = ::google::cloud::aiplatform_v1;
    namespace vertex_ai_proto = ::google::cloud::aiplatform::v1;
    [](std::string const& project_id, std::string const& location_id,
       std::string const& model, std::vector<std::string> const& content) {
      google::cloud::Location location(project_id, location_id);
      auto client = vertex_ai::PredictionServiceClient(
          vertex_ai::MakePredictionServiceConnection(location.location_id()));

      std::vector<vertex_ai_proto::Content> contents;
      for (auto const& c : content) {
        vertex_ai_proto::Content content;
        content.set_role("user");
        content.add_parts()->set_text(c);
        contents.push_back(std::move(content));
      }
      auto response = client.GenerateContent(
          location.FullName() + "/publishers/google/models/" + model, contents);
      if (!response) throw std::move(response).status();

      for (auto const& candidate : response->candidates()) {
        for (auto const& p : candidate.content().parts()) {
          std::cout << p.text() << "\n";
        }
      }
    }

### C#


Before trying this sample, follow the C# 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 C# API
reference documentation](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest).


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).


    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.html;
    using System;
    using System.Threading.Tasks;

    public class TextInputSample
    {
        public async Task<string> TextInput(
            string projectId = "your-project-id",
            string location = "us-central1",
            string publisher = "google",
            string model = "gemini-2.0-flash-001")
        {

            var predictionServiceClient = new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.PredictionServiceClientBuilder.html
            {
                Endpoint = $"{location}-aiplatform.googleapis.com"
            }.Build();
            string prompt = @"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?";

            var generateContentRequest = new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.GenerateContentRequest.html
            {
                Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
                Contents =
                {
                    new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.Content.html
                    {
                        Role = "USER",
                        Parts =
                        {
                            new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.Part.html { Text = prompt }
                        }
                    }
                }
            };

            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.GenerateContentResponse.html response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

            string responseText = response.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.GenerateContentResponse.html#Google_Cloud_AIPlatform_V1_GenerateContentResponse_Candidates[0].https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.Content.html.Parts[0].Text;
            Console.WriteLine(responseText);

            return responseText;
        }
    }

### 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"
    	"fmt"
    	"io"

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

    func generateContentFromText(w io.Writer, projectID 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("error creating client: %w", err)
    	}
    	gemini := client.GenerativeModel(modelName)
    	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(
    		"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?")

    	resp, err := gemini.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("error generating content: %w", err)
    	}
    	// See the JSON response in
    	// https://pkg.go.dev/cloud.google.com/go/vertexai/genai#GenerateContentResponse.
    	rb, err := json.MarshalIndent(resp, "", "  ")
    	if err != nil {
    		return fmt.Errorf("json.MarshalIndent: %w", err)
    	}
    	fmt.Fprintln(w, string(rb))
    	return nil
    }

### Java


Before trying this sample, follow the Java 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 Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1).


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 com.google.cloud.vertexai.https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.VertexAI.html;
    import com.google.cloud.vertexai.api.https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.api.GenerateContentResponse.html;
    import com.google.cloud.vertexai.generativeai.https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.generativeai.GenerativeModel.html;
    import com.google.cloud.vertexai.generativeai.https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.generativeai.ResponseHandler.html;
    import java.io.IOException;

    public class TextInput {

      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
        String projectId = "your-google-cloud-project-id";
        String location = "us-central1";
        String modelName = "gemini-2.5-flash";
        String textPrompt =
            "What's a good name for a flower shop that specializes in selling bouquets of"
                + " dried flowers?";

        String output = textInput(projectId, location, modelName, textPrompt);
        System.out.println(output);
      }

      // Passes the provided text input to the Gemini model and returns the text-only response.
      // For the specified textPrompt, the model returns a list of possible store names.
      public static String textInput(
          String projectId, String location, String modelName, String textPrompt) throws IOException {
        // Initialize client that will be used to send requests. This client only needs
        // to be created once, and can be reused for multiple requests.
        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.VertexAI.html vertexAI = new https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.VertexAI.html(projectId, location)) {
          https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.generativeai.GenerativeModel.html model = new https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.generativeai.GenerativeModel.html(modelName, vertexAI);

          https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.api.GenerateContentResponse.html response = model.https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.generativeai.GenerativeModel.html#com_google_cloud_vertexai_generativeai_GenerativeModel_generateContent_com_google_cloud_vertexai_api_Content_(textPrompt);
          String output = https://docs.cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.generativeai.ResponseHandler.html.getText(response);
          return output;
        }
      }
    }

### Node.js


Before trying this sample, follow the Node.js 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 Node.js API
reference documentation](https://docs.cloud.google.com/nodejs/docs/reference/aiplatform/latest).


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).

    const {GoogleGenAI} = require('@google/genai');
    /**
     * TODO(developer): Update these variables before running the sample.
     */
    async function generate_from_text_input(
      projectId = 'PROJECT_ID',
      model = 'gemini-2.5-flash'
    ) {
      const client = new GoogleGenAI({
        vertexai: true,
        project: projectId,
        location: 'us-central1',
      });

      const prompt =
        "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?";

      const response = await client.models.generateContent({
        model: model,
        contents: prompt,
      });

      console.log(response.text);
    }

### Python


Before trying this sample, follow the Python 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 Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/aiplatform/latest).


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 https://docs.cloud.google.com/python/docs/reference/vertexai/latest
    from vertexai.generative_models import https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.generative_models.GenerativeModel.html

    # TODO(developer): Update and un-comment below line
    # PROJECT_ID = "your-project-id"
    https://docs.cloud.google.com/python/docs/reference/vertexai/latest.init(project=PROJECT_ID, location="us-central1")

    model = GenerativeModel("gemini-2.0-flash-001")

    response = model.https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.generative_models.GenerativeModel.html#vertexai_preview_generative_models_GenerativeModel_generate_content(
        "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?"
    )

    print(response.text)
    # Example response:
    # **Emphasizing the Dried Aspect:**
    # * Everlasting Blooms
    # * Dried & Delightful
    # * The Petal Preserve
    # ...

## 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).