Skip to content

Commit 2637e2d

Browse files
aperepelgoodvibes
andauthored
fix: Use Spanner's UPSERT over INSERT (#90)
Switch to UPSERT. This provides for a nice experience for DELETE-then-INSERT use cases in Spanner when the primary key stays the same. Otherwise the DELETE is not pushed before INSERT and the client gets a 409 Row [abc] already exists. The way LangChain integration is designed, there is no easy way to wrap them all in a TX with additional args for such visibility. Co-authored-by: goodvibes <goodvibes@google.com>
1 parent 34de40e commit 2637e2d

4 files changed

Lines changed: 5 additions & 5 deletions

File tree

samples/langchain_quick_start.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@
407407
"for i in range(1, len(rows), batch_size):\n",
408408
" with database.batch() as batch:\n",
409409
" batch_rows = rows[i : i + batch_size]\n",
410-
" batch.insert(\n",
410+
" batch.insert_or_update(\n",
411411
" table=table_name,\n",
412412
" columns=[\n",
413413
" \"show_id\",\n",
@@ -676,7 +676,7 @@
676676
"for i in range(0, len(vector_store_rows), batch_size):\n",
677677
" with database.batch() as batch:\n",
678678
" batch_rows = vector_store_rows[i : i + batch_size]\n",
679-
" batch.insert(\n",
679+
" batch.insert_or_update(\n",
680680
" table=sample_vector_table_name,\n",
681681
" columns=[\n",
682682
" \"langchain_id\",\n",

src/langchain_google_spanner/chat_message_history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def messages(self) -> List[BaseMessage]: # type: ignore
212212
def add_message(self, message: BaseMessage) -> None:
213213
"""Append the message to the record in Cloud Spanner"""
214214
with self.database.batch() as batch:
215-
batch.insert(
215+
batch.insert_or_update(
216216
table=self.table_name,
217217
columns=("session_id", "created_at", "message"),
218218
values=[

src/langchain_google_spanner/loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def add_documents(self, documents: List[Document]):
318318

319319
for values_batch in _batch(values, MUTATION_BATCH_SIZE):
320320
with db.batch() as batch:
321-
batch.insert(
321+
batch.insert_or_update(
322322
table=self.table_name,
323323
columns=self._table_fields,
324324
values=values_batch,

src/langchain_google_spanner/vector_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ def add_texts(
706706

707707
def _insert_data(self, records, columns_to_insert):
708708
with self._database.batch() as batch:
709-
batch.insert(
709+
batch.insert_or_update(
710710
table=self._table_name,
711711
columns=columns_to_insert,
712712
values=records,

0 commit comments

Comments
 (0)