Skip to content

Add code samples for Agent Search - #14591

Open
sailobo wants to merge 4 commits into
GoogleCloudPlatform:mainfrom
sailobo:agent-search-samples
Open

Add code samples for Agent Search#14591
sailobo wants to merge 4 commits into
GoogleCloudPlatform:mainfrom
sailobo:agent-search-samples

Conversation

@sailobo

@sailobo sailobo commented Sep 8, 2026

Copy link
Copy Markdown

Description

Adds 9 supportability-driven Python code samples for Vertex AI Search (Agent Search / Discovery Engine) addressing high-frequency customer implementation patterns:

  1. converse_conversation_sample.py (genappbuilder_converse_conversation): Multi-turn conversational search using ConversationalSearchServiceClient (session initialization and continuation).
  2. create_document_metadata_sample.py (genappbuilder_create_document_metadata): Structured document ingestion passing dictionary metadata directly into Document(struct_data=...).
  3. enable_gemini_layout_parser_sample.py (genappbuilder_enable_gemini_layout_parser): Data store creation with Gemini LLM layout parsing, table/image annotations, and chunking config.
  4. update_data_store_gemini_parser_sample.py (genappbuilder_update_data_store_gemini_parser): Dedicated sample to update an existing data store to enable Gemini layout parser with update_mask.
  5. import_custom_chunks_sample.py (genappbuilder_import_custom_chunks): Bring-Your-Own-Chunks (BYOC) document ingestion with inlined BYOC JSON schema explanation.
  6. poll_lro_robust_sample.py (genappbuilder_poll_lro_robust): Exponential backoff polling for long-running operations (Operation.metadata and error unpacking).
  7. search_chunks_sample.py (genappbuilder_search_chunks): Chunk-mode search query returning chunk content, parent document metadata, and adjacent previous/next snippets.
  8. search_extractive_segments_sample.py (genappbuilder_search_extractive_segments): Multi-data store search with extractive segments and relevance confidence scores.
  9. write_user_event_sample.py (genappbuilder_write_user_event): User event ingestion with search attribution tokens for search quality feedback loops.

Includes comprehensive unit tests in discoveryengine/supportability_samples_test.py.

Reviewers: @holtskinner, @janeday
Internal Reference: cl/972770246

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Adding code samples for Agent Search
@sailobo
sailobo requested review from a team as code owners September 8, 2026 19:27
@product-auto-label product-auto-label Bot added samples Issues that are directly related to samples. api: discoveryengine Issues related to the Discovery Engine API API. labels Sep 8, 2026
@snippet-bot

snippet-bot Bot commented Sep 8, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 9 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several Python code samples and corresponding unit tests for Google Cloud Discovery Engine (Agent Search), covering features like multi-turn conversational search, structured document ingestion, Gemini layout parsing, custom chunk imports, robust LRO polling, and user event recording. The review feedback highlights several improvement opportunities: avoiding the hasattr anti-pattern on Protobuf messages in converse_conversation_sample.py, adding defensive None checks to prevent potential AttributeErrors in create_document_metadata_sample.py and search_extractive_segments_sample.py, and using the idiomatic GetCurrentTime() method for Protobuf timestamps in write_user_event_sample.py.

Comment on lines +99 to +106
if (
hasattr(response.reply, "summary")
and response.reply.summary
and response.reply.summary.summary_text
):
reply_text = response.reply.summary.summary_text
elif hasattr(response.reply, "reply") and response.reply.reply:
reply_text = response.reply.reply

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using hasattr to check for the presence of fields on Protobuf or proto-plus messages is an anti-pattern because these attributes are always defined on the class descriptor, meaning hasattr will always return True regardless of whether the field is set. Instead, directly check the truthiness of the fields, which is more idiomatic and robust.

    if response.reply.summary and response.reply.summary.summary_text:
      reply_text = response.reply.summary.summary_text
    elif response.reply.reply:
      reply_text = response.reply.reply

Comment thread discoveryengine/create_document_metadata_sample.py Outdated
Comment thread discoveryengine/search_extractive_segments_sample.py
Comment on lines +69 to +73
current_time = time.time()
event_time = timestamp_pb2.Timestamp(
seconds=int(current_time),
nanos=int((current_time - int(current_time)) * 1e9),
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of manually calculating seconds and nanoseconds from time.time(), you can use the built-in GetCurrentTime() method on the Protobuf Timestamp object. This simplifies the code and makes it more idiomatic.

  # Set accurate UTC event timestamp
  event_time = timestamp_pb2.Timestamp()
  event_time.GetCurrentTime()

@holtskinner

Copy link
Copy Markdown
Contributor

@sailobo Be sure to register your GitHub account with Google internally. Also address the lint isses https://github.com/GoogleCloudPlatform/python-docs-samples/actions/runs/34269115170/job/102206281775?pr=14591 and the Gemini Code Assist comments

sailobo and others added 3 commits September 8, 2026 20:32
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@sailobo

sailobo commented Sep 9, 2026

Copy link
Copy Markdown
Author

@sailobo Be sure to register your GitHub account with Google internally. Also address the lint isses https://github.com/GoogleCloudPlatform/python-docs-samples/actions/runs/34269115170/job/102206281775?pr=14591 and the Gemini Code Assist comments

@holtskinner I have applied the batched suggestions from the Gemini Code Assist bot to address the review feedback and lint issues. Also, I have double-checked, and my GitHub account is successfully registered internally (I will need to follow up in aligning ldap as per a previous registration error )

Could you please approve the workflow runs so the checks can proceed against these new commits?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: discoveryengine Issues related to the Discovery Engine API API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants