Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain EmbeddingsUnitTests

From Leeroopedia
Knowledge Sources
Domains Testing, Embeddings, Standard Tests
Last Updated 2026-02-11 00:00 GMT

Overview

Standard unit test suite for validating Embeddings implementations, verifying initialization and environment variable configuration.

Description

This module in the langchain-tests (standard-tests) package defines two test classes: EmbeddingsTests, a base class providing the embeddings_class abstract property and a model fixture, and EmbeddingsUnitTests, which extends it with concrete unit tests. The unit tests verify basic model initialization from constructor parameters and initialization from environment variables. The init_from_env_params property can be overridden to enable environment variable initialization testing, providing a tuple of environment variables, init args, and expected attribute values.

Usage

Import EmbeddingsUnitTests when developing a custom embeddings integration and you need to verify that the model initializes correctly from both direct parameters and environment variables without making any network calls.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/standard-tests/langchain_tests/unit_tests/embeddings.py
  • Lines: 1-137

Signature

class EmbeddingsTests(BaseStandardTests):
    """Embeddings tests base class."""

    @property
    @abstractmethod
    def embeddings_class(self) -> type[Embeddings]:
        """Embeddings class."""

    @property
    def embedding_model_params(self) -> dict[str, Any]:
        """Embeddings model parameters."""
        ...

    @pytest.fixture
    def model(self) -> Embeddings:
        """Embeddings model fixture."""
        ...


class EmbeddingsUnitTests(EmbeddingsTests):
    """Base class for embeddings unit tests."""

    def test_init(self) -> None:
        ...

    @property
    def init_from_env_params(
        self,
    ) -> tuple[dict[str, str], dict[str, Any], dict[str, Any]]:
        ...

    def test_init_from_env(self) -> None:
        ...

Import

from langchain_tests.unit_tests.embeddings import EmbeddingsUnitTests

I/O Contract

Abstract Properties (Must Override)

Name Type Required Description
embeddings_class type[Embeddings] Yes The Embeddings subclass to test.

Optional Properties

Name Type Required Description
embedding_model_params dict[str, Any] No Constructor parameters for the embeddings model. Defaults to empty dict.
init_from_env_params tuple[dict[str, str], dict[str, Any], dict[str, Any]] No Tuple of (env_vars, init_args, expected_attrs) for environment variable initialization testing. Defaults to empty dicts (test is skipped).

Test Methods

Test Description
test_init Verifies the embeddings model can be initialized with the provided embedding_model_params.
test_init_from_env Verifies the model can be initialized from environment variables. Skipped if init_from_env_params returns empty dicts. Checks that instance attributes match expected values, handling SecretStr unwrapping.

Usage Examples

Basic Usage

from typing import Type

from langchain_tests.unit_tests.embeddings import EmbeddingsUnitTests
from my_package.embeddings import MyEmbeddingsModel


class TestMyEmbeddingsModelUnit(EmbeddingsUnitTests):
    @property
    def embeddings_class(self) -> Type[MyEmbeddingsModel]:
        return MyEmbeddingsModel

    @property
    def embedding_model_params(self) -> dict:
        return {"model": "model-001", "api_key": "test-key"}

With Environment Variable Testing

from typing import Tuple
from langchain_tests.unit_tests.embeddings import EmbeddingsUnitTests
from my_package.embeddings import MyEmbeddingsModel


class TestMyEmbeddingsModelUnit(EmbeddingsUnitTests):
    @property
    def embeddings_class(self):
        return MyEmbeddingsModel

    @property
    def embedding_model_params(self) -> dict:
        return {"model": "model-001", "api_key": "test-key"}

    @property
    def init_from_env_params(self) -> Tuple[dict, dict, dict]:
        return (
            {"MY_API_KEY": "api_key"},         # env vars to set
            {"model": "model-001"},             # init args
            {"my_api_key": "api_key"},          # expected attrs
        )

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment