Implementation:Langchain ai Langchain ModelLaboratory
| Knowledge Sources | |
|---|---|
| Domains | Model Comparison, Experimentation |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
ModelLaboratory is a utility class for experimenting with and comparing the output of different LLM models or chains side by side on the same input.
Description
ModelLaboratory lives in the langchain_classic package and provides a convenient way to run the same input through multiple chains (or LLMs) and compare their outputs visually in the terminal. It supports initialization from either a list of Chain objects or a list of BaseLLM objects (via the from_llms class method). Each chain must have exactly one input and one output variable. Outputs are color-coded for easy visual comparison.
Usage
Import this class when you want to compare the behavior of different models or chains against the same prompt input during experimentation and prototyping.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/langchain/langchain_classic/model_laboratory.py - Lines: 1-99
Signature
class ModelLaboratory:
def __init__(
self,
chains: Sequence[Chain],
names: list[str] | None = None,
) -> None: ...
@classmethod
def from_llms(
cls,
llms: list[BaseLLM],
prompt: PromptTemplate | None = None,
) -> ModelLaboratory: ...
def compare(self, text: str) -> None: ...
Import
from langchain_classic.model_laboratory import ModelLaboratory
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| chains | Sequence[Chain] |
Yes | A sequence of chains to experiment with. Each must have exactly one input and one output variable. |
| names | None | No | Optional list of display names corresponding to each chain. |
| llms | list[BaseLLM] |
Yes (for from_llms) |
A list of LLMs to experiment with. |
| prompt | None | No | Optional prompt template to use with the LLMs (used by from_llms).
|
| text | str |
Yes (for compare) |
Input text to run all models on. |
Outputs
| Name | Type | Description |
|---|---|---|
| compare output | None |
Prints color-coded model outputs to stdout for visual comparison. |
| from_llms return | ModelLaboratory |
A new instance initialized with LLM chains. |
Usage Examples
Basic Usage
from langchain_classic.model_laboratory import ModelLaboratory
from langchain_core.prompts.prompt import PromptTemplate
# Initialize from LLMs
lab = ModelLaboratory.from_llms([llm1, llm2, llm3])
# Compare outputs
lab.compare("What is the meaning of life?")
# Prints color-coded outputs from each model
# Initialize with custom chains and names
lab = ModelLaboratory(chains=[chain1, chain2], names=["GPT-4", "Claude"])
lab.compare("Explain quantum computing")
Related Pages
- Heuristic:Langchain_ai_Langchain_Warning_Deprecated_Langchain_Classic
- Depends on
langchain_classic.chains.base.Chainandlangchain_classic.chains.llm.LLMChain