Home / Function/ add_texts() — langchain Function Reference

add_texts() — langchain Function Reference

Architecture documentation for the add_texts() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  8987550d_87eb_63b4_a3e6_a9f355797f08["add_texts()"]
  6c336ac6_f55c_1ad7_6db3_73dbd71fb625["VectorStore"]
  8987550d_87eb_63b4_a3e6_a9f355797f08 -->|defined in| 6c336ac6_f55c_1ad7_6db3_73dbd71fb625
  ebcdae9e_b9ad_ed9c_d4b2_8d59854c890a["add_documents()"]
  ebcdae9e_b9ad_ed9c_d4b2_8d59854c890a -->|calls| 8987550d_87eb_63b4_a3e6_a9f355797f08
  ebcdae9e_b9ad_ed9c_d4b2_8d59854c890a["add_documents()"]
  8987550d_87eb_63b4_a3e6_a9f355797f08 -->|calls| ebcdae9e_b9ad_ed9c_d4b2_8d59854c890a
  01360435_9886_2db5_7556_2148d97a9fdd["add_documents()"]
  8987550d_87eb_63b4_a3e6_a9f355797f08 -->|calls| 01360435_9886_2db5_7556_2148d97a9fdd
  style 8987550d_87eb_63b4_a3e6_a9f355797f08 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/vectorstores/base.py lines 46–97

    def add_texts(
        self,
        texts: Iterable[str],
        metadatas: list[dict] | None = None,
        *,
        ids: list[str] | None = None,
        **kwargs: Any,
    ) -> list[str]:
        """Run more texts through the embeddings and add to the `VectorStore`.

        Args:
            texts: Iterable of strings to add to the `VectorStore`.
            metadatas: Optional list of metadatas associated with the texts.
            ids: Optional list of IDs associated with the texts.
            **kwargs: `VectorStore` specific parameters.

                One of the kwargs should be `ids` which is a list of ids
                associated with the texts.

        Returns:
            List of IDs from adding the texts into the `VectorStore`.

        Raises:
            ValueError: If the number of metadatas does not match the number of texts.
            ValueError: If the number of IDs does not match the number of texts.
        """
        if type(self).add_documents != VectorStore.add_documents:
            # This condition is triggered if the subclass has provided
            # an implementation of the upsert method.
            # The existing add_texts
            texts_: Sequence[str] = (
                texts if isinstance(texts, (list, tuple)) else list(texts)
            )
            if metadatas and len(metadatas) != len(texts_):
                msg = (
                    "The number of metadatas must match the number of texts."
                    f"Got {len(metadatas)} metadatas and {len(texts_)} texts."
                )
                raise ValueError(msg)
            metadatas_ = iter(metadatas) if metadatas else cycle([{}])
            ids_: Iterator[str | None] = iter(ids) if ids else cycle([None])
            docs = [
                Document(id=id_, page_content=text, metadata=metadata_)
                for text, metadata_, id_ in zip(texts, metadatas_, ids_, strict=False)
            ]
            if ids is not None:
                # For backward compatibility
                kwargs["ids"] = ids

            return self.add_documents(docs, **kwargs)
        msg = f"`add_texts` has not been implemented for {self.__class__.__name__} "
        raise NotImplementedError(msg)

Subdomains

Called By

Frequently Asked Questions

What does add_texts() do?
add_texts() is a function in the langchain codebase, defined in libs/core/langchain_core/vectorstores/base.py.
Where is add_texts() defined?
add_texts() is defined in libs/core/langchain_core/vectorstores/base.py at line 46.
What does add_texts() call?
add_texts() calls 2 function(s): add_documents, add_documents.
What calls add_texts()?
add_texts() is called by 1 function(s): add_documents.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free