LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • Client
  • AsyncClient
  • Run Helpers
  • Run Trees
  • Evaluation
  • Schemas
  • Utilities
  • Wrappers
  • Anonymizer
  • Testing
  • Expect API
  • Middleware
  • Pytest Plugin
  • Deployment SDK
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

OverviewClientAsyncClientRun HelpersRun TreesEvaluationSchemasUtilitiesWrappersAnonymizerTestingExpect APIMiddlewarePytest PluginDeployment SDK
Language
Theme
Pythonlangsmith_expect_Expectembedding_distance
Method●Since v0.1

embedding_distance

Compute the embedding distance between the prediction and reference.

This logs the embedding distance to LangSmith and returns a _Matcher instance for making assertions on the distance value.

By default, this uses the OpenAI API for computing embeddings.

Copy
embedding_distance(
  self,
  prediction: str,
  reference: str,
  *,
  config: Optional[EmbeddingConfig] = None
) -> _Matcher

Example:

expect.embedding_distance(
    prediction="hello",
    reference="hi",
).to_be_less_than(1.0)

Parameters

NameTypeDescription
prediction*str

The predicted string to compare.

reference*str

The reference string to compare against.

configOptional[EmbeddingConfig]
Default:None

Optional configuration for the embedding distance evaluator.

Supported options:

  • encoder: A custom encoder function to encode the list of input strings to embeddings.

    Defaults to the OpenAI API.

  • metric: The distance metric to use for comparison.

    Supported values: 'cosine', 'euclidean', 'manhattan', 'chebyshev', 'hamming'.

View source on GitHub