From 3089984565b324120462609d22052ee5ead896da Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:27:58 +0530 Subject: [PATCH 01/14] chore: Refactor query in sample to run on background thread --- .../create_embeddings.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/samples/langchain_on_vertexai/create_embeddings.py b/samples/langchain_on_vertexai/create_embeddings.py index 105a86df..fff527db 100644 --- a/samples/langchain_on_vertexai/create_embeddings.py +++ b/samples/langchain_on_vertexai/create_embeddings.py @@ -31,6 +31,13 @@ from langchain_google_cloud_sql_pg import PostgresEngine, PostgresVectorStore +async def run_on_background(engine: PostgresEngine, coro: Coroutine) -> Any: + """Runs a coroutine on the engine's background loop.""" + if engine._default_loop: + return await asyncio.wrap_future( + asyncio.run_coroutine_threadsafe(coro, engine._default_loop) + ) + return await coro async def create_databases(): engine = await PostgresEngine.afrom_instance( @@ -41,10 +48,12 @@ async def create_databases(): user=USER, password=PASSWORD, ) - async with engine._pool.connect() as conn: - await conn.execute(text("COMMIT")) - await conn.execute(text(f'DROP DATABASE IF EXISTS "{DATABASE}"')) - await conn.execute(text(f'CREATE DATABASE "{DATABASE}"')) + async def _logic(): + async with engine._pool.connect() as conn: + await conn.execute(text("COMMIT")) + await conn.execute(text(f'DROP DATABASE IF EXISTS "{DATABASE}"')) + await conn.execute(text(f'CREATE DATABASE "{DATABASE}"')) + await run_on_background(engine, _logic()) await engine.close() @@ -95,7 +104,7 @@ async def grant_select(engine): engine, table_name=TABLE_NAME, embedding_service=VertexAIEmbeddings( - model_name="textembedding-gecko@latest", project=PROJECT_ID + model_name="text-embedding-005", project=PROJECT_ID ), ) From e01e8a811d31d00f2f21c93def19853ddd6540d6 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:29:07 +0530 Subject: [PATCH 02/14] Update prebuilt_langchain_agent_template.py --- .../langchain_on_vertexai/prebuilt_langchain_agent_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/langchain_on_vertexai/prebuilt_langchain_agent_template.py b/samples/langchain_on_vertexai/prebuilt_langchain_agent_template.py index 472b9da9..9e492783 100644 --- a/samples/langchain_on_vertexai/prebuilt_langchain_agent_template.py +++ b/samples/langchain_on_vertexai/prebuilt_langchain_agent_template.py @@ -65,7 +65,7 @@ def similarity_search(query: str) -> list[Document]: engine, table_name=TABLE_NAME, embedding_service=VertexAIEmbeddings( - model_name="textembedding-gecko@latest", project=PROJECT_ID + model_name="text-embedding-005", project=PROJECT_ID ), ) retriever = vector_store.as_retriever() From 4d9bf30682567347198468f1dd42f01d95a37658 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:29:26 +0530 Subject: [PATCH 03/14] Update embedding model name in retriever agent --- .../retriever_agent_with_history_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/langchain_on_vertexai/retriever_agent_with_history_template.py b/samples/langchain_on_vertexai/retriever_agent_with_history_template.py index 7d8a520e..2867d041 100644 --- a/samples/langchain_on_vertexai/retriever_agent_with_history_template.py +++ b/samples/langchain_on_vertexai/retriever_agent_with_history_template.py @@ -91,7 +91,7 @@ def set_up(self): engine, table_name=self.table, embedding_service=VertexAIEmbeddings( - model_name="textembedding-gecko@latest", project=self.project + model_name="text-embedding-005", project=self.project ), ) retriever = vector_store.as_retriever() From eca5db12ce25e01f98e7514d7b42bd9b83ff91a2 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:29:43 +0530 Subject: [PATCH 04/14] Update model name for Vertex AI embeddings --- samples/langchain_on_vertexai/retriever_chain_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/langchain_on_vertexai/retriever_chain_template.py b/samples/langchain_on_vertexai/retriever_chain_template.py index d05780c3..8abfbb21 100644 --- a/samples/langchain_on_vertexai/retriever_chain_template.py +++ b/samples/langchain_on_vertexai/retriever_chain_template.py @@ -97,7 +97,7 @@ def set_up(self): engine, table_name=self.table, embedding_service=VertexAIEmbeddings( - model_name="textembedding-gecko@latest", project=self.project + model_name="text-embedding-005", project=self.project ), ) retriever = vector_store.as_retriever() From 0533ddaa9798ac94e6ffc2b7ea3725080558fa53 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:31:25 +0530 Subject: [PATCH 05/14] Run table deletion logic in background coroutine Refactor delete_tables to run logic in background coroutine. --- samples/langchain_on_vertexai/clean_up.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/samples/langchain_on_vertexai/clean_up.py b/samples/langchain_on_vertexai/clean_up.py index 45e57ae5..21321797 100644 --- a/samples/langchain_on_vertexai/clean_up.py +++ b/samples/langchain_on_vertexai/clean_up.py @@ -31,6 +31,13 @@ TEST_NAME = os.getenv("DISPLAY_NAME") +async def run_on_background(engine: PostgresEngine, coro: Coroutine) -> Any: + """Runs a coroutine on the engine's background loop to avoid ConnectorLoopError.""" + if engine._default_loop: + return await asyncio.wrap_future( + asyncio.run_coroutine_threadsafe(coro, engine._default_loop) + ) + return await coro async def delete_tables(): engine = await PostgresEngine.afrom_instance( @@ -42,10 +49,14 @@ async def delete_tables(): password=PASSWORD, ) - async with engine._pool.connect() as conn: - await conn.execute(text("COMMIT")) - await conn.execute(text(f"DROP TABLE IF EXISTS {TABLE_NAME}")) - await conn.execute(text(f"DROP TABLE IF EXISTS {CHAT_TABLE_NAME}")) + async def _logic(): + async with engine._pool.connect() as conn: + async with engine._pool.connect() as conn: + await conn.execute(text("COMMIT")) + await conn.execute(text(f"DROP TABLE IF EXISTS {TABLE_NAME}")) + await conn.execute(text(f"DROP TABLE IF EXISTS {CHAT_TABLE_NAME}")) + await run_on_background(engine, _logic()) + await engine.close() await engine._connector.close_async() From 49e67d22b2c084bfefc6a5b8748ee510caed0469 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:33:21 +0530 Subject: [PATCH 06/14] Update google-cloud-aiplatform version to 1.121.0 --- samples/langchain_on_vertexai/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/langchain_on_vertexai/requirements.txt b/samples/langchain_on_vertexai/requirements.txt index f841a4c3..064bf76a 100644 --- a/samples/langchain_on_vertexai/requirements.txt +++ b/samples/langchain_on_vertexai/requirements.txt @@ -1,4 +1,4 @@ -google-cloud-aiplatform[reasoningengine,langchain]==1.120.0 +google-cloud-aiplatform[reasoningengine,langchain]==1.121.0 google-cloud-resource-manager==1.14.2 langchain-community==0.3.31 langchain-google-cloud-sql-pg==0.14.1 From e373c3d86f5d6be4141d4516a5c367ecd4153444 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:35:50 +0530 Subject: [PATCH 07/14] Update create_embeddings.py --- samples/langchain_on_vertexai/create_embeddings.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/samples/langchain_on_vertexai/create_embeddings.py b/samples/langchain_on_vertexai/create_embeddings.py index fff527db..0005f903 100644 --- a/samples/langchain_on_vertexai/create_embeddings.py +++ b/samples/langchain_on_vertexai/create_embeddings.py @@ -31,6 +31,7 @@ from langchain_google_cloud_sql_pg import PostgresEngine, PostgresVectorStore + async def run_on_background(engine: PostgresEngine, coro: Coroutine) -> Any: """Runs a coroutine on the engine's background loop.""" if engine._default_loop: @@ -39,6 +40,7 @@ async def run_on_background(engine: PostgresEngine, coro: Coroutine) -> Any: ) return await coro + async def create_databases(): engine = await PostgresEngine.afrom_instance( PROJECT_ID, @@ -48,11 +50,13 @@ async def create_databases(): user=USER, password=PASSWORD, ) + async def _logic(): async with engine._pool.connect() as conn: await conn.execute(text("COMMIT")) await conn.execute(text(f'DROP DATABASE IF EXISTS "{DATABASE}"')) await conn.execute(text(f'CREATE DATABASE "{DATABASE}"')) + await run_on_background(engine, _logic()) await engine.close() From a65bc9ecb50311dada48237fdd7561c9b690cd90 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:40:45 +0530 Subject: [PATCH 08/14] Update create_embeddings.py --- samples/langchain_on_vertexai/create_embeddings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/langchain_on_vertexai/create_embeddings.py b/samples/langchain_on_vertexai/create_embeddings.py index 0005f903..370d8262 100644 --- a/samples/langchain_on_vertexai/create_embeddings.py +++ b/samples/langchain_on_vertexai/create_embeddings.py @@ -13,6 +13,7 @@ # limitations under the License. import asyncio import uuid +from typing import Any, Coroutine from config import ( CHAT_TABLE_NAME, From 8b5c93081184e9f5b48ee519407a78afb7e40a43 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:40:56 +0530 Subject: [PATCH 09/14] Update clean_up.py --- samples/langchain_on_vertexai/clean_up.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/langchain_on_vertexai/clean_up.py b/samples/langchain_on_vertexai/clean_up.py index 21321797..1564d67f 100644 --- a/samples/langchain_on_vertexai/clean_up.py +++ b/samples/langchain_on_vertexai/clean_up.py @@ -13,6 +13,7 @@ # limitations under the License. import asyncio import os +from typing import Any, Coroutine from config import ( CHAT_TABLE_NAME, From 28c1c63c7c1ffbfa8c8635de0d0945c544273878 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:51:04 +0530 Subject: [PATCH 10/14] Update clean_up.py --- samples/langchain_on_vertexai/clean_up.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/langchain_on_vertexai/clean_up.py b/samples/langchain_on_vertexai/clean_up.py index 1564d67f..f0b3ba31 100644 --- a/samples/langchain_on_vertexai/clean_up.py +++ b/samples/langchain_on_vertexai/clean_up.py @@ -52,7 +52,6 @@ async def delete_tables(): async def _logic(): async with engine._pool.connect() as conn: - async with engine._pool.connect() as conn: await conn.execute(text("COMMIT")) await conn.execute(text(f"DROP TABLE IF EXISTS {TABLE_NAME}")) await conn.execute(text(f"DROP TABLE IF EXISTS {CHAT_TABLE_NAME}")) From 9d8f5de22b5c862600222ceb25cf268dcd8a30b3 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:53:47 +0530 Subject: [PATCH 11/14] Update clean_up.py --- samples/langchain_on_vertexai/clean_up.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/samples/langchain_on_vertexai/clean_up.py b/samples/langchain_on_vertexai/clean_up.py index f0b3ba31..45e57ae5 100644 --- a/samples/langchain_on_vertexai/clean_up.py +++ b/samples/langchain_on_vertexai/clean_up.py @@ -13,7 +13,6 @@ # limitations under the License. import asyncio import os -from typing import Any, Coroutine from config import ( CHAT_TABLE_NAME, @@ -32,13 +31,6 @@ TEST_NAME = os.getenv("DISPLAY_NAME") -async def run_on_background(engine: PostgresEngine, coro: Coroutine) -> Any: - """Runs a coroutine on the engine's background loop to avoid ConnectorLoopError.""" - if engine._default_loop: - return await asyncio.wrap_future( - asyncio.run_coroutine_threadsafe(coro, engine._default_loop) - ) - return await coro async def delete_tables(): engine = await PostgresEngine.afrom_instance( @@ -50,13 +42,10 @@ async def delete_tables(): password=PASSWORD, ) - async def _logic(): - async with engine._pool.connect() as conn: - await conn.execute(text("COMMIT")) - await conn.execute(text(f"DROP TABLE IF EXISTS {TABLE_NAME}")) - await conn.execute(text(f"DROP TABLE IF EXISTS {CHAT_TABLE_NAME}")) - await run_on_background(engine, _logic()) - + async with engine._pool.connect() as conn: + await conn.execute(text("COMMIT")) + await conn.execute(text(f"DROP TABLE IF EXISTS {TABLE_NAME}")) + await conn.execute(text(f"DROP TABLE IF EXISTS {CHAT_TABLE_NAME}")) await engine.close() await engine._connector.close_async() From 9197b9a183531b29a3de31fb4d28f0a0eb62820f Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 23:08:34 +0530 Subject: [PATCH 12/14] Update clean_up.py --- samples/langchain_on_vertexai/clean_up.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/samples/langchain_on_vertexai/clean_up.py b/samples/langchain_on_vertexai/clean_up.py index 45e57ae5..056958f4 100644 --- a/samples/langchain_on_vertexai/clean_up.py +++ b/samples/langchain_on_vertexai/clean_up.py @@ -32,6 +32,15 @@ TEST_NAME = os.getenv("DISPLAY_NAME") +async def run_on_background(engine: PostgresEngine, coro: Coroutine) -> Any: + """Runs a coroutine on the engine's background loop.""" + if engine._default_loop: + return await asyncio.wrap_future( + asyncio.run_coroutine_threadsafe(coro, engine._default_loop) + ) + return await coro + + async def delete_tables(): engine = await PostgresEngine.afrom_instance( PROJECT_ID, @@ -42,10 +51,13 @@ async def delete_tables(): password=PASSWORD, ) - async with engine._pool.connect() as conn: - await conn.execute(text("COMMIT")) - await conn.execute(text(f"DROP TABLE IF EXISTS {TABLE_NAME}")) - await conn.execute(text(f"DROP TABLE IF EXISTS {CHAT_TABLE_NAME}")) + async def _logic(): + async with engine._pool.connect() as conn: + await conn.execute(text("COMMIT")) + await conn.execute(text(f"DROP TABLE IF EXISTS {TABLE_NAME}")) + await conn.execute(text(f"DROP TABLE IF EXISTS {CHAT_TABLE_NAME}")) + + await run_on_background(engine, _logic()) await engine.close() await engine._connector.close_async() From 4cc71c0b027f0dc72d5850c9ef11fdb673fd4c4e Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 23:12:26 +0530 Subject: [PATCH 13/14] Update clean_up.py --- samples/langchain_on_vertexai/clean_up.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/langchain_on_vertexai/clean_up.py b/samples/langchain_on_vertexai/clean_up.py index 056958f4..2fa8cd96 100644 --- a/samples/langchain_on_vertexai/clean_up.py +++ b/samples/langchain_on_vertexai/clean_up.py @@ -13,6 +13,7 @@ # limitations under the License. import asyncio import os +from typing import Any, Coroutine from config import ( CHAT_TABLE_NAME, From ee7db2b0169f0a79e384a796f36053fe8300a681 Mon Sep 17 00:00:00 2001 From: dishaprakash <57954147+dishaprakash@users.noreply.github.com> Date: Mon, 15 Dec 2025 23:34:15 +0530 Subject: [PATCH 14/14] Update clean_up.py --- samples/langchain_on_vertexai/clean_up.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/langchain_on_vertexai/clean_up.py b/samples/langchain_on_vertexai/clean_up.py index 2fa8cd96..42c3866a 100644 --- a/samples/langchain_on_vertexai/clean_up.py +++ b/samples/langchain_on_vertexai/clean_up.py @@ -60,7 +60,6 @@ async def _logic(): await run_on_background(engine, _logic()) await engine.close() - await engine._connector.close_async() def delete_engines():