Get answers and follow-ups (Legacy)

This page introduces search with answer and follow-ups for Gemini Enterprise and shows you how to implement it using method calls.

Search with answer and follow-ups is based on the answer method. The answer method also has some important additional features, such as the ability to handle complex queries.

Features of the answer method

Key features of the answer method are as follows:

The features of answer and follow-ups can be divided into three phases of the query, search and answer:

When to use answer and when to use search

Gemini Enterprise has two methods that are used for querying apps. They have different but overlapping features.

Use the answer method when:

Use the search method when:

Use the answer and search methods together when:

Query phase features

The answer and follow-ups feature supports natural language query processing.

This section describes and illustrates the various options for query rephrasing and classification.

Query rephrasing

Query rephrasing is on by default. This feature chooses the best way to rephrase queries automatically to improve search results. This feature can also handle queries that don't require rephrasing.

Query classification

Query classification options are to identify adversarial queries and non-answer seeking queries. By default, query classification options are off.

For more information about adversarial and non-answer seeking queries, see Ignore adversarial queries and Ignore non-summary seeking queries.

Search phase features

For searching, the answer method has the same options as the search method. For example:

Answer phase features

During the answer phase, when answers are generated from the search results, you can enable the same features as in the search method. For example:

Additional answer phase features that are not available in the search method are:

To receive multimodal answers that include charts or images in addition to text, the following options are available:

Search and answer (basic)

The following command shows how to call the answer method and return a generated answer and a list of search results, with links to the sources.

This command shows the required input only. The options are left at their defaults.

REST

To search and get results with a generated answer, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"}
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Query phase commands

    This section shows how to specify options for the query phase of the answer method call.

    Search and answer (rephrasing disabled)

    The following command shows how to call the answer method and return a generated answer and a list of search results. The answer could be different from the preceding answer because the rephrasing option is disabled.

REST

To search and get results with a generated answer without applying query rephrasing, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "queryUnderstandingSpec": {
               "queryRephraserSpec": {
                  "disable": true
            }
        }
          }'
    
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Search and answer (specify maximum steps)

    The following command shows how to call the answer method and return a generated answer and a list of search results. The answer is different from the preceding answers because the number of rephrasing steps has been increased.

REST

To search and get results with a generated answer allowing up to five rephrasing steps, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "queryUnderstandingSpec": {
                "queryRephraserSpec": {
                    "maxRephraseSteps": MAX_REPHRASE
                 }
             }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Search and answer with query classification

    The following command shows how to call the answer method to inquire whether a query is adversarial, non-answer seeking, or neither.

    The response includes the classification type for the query, but the answer itself is not affected by the classification. If you want to change the answer behavior according to the query type, you can do this in the answer phase. See Ignore adversarial queries and Ignore non-summary seeking queries.

REST

To determine if a query is adversarial or non-answer seeking, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "queryUnderstandingSpec": {
                "queryClassificationSpec": {
                    "types": ["QUERY_CLASSIFICATION_TYPE"]
                 }
             }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Search phase commands: Search and answer with search result options

    This section shows how to specify options for the search phase portion of the answer method call, options such as setting the maximum number of documents returned, boosting, and filtering, and how to get an answer when you supply your own search results.

    The following command shows how to call the answer method and specify various options for how the search result is returned. (The search results are independent of the answer.)

REST

To set various options related to which and how search results are returned, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
              "searchSpec": {
              "searchParams": {
                "maxReturnResults": MAX_RETURN_RESULTS,
                "filter": "FILTER",
                "boostSpec": BOOST_SPEC,
                "orderBy": "ORDER_BY",
                "searchResultMode": SEARCH_RESULT_MODE
               }
             }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Answer phase commands

    This section shows how to customize the answer method call. You can combine the following options as needed.

    Ignore adversarial queries and non-answer-seeking queries

    The following command shows how to avoid answering adversarial queries and non-answer-seeking queries when calling the answer method.

REST

To skip answering queries that are adversarial or non-answer-seeking, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "answerGenerationSpec": {
               "ignoreAdversarialQuery": true,
               "ignoreNonAnswerSeekingQuery": true
            }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Show only relevant answers

    Gemini Enterprise can assess how relevant the results are to a query. If no results are determined to be sufficiently relevant, then instead of generating an answer from non-relevant or minimally-relevant results, you can choose to return a fallback answer: "We do not have a summary for your query."

    The following command shows how to return the fallback answer in the case of irrelevant results when calling the answer method.

REST

To return a fallback answer if no relevant results are found, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "answerGenerationSpec": {
               "ignoreLowRelevantContent": true
            }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Return grounding support scores

    The following command shows how to return grounding support scores for answers and claims.

REST

To return a support score for each claim (sentence in the answer) and an aggregated support score for the answer, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "groundingSpec": {
               "includeGroundingSupports": true,
            }
          }'
    

    Replace the following:

    Show only well-grounded answers

    The following command shows how to return only those answers that are deemed to be well grounded in the corpus, the information in the data store. Poorly-grounded answers are filtered out.

    You choose a low or high level threshold for the grounding support score. Then, the answer is only returned if it meets or exceeds that level. You can experiment with the two filter thresholds and with no threshold to determine what level of filter is likely to provide the best results for your users.

REST

To return an answer only if it meets a support-score threshold, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "groundingSpec": {
               "filteringLevel": "FILTER_LEVEL"
            }
          }'
    

    Replace the following:

    Specify the answer model

    The following command shows how to change the model version used to generate answers.

    For information about the supported models, see Answer generation model versions and lifecycle.

REST

To generate an answer using a model different from the default model, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "answerGenerationSpec": {
               "modelSpec": {
                  "modelVersion": "MODEL_VERSION",
               }
             }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Specify a custom preamble

    The following command shows how to set a preamble for the generated answer. A preamble contains natural language instructions for customizing the answer. You can request customizations such as length, level of detail, style of output (such as "simple"), language of output, focus of answer, and format (such as tables, bullets, and XML). For example, a preamble might be "Explain like you are a ten years old kid."

    The preamble can have a significant effect on the quality of the generated answer. For information about what to write in preambles and examples of good preambles, see About custom preambles.

REST

To generate an answer using a model different from the default model, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "answerGenerationSpec": {
               "promptSpec": {
                   "preamble": "PREAMBLE",
               }
            }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Include citations

    The following command shows how to request citations to be included with the answer.

    REST

    To generate an answer using a model different from the default model, do the following:

    1. Run the following curl command:

      curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
        "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
        -d '{
              "query": { "text": "QUERY"},
              "answerGenerationSpec": {
                 "includeCitations": INCLUDE_CITATIONS
              }
            }'
      

      Replace the following:

      • PROJECT_ID: the ID of your project.
      • APP_ID: the ID of the app that you want to query.
      • QUERY: a free-text string that contains the question or search query.
      • INCLUDE_CITATIONS: specifies whether to include citation metadata in the answer. The default value is false.

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Set the answer language code

    The following command shows how to set the language code for answers.

REST

To generate an answer using a model different from the default model, do the following:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "answerGenerationSpec": {
               "answerLanguageCode": "ANSWER_LANGUAGE_CODE"
               }
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.api_core.client_options import ClientOptions
    from google.cloud import discoveryengine_v1 as discoveryengine
    
    # TODO(developer): Uncomment these variables before running the sample.
    # project_id = "YOUR_PROJECT_ID"
    # location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
    # engine_id = "YOUR_APP_ID"
    
    
    def answer_query_sample(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.AnswerQueryResponse:
        #  For more information, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
        client_options = (
            ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
            if location != "global"
            else None
        )
    
        # Create a client
        client = discoveryengine.ConversationalSearchServiceClient(
            client_options=client_options
        )
    
        # The full resource name of the Search serving config
        serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_serving_config"
    
        # Optional: Options for query phase
        # The `query_understanding_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/QueryUnderstandingSpec
        query_understanding_spec = discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec(
            query_rephraser_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryRephraserSpec(
                disable=False,  # Optional: Disable query rephraser
                max_rephrase_steps=1,  # Optional: Number of rephrase steps
            ),
            # Optional: Classify query types
            query_classification_spec=discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec(
                types=[
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.ADVERSARIAL_QUERY,
                    discoveryengine.AnswerQueryRequest.QueryUnderstandingSpec.QueryClassificationSpec.Type.NON_ANSWER_SEEKING_QUERY,
                ]  # Options: ADVERSARIAL_QUERY, NON_ANSWER_SEEKING_QUERY or both
            ),
        )
    
        # Optional: Options for answer phase
        # The `answer_generation_spec` below includes all available query phase options.
        # For more details, refer to https://cloud.google.com/generative-ai-app-builder/docs/reference/rest/v1/AnswerGenerationSpec
        answer_generation_spec = discoveryengine.AnswerQueryRequest.AnswerGenerationSpec(
            ignore_adversarial_query=False,  # Optional: Ignore adversarial query
            ignore_non_answer_seeking_query=False,  # Optional: Ignore non-answer seeking query
            ignore_low_relevant_content=False,  # Optional: Return fallback answer when content is not relevant
            model_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.ModelSpec(
            # Use the 2026 stable production model for answer generation
            model_version="gemini-2.5-flash/answer_gen/stable",
            ),
            prompt_spec=discoveryengine.AnswerQueryRequest.AnswerGenerationSpec.PromptSpec(
                preamble="Give a detailed answer.",  # Optional: Natural language instructions for customizing the answer.
            ),
            include_citations=True,  # Optional: Include citations in the response
            answer_language_code="en",  # Optional: Language code of the answer
        )
    
        # Initialize request argument(s)
        request = discoveryengine.AnswerQueryRequest(
            serving_config=serving_config,
            query=discoveryengine.Query(text="What is Vertex AI Search?"),
            session=None,  # Optional: include previous session ID to continue a conversation
            query_understanding_spec=query_understanding_spec,
            answer_generation_spec=answer_generation_spec,
            user_pseudo_id="user-pseudo-id",  # Optional: Add user pseudo-identifier for queries.
        )
    
        # Make the request
        response = client.answer_query(request)
    
        # Handle the response
        print(response)
    
        return response
    
    

    Personalize answers

    If there is specific information about the user available—for example, data in a profile, you can specify that information in the endUserMetadata object so that the query results can be personalized for the user.

    For example, if a user is searching for information about upgrading a mobile phone, information from their profile, such as current phone model and cellular plan provides information that can personalize the generated answer.

    To add personal information about the user making a query and to generate an answer that takes into account the personal information, do the following:

    1. Run the following curl command:

      curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
        "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
          "query": { "text": "QUERY"},
          "endUserSpec": {
             "endUserMetadata": [
               {
                 "chunkInfo": {
                    "content": "PERSONALIZED_INFO",
                    "documentMetadata":  { "title": "INFO_DESCRIPTION"}
                 }
               }
             ]
          }
        }'
      

      Replace the following:

      • PROJECT_ID: the ID of your project.
      • APP_ID: the ID of the app that you want to query.
      • QUERY: a free-text string that contains the question or search query.
      • PERSONALIZATION_INFO: a string that contains information specific to the user who is making the query. For example, This customer has a Pixel 6 Pro purchased over a period of 24-months starting 2023-01-15. This customer is on the Business Plus International plan. No payment is due at this time. The length limit for this string is 8,000 characters.
      • INFO_DESCRIPTION: a string that briefly describes the personalization information—for example, Customer profile data, including model, plan, and billing status. The model makes use of both this description and the personalization information when generating a customized answer to the query.

    Generate charts for answers

    The answer method can generate charts and return them as part of the answer to a query.

    You can specifically request that an answer include a chart, for example, "Plot the year-on-year growth rate of small business payments over years with available data". If the system determines that sufficient data is present, a chart is returned. Usually, some answer text is returned along with the chart.

    Also, if there is sufficient data to create a chart, the answer method can return a chart even if the query didn't explicitly request a chart. For example, "What was the improvement in HDI scores associated with more access to clean drinking water in the decade between 2010 and 2020?"

    Only one chart is generated per answer. However, the chart might be a composite chart, containing other smaller charts. An example of a composite chart:

    composite chart contains four smaller charts

    Limitation

    Queries must be in English.

    Common failure scenarios

    You won't always get an image returned with your answer. If there isn't sufficient data, a figure can't be generated.

    Other failure scenarios include code execution failures and timeouts. If either of these happen, rephrase your query and try again.

    Before you begin

    Before you run a query that asks for generated charts, do the following:

    Procedure

REST

Call the answer method as follows to return an answer that can include a chart generated from the data in the data store:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "answerGenerationSpec": {
              "model_spec": {
                 "model_version": "MODEL_VERSION"
             },
              "multimodalSpec": {
                 "imageSource": "IMAGE_SOURCE"
                 }
            }
          }'
    

    Replace the following:

    Retrieve existing images from the data store

    You can choose to have images from the data store returned with the answer and in citation references. The data store must be an unstructured data store with the layout parser turned on.

    When imageSource is CORPUS_IMAGE_ONLY or ALL_AVAILABLE_SOURCES, then the answer method can retrieve images from the data store as appropriate. However, turning this on doesn't mean that images are always returned.

    You get one image (maximum) per answer. Citations can contain multiple images.

    Limitations

    Procedure

REST

Call the answer method as follows to return an answer that can include an image from the data store in the answer:

  1. Run the following curl command:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
      -d '{
            "query": { "text": "QUERY"},
            "answerGenerationSpec": {
              "model_spec": {
                 "model_version": "MODEL_VERSION"
              },
              includeCitations: true,
              "multimodalSpec": {
                 "imageSource": "IMAGE_SOURCE"
                 }
            }
          }'
    

    Replace the following:

    Commands for follow-up questions

    Follow-ups are multi-turn queries. After the first query in a follow-up session, subsequent "turns" take into account prior interactions. With follow-ups, the answer method can also suggest related questions, which your users can choose instead of entering their own follow-up questions.

    All the answer and follow-ups features described in the preceding sections, such as citations, filters, SafeSearch, ignoring certain types of queries, and using a preamble to customize answers can be applied along with follow-ups.

    Example of a follow-up session

    The following is an example of a session with follow-ups. Suppose that you want to know about vacationing in Mexico:

    Without follow-ups, your question "What is the exchange rate?" wouldn't be answerable because regular search wouldn't know that you wanted the Mexican exchange rate. Similarly, without follow-ups, there wouldn't be the context needed to give you temperatures specifially for Mexico.

    When you ask "What is the best time of the year to vacation in Mexico?", in addition to answering your question, answer and follow-ups can suggest other questions that you might ask, such as "What is the cheapest month to vacation in Mexico?" and "What are the tourist months in Mexico?".

    After the related questions feature is enabled, questions are returned as strings in the response.

    About sessions

    To understand how follow-ups work in Gemini Enterprise, you need to understand about sessions.

    A session is made up of text queries provided by a user and responses provided by Gemini Enterprise.

    These query and response pairs are sometimes referred to as turns. In the preceding example, the second turn is made up of "What is the exchange rate?" and "1 USD is equal to approximately 17.65 Mexican pesos."

    Sessions are stored with the app. In the app, a session is represented by the session resource.

    In addition to containing the query and response messages, the session resource has:

    Store session information and get responses

    You can use the command line to generate search responses and answers and to store these, along with each query in a session.

    REST

    To use the command line to create a session and generate responses from the user's input, follow these steps:

    1. Specify the app where you want to store the session:

      curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
        "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions" \
        -d '{
              "userPseudoId": "USER_PSEUDO_ID"
            }'
      

      Replace the following:

      • PROJECT_ID: the ID of your project.

      • APP_ID: the ID of the app.

      • USER_PSEUDO_ID: this is a unique identifier for tracking a search visitor. For example, you can implement this with an HTTP cookie, which uniquely identifies a visitor on a single device.

      Example command and result

      curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)"
      -H "Content-Type: application/json"
      "https://discoveryengine.googleapis.com/v1/projects/my-project-123/locations/global/collections/default_collection/engines/my-app/sessions"
      -d '{
      "userPseudoId": "test_user"
      }'
      
      { "name": "projects/123456/locations/global/collections/default_collection/engines/my-app/sessions/16002628354770206943", "state": "IN_PROGRESS", "userPseudoId": "test_user", "startTime": "2024-09-13T18:47:10.465311Z", "endTime": "2024-09-13T18:47:10.465311Z" }
    2. Note down the session ID, the numbers at the end of the name: field in the JSON response. In the example result, the ID is 5386462384953257772. You need this ID in the next step.

    3. Generate an answer and add it to a session in your app:

      curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
        "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:answer" \
        -d '{
              "query": { "text": "QUERY"},
              "session": "projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions/SESSION_ID",
                "searchSpec":{ "searchParams": {"filter": "FILTER"} }
      }'
      

      Replace the following:

      • PROJECT_ID: the ID of your project.
      • APP_ID: the ID of the app.
      • QUERY: a free-text string that contains the question or search query.
      • SESSION_ID: the ID for the session that you created in step 1. These are the digits at the end of the name: field, noted step 2. For a session, use the same session ID in every turn.
      • FILTER: a text field for filtering search using a filter expression. The default value is an empty string. The way you construct your filter varies depending on whether you have unstructured data with metadata, structured data, or website data. For more information, see Filter custom search for structured or unstructured data.

      Example command and result

      curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)"
      -H "Content-Type: application/json"
      "https://discoveryengine.googleapis.com/v1/projects/my-project-123/locations/global/collections/default_collection/engines/my-app/servingConfigs/default_search:answer"
      -d '{
      "query": { "text": "Compare bigquery with spanner database?"},
      "session":  "projects/123456/locations/global/collections/default_collection/engines/my-app/sessions/16002628354770206943",
      }'
          
      { "answer": { "name": "projects/123456/locations/global/collections/default_collection/engines/my-app/sessions/16002628354770206943/answers/4861507376861383072", "state": "SUCCEEDED", "answerText": "BigQuery and Spanner are both powerful tools that can be used together to handle transactional and analytical workloads. Spanner is a fully managed relational database optimized for transactional workloads, while BigQuery is a serverless data warehouse designed for business agility. Spanner provides seamless replication across regions in Google Cloud and processes over 1 billion requests per second at peak. BigQuery analyzes over 110 terabytes of data per second. Users can leverage federated queries to read data from Spanner and write to a native BigQuery table. \n", "steps": [ { "state": "SUCCEEDED", "description": "Rephrase the query and search.", "actions": [ { "searchAction": { "query": "Compare bigquery with spanner database?" }, "observation": { "searchResults": [ { "document": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/branches/0/documents/ecc0e7547253f4ca3ff3328ce89995af", "uri": "https://cloud.google.com/blog/topics/developers-practitioners/how-spanner-and-bigquery-work-together-handle-transactional-and-analytical-workloads", "title": "How Spanner and BigQuery work together to handle transactional and analytical workloads | Google Cloud Blog", "snippetInfo": [ { "snippet": "Using Cloud \u003cb\u003eSpanner\u003c/b\u003e and \u003cb\u003eBigQuery\u003c/b\u003e also allows customers to build their \u003cb\u003edata\u003c/b\u003e clouds using Google Cloud, a unified, open approach to \u003cb\u003edata\u003c/b\u003e-driven transformation ...", "snippetStatus": "SUCCESS" } ] }, { "document": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/branches/0/documents/d7e238f73608a860e00b752ef80e2941", "uri": "https://cloud.google.com/blog/products/databases/cloud-spanner-gets-stronger-with-bigquery-federated-queries", "title": "Cloud Spanner gets stronger with BigQuery-federated queries | Google Cloud Blog", "snippetInfo": [ { "snippet": "As enterprises compete for market share, their need for real-time insights has given rise to increased demand for transactional \u003cb\u003edatabases\u003c/b\u003e to support \u003cb\u003edata\u003c/b\u003e ...", "snippetStatus": "SUCCESS" } ] }, { "document": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/branches/0/documents/e10a5a3c267dc61579e7c00fefe656eb", "uri": "https://cloud.google.com/blog/topics/developers-practitioners/replicating-cloud-spanner-bigquery-scale", "title": "Replicating from Cloud Spanner to BigQuery at scale | Google Cloud Blog", "snippetInfo": [ { "snippet": "... \u003cb\u003eSpanner data\u003c/b\u003e into \u003cb\u003eBigQuery\u003c/b\u003e for analytics. In this post, you will learn how to efficiently use this feature to replicate large tables with high throughput ...", "snippetStatus": "SUCCESS" } ] }, ... { "document": "projects/123456/locations/global/collections/default_collection/dataStores/my-data-store/branches/0/documents/8100ad36e1cac149eb9fc180a41d8f25", "uri": "https://cloud.google.com/blog/products/gcp/from-nosql-to-new-sql-how-spanner-became-a-global-mission-critical-database", "title": "How Spanner became a global, mission-critical database | Google Cloud Blog", "snippetInfo": [ { "snippet": "... SQL \u003cb\u003evs\u003c/b\u003e. NoSQL dichotomy may no longer be relevant." The \u003cb\u003eSpanner\u003c/b\u003e SQL query processor, while recognizable as a standard implementation, has unique ...", "snippetStatus": "SUCCESS" } ] } ] } } ] } ] }, "session": { "name": "projects/123456/locations/global/collections/default_collection/engines/my-app/sessions/16002628354770206943", "state": "IN_PROGRESS", "userPseudoId": "test_user", "turns": [ { "query": { "queryId": "projects/123456/locations/global/questions/741830", "text": "Compare bigquery with spanner database?" }, "answer": "projects/123456/locations/global/collections/default_collection/engines/my-app/sessions/16002628354770206943/answers/4861507376861383072" } ], "startTime": "2024-09-13T18:47:10.465311Z", "endTime": "2024-09-13T18:47:10.465311Z" }, "answerQueryToken": "NMwKDAjFkpK3BhDU24uZAhIkNjZlNDIyZWYtMDAwMC0yMjVmLWIxMmQtZjQwMzA0M2FkYmNj" }
    4. Repeat step 3 for each new query in the session.

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.cloud import discoveryengine_v1 as discoveryengine
    
    
    def create_session(
        project_id: str,
        location: str,
        engine_id: str,
        user_pseudo_id: str,
    ) -> discoveryengine.Session:
        """Creates a session.
    
        Args:
            project_id: The ID of your Google Cloud project.
            location: The location of the app.
            engine_id: The ID of the app.
            user_pseudo_id: A unique identifier for tracking visitors. For example, this
              could be implemented with an HTTP cookie, which should be able to
              uniquely identify a visitor on a single device.
        Returns:
            discoveryengine.Session: The newly created Session.
        """
    
        client = discoveryengine.SessionServiceClient()
    
        session = client.create_session(
            # The full resource name of the engine
            parent=f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}",
            session=discoveryengine.Session(user_pseudo_id=user_pseudo_id),
        )
    
        # Send Session name in `answer_query()`
        print(f"Session: {session.name}")
        return session
    
    

    Get a session from the data store

    The following command shows how to call the get method and get a session from the data store.

REST

To get a session from a data store, do the following:

  1. Run the following curl command:

    curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions/SESSION_ID"
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.cloud import discoveryengine_v1 as discoveryengine
    
    
    def get_session(
        project_id: str,
        location: str,
        engine_id: str,
        session_id: str,
    ) -> discoveryengine.Session:
        """Retrieves a session.
    
        Args:
            project_id: The ID of your Google Cloud project.
            location: The location of the app.
            engine_id: The ID of the app.
            session_id: The ID of the session.
        """
    
        client = discoveryengine.SessionServiceClient()
    
        # The full resource name of the session
        name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/sessions/{session_id}"
    
        session = client.get_session(name=name)
    
        print(f"Session details: {session}")
        return session
    
    

    Delete a session from the app

    The following command shows how to call the delete method and delete a session from the data store.

    By default, sessions older than 60 days are automatically deleted. However, if you want to delete a particular session—for example, if it contains sensitive content, then use this API call to delete it.

REST

To delete a session from an app, do the following:

  1. Run the following curl command:

    curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions/SESSION_ID"
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.cloud import discoveryengine_v1 as discoveryengine
    
    
    def delete_session(
        project_id: str,
        location: str,
        engine_id: str,
        session_id: str,
    ) -> None:
        """Deletes a session.
    
        Args:
            project_id: The ID of your Google Cloud project.
            location: The location of the app.
            engine_id: The ID of the app.
            session_id: The ID of the session.
        """
    
        client = discoveryengine.SessionServiceClient()
    
        # The full resource name of the session
        name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/sessions/{session_id}"
    
        client.delete_session(name=name)
    
        print(f"Session {name} deleted.")
    
    

    Update a session

    There are various reasons that you might want to update a session. For example, to do one of the following:

    The following command shows how to call the patch method and update a session in the data store.

REST

To update a session from an app, do the following:

  1. Run the following curl command:

    curl -X PATCH \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions/SESSION_ID?updateMask=state" \
      -d '{
            "state": "NEW_STATE"
          }'
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.cloud import discoveryengine_v1 as discoveryengine
    from google.protobuf import field_mask_pb2
    
    
    def update_session(
        project_id: str,
        location: str,
        engine_id: str,
        session_id: str,
    ) -> discoveryengine.Session:
        """Updates a session.
    
        Args:
            project_id: The ID of your Google Cloud project.
            location: The location of the app.
            engine_id: The ID of the app.
            session_id: The ID of the session.
        Returns:
            discoveryengine.Session: The updated Session.
        """
        client = discoveryengine.SessionServiceClient()
    
        # The full resource name of the session
        name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/sessions/{session_id}"
    
        session = discoveryengine.Session(
            name=name,
            state=discoveryengine.Session.State.IN_PROGRESS,  # Options: IN_PROGRESS, STATE_UNSPECIFIED
        )
    
        # Fields to Update
        update_mask = field_mask_pb2.FieldMask(paths=["state"])
    
        session = client.update_session(session=session, update_mask=update_mask)
        print(f"Updated session: {session.name}")
        return session
    
    

    List all sessions

    The following command shows how to call the list method and list the sessions in the data store.

REST

To list the sessions for an app, do the following:

  1. Run the following curl command:

    curl -X GET \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions"
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.cloud import discoveryengine_v1 as discoveryengine
    
    
    def list_sessions(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.ListSessionsResponse:
        """Lists all sessions associated with a data store.
    
        Args:
            project_id: The ID of your Google Cloud project.
            location: The location of the app.
            engine_id: The ID of the app.
        Returns:
            discoveryengine.ListSessionsResponse: The list of sessions.
        """
    
        client = discoveryengine.SessionServiceClient()
    
        # The full resource name of the engine
        parent = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}"
    
        response = client.list_sessions(
            request=discoveryengine.ListSessionsRequest(
                parent=parent,
                filter='state="IN_PROGRESS"',  # Optional: Filter requests by userPseudoId or state
                order_by="update_time",  # Optional: Sort results
            )
        )
    
        print("Sessions:")
        for session in response.sessions:
            print(session)
    
        return response
    
    

    List sessions for a user

    The following command shows how to call the list method to list sessions associated with a user or visitor.

REST

To list sessions associated with a user or visitor, do the following:

  1. Run the following curl command:

    curl -X GET \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions?filter=userPseudoId=USER_PSEUDO_ID"
    

    Replace the following:

    Python

    Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

    To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

    from google.cloud import discoveryengine_v1 as discoveryengine
    
    
    def list_sessions(
        project_id: str,
        location: str,
        engine_id: str,
    ) -> discoveryengine.ListSessionsResponse:
        """Lists all sessions associated with a data store.
    
        Args:
            project_id: The ID of your Google Cloud project.
            location: The location of the app.
            engine_id: The ID of the app.
        Returns:
            discoveryengine.ListSessionsResponse: The list of sessions.
        """
    
        client = discoveryengine.SessionServiceClient()
    
        # The full resource name of the engine
        parent = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}"
    
        response = client.list_sessions(
            request=discoveryengine.ListSessionsRequest(
                parent=parent,
                filter='state="IN_PROGRESS"',  # Optional: Filter requests by userPseudoId or state
                order_by="update_time",  # Optional: Sort results
            )
        )
    
        print("Sessions:")
        for session in response.sessions:
            print(session)
    
        return response
    
    

    List sessions for a user and state

    The following command shows how to call the list method to list sessions in a given state for a particular user.

REST

To list sessions for a user that are open or closed and associated with a given user or visitor, do the following:

Run the following curl command:

curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/sessions?filter=userPseudoId=USER_PSEUDO_ID%20AND%20state=STATE"

Replace the following:

Python

Before trying this sample, follow the Python setup instructions in the Gemini Enterprise quickstart using client libraries. For more information, see the Gemini Enterprise Python API reference documentation.

To authenticate to Gemini Enterprise, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

from google.cloud import discoveryengine_v1 as discoveryengine


def list_sessions(
    project_id: str,
    location: str,
    engine_id: str,
) -> discoveryengine.ListSessionsResponse:
    """Lists all sessions associated with a data store.

    Args:
        project_id: The ID of your Google Cloud project.
        location: The location of the app.
        engine_id: The ID of the app.
    Returns:
        discoveryengine.ListSessionsResponse: The list of sessions.
    """

    client = discoveryengine.SessionServiceClient()

    # The full resource name of the engine
    parent = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}"

    response = client.list_sessions(
        request=discoveryengine.ListSessionsRequest(
            parent=parent,
            filter='state="IN_PROGRESS"',  # Optional: Filter requests by userPseudoId or state
            order_by="update_time",  # Optional: Sort results
        )
    )

    print("Sessions:")
    for session in response.sessions:
        print(session)

    return response