Home / Function/ embed_documents() — langchain Function Reference

embed_documents() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  64f4fa06_4784_3e51_9655_1c4667c3f612["embed_documents()"]
  2f237d29_e276_c4ef_3a56_7139ce49b50e["OpenAIEmbeddings"]
  64f4fa06_4784_3e51_9655_1c4667c3f612 -->|defined in| 2f237d29_e276_c4ef_3a56_7139ce49b50e
  e468f110_acc6_1477_1d24_c998730b0bf4["embed_query()"]
  e468f110_acc6_1477_1d24_c998730b0bf4 -->|calls| 64f4fa06_4784_3e51_9655_1c4667c3f612
  852243b7_32dc_46e6_5d5f_62e271437d8d["_ensure_sync_client_available()"]
  64f4fa06_4784_3e51_9655_1c4667c3f612 -->|calls| 852243b7_32dc_46e6_5d5f_62e271437d8d
  bd7de307_7f7c_35fc_e574_e5dfd1b9a161["_get_len_safe_embeddings()"]
  64f4fa06_4784_3e51_9655_1c4667c3f612 -->|calls| bd7de307_7f7c_35fc_e574_e5dfd1b9a161
  style 64f4fa06_4784_3e51_9655_1c4667c3f612 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/embeddings/base.py lines 677–711

    def embed_documents(
        self, texts: list[str], chunk_size: int | None = None, **kwargs: Any
    ) -> list[list[float]]:
        """Call OpenAI's embedding endpoint to embed search docs.

        Args:
            texts: The list of texts to embed.
            chunk_size: The chunk size of embeddings.

                If `None`, will use the chunk size specified by the class.
            kwargs: Additional keyword arguments to pass to the embedding API.

        Returns:
            List of embeddings, one for each text.
        """
        self._ensure_sync_client_available()
        chunk_size_ = chunk_size or self.chunk_size
        client_kwargs = {**self._invocation_params, **kwargs}
        if not self.check_embedding_ctx_length:
            embeddings: list[list[float]] = []
            for i in range(0, len(texts), chunk_size_):
                response = self.client.create(
                    input=texts[i : i + chunk_size_], **client_kwargs
                )
                if not isinstance(response, dict):
                    response = response.model_dump()
                embeddings.extend(r["embedding"] for r in response["data"])
            return embeddings

        # Unconditionally call _get_len_safe_embeddings to handle length safety.
        # This could be optimized to avoid double work when all texts are short enough.
        engine = cast(str, self.deployment)
        return self._get_len_safe_embeddings(
            texts, engine=engine, chunk_size=chunk_size, **kwargs
        )

Domain

Subdomains

Called By

Frequently Asked Questions

What does embed_documents() do?
embed_documents() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/embeddings/base.py.
Where is embed_documents() defined?
embed_documents() is defined in libs/partners/openai/langchain_openai/embeddings/base.py at line 677.
What does embed_documents() call?
embed_documents() calls 2 function(s): _ensure_sync_client_available, _get_len_safe_embeddings.
What calls embed_documents()?
embed_documents() is called by 1 function(s): embed_query.

Analyze Your Own Codebase

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

Try Supermodel Free