Skip to main content
The Python and TypeScript SDKs are the recommended way to run evaluations in LangSmith. They include optimizations and features that enhance performance and reliability. If you cannot use the SDKs—for example, if you are working in a different language or a restricted environment—you can use the REST API directly. This guide demonstrates how to run evaluations using the REST API with Python’s requests library, but the same principles apply to any language. Before diving into this content, it might be helpful to read the following:

Create a dataset

For this example, we use the Python SDK to create a dataset quickly. To create datasets via the API or UI instead, refer to Managing datasets.

Run a single experiment

To run an experiment via the API, you’ll need to:
  1. Fetch the examples from your dataset.
  2. Create an experiment (also called a “session” in the API).
  3. For each example, create runs that reference both the example and the experiment.
  4. Close the experiment by setting its end_time.
First, pull all of the examples you’d want to use in your experiment using the /examples endpoint:
from langsmith import uuid7 Next, define a function that will run your model on a single example and log the results to LangSmith. When using the API directly, you’re responsible for:
  • Creating run objects via POST to /runs with reference_example_id and session_id set.
  • Tracking parent-child relationships between runs (e.g., a parent “chain” run containing a child “llm” run).
  • Updating runs with outputs via PATCH to /runs/{run_id}.
Now create the experiments and run completions on all examples. In the API, an “experiment” is represented as a session (or “tracer session”) that references a dataset via reference_dataset_id. The key difference from regular tracing is that runs in an experiment must have a reference_example_id that links each run to a specific example in the dataset.

Add evaluation feedback

After running your experiments, you’ll typically want to evaluate the results by adding feedback scores. This allows you to track metrics like correctness, accuracy, or any custom evaluation criteria. In this example, the evaluation checks if each model’s output matches the expected label in the dataset. The code posts a “correctness” score (1.0 for correct, 0.0 for incorrect) to track how accurately each model classifies toxic vs. non-toxic text. The following code adds feedback to the runs from the single experiment example:
You can add multiple feedback scores with different keys to track various metrics. For example, you might add both a “correctness” score and a “toxicity_detected” score.

Run a pairwise experiment

Next, we’ll demonstrate how to run a pairwise experiment. In a pairwise experiment, you compare two examples against each other. For more information, check out How to run a pairwise evaluation.