From bc6170819c35db80238a843d5d658f30e73630d3 Mon Sep 17 00:00:00 2001 From: Amy Wu Date: Tue, 24 Mar 2026 11:31:08 -0700 Subject: [PATCH] fix: fix rag resource parsing Fixes #6442 PiperOrigin-RevId: 888772249 --- tests/unit/vertex_rag/test_rag_data.py | 26 ++++++++++++++++++++++++++ vertexai/rag/utils/_gapic_utils.py | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/unit/vertex_rag/test_rag_data.py b/tests/unit/vertex_rag/test_rag_data.py index 94f6c35bf9..17a59defca 100644 --- a/tests/unit/vertex_rag/test_rag_data.py +++ b/tests/unit/vertex_rag/test_rag_data.py @@ -653,6 +653,32 @@ def test_update_corpus_failure(self): ) e.match("Failed in RagCorpus update due to") + @pytest.mark.usefixtures("rag_data_client_mock") + @pytest.mark.parametrize("corpus_name", ["Capital-Corpus", "123-Corpus"]) + def test_get_corpus_with_valid_resource_name_success(self, corpus_name): + # Tests regex allowing names starting with uppercase or digits. + rag_corpus = rag.get_corpus(corpus_name) + rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS) + + @pytest.mark.usefixtures("rag_data_client_mock") + @pytest.mark.parametrize("file_name", ["Capital-File", "123-File"]) + def test_get_file_with_valid_resource_name_success(self, file_name): + # This test covers the regex change for RAG file names. + rag_file = rag.get_file( + name=file_name, + corpus_name=test_rag_constants.TEST_RAG_CORPUS_ID, + ) + rag_file_eq(rag_file, test_rag_constants.TEST_RAG_FILE) + + def test_get_corpus_with_invalid_name_format_failure(self): + # Verify names starting with invalid characters (like underscore) still fail. + with pytest.raises(ValueError) as e: + rag.get_corpus("_invalid-name") + e.match( + "name must be of the format `projects/{project}/locations/{location}/" + "ragCorpora/{rag_corpus}` or `{rag_corpus}`" + ) + @pytest.mark.usefixtures("rag_data_client_mock") def test_get_corpus_success(self): rag_corpus = rag.get_corpus(test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME) diff --git a/vertexai/rag/utils/_gapic_utils.py b/vertexai/rag/utils/_gapic_utils.py index a27556bfd5..d0311dec16 100644 --- a/vertexai/rag/utils/_gapic_utils.py +++ b/vertexai/rag/utils/_gapic_utils.py @@ -68,7 +68,7 @@ ) -_VALID_RESOURCE_NAME_REGEX = "[a-z][a-zA-Z0-9._-]{0,127}" +_VALID_RESOURCE_NAME_REGEX = "[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}" _VALID_DOCUMENT_AI_PROCESSOR_NAME_REGEX = ( r"projects/[^/]+/locations/[^/]+/processors/[^/]+(?:/processorVersions/[^/]+)?" )