Home / Function/ _wrap_in_chain_factory() — langchain Function Reference

_wrap_in_chain_factory() — langchain Function Reference

Architecture documentation for the _wrap_in_chain_factory() function in runner_utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c2ae8ee6_ba74_2f11_df16_cafb61b88f1e["_wrap_in_chain_factory()"]
  8253c602_7d0c_9195_a7e1_3e9b19304131["runner_utils.py"]
  c2ae8ee6_ba74_2f11_df16_cafb61b88f1e -->|defined in| 8253c602_7d0c_9195_a7e1_3e9b19304131
  9a9f493e_7864_c75d_ebe0_af192df494f6["_prepare_eval_run()"]
  9a9f493e_7864_c75d_ebe0_af192df494f6 -->|calls| c2ae8ee6_ba74_2f11_df16_cafb61b88f1e
  00d82cfb_ba59_4f67_e504_1faad0617f06["prepare()"]
  00d82cfb_ba59_4f67_e504_1faad0617f06 -->|calls| c2ae8ee6_ba74_2f11_df16_cafb61b88f1e
  0522e7ee_f1e6_b6f7_6738_1dad72cfffba["run_on_dataset()"]
  c2ae8ee6_ba74_2f11_df16_cafb61b88f1e -->|calls| 0522e7ee_f1e6_b6f7_6738_1dad72cfffba
  style c2ae8ee6_ba74_2f11_df16_cafb61b88f1e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/smith/evaluation/runner_utils.py lines 183–244

def _wrap_in_chain_factory(
    llm_or_chain_factory: MODEL_OR_CHAIN_FACTORY,
    dataset_name: str = "<my_dataset>",
) -> MCF:
    """Wrap in a chain factory.

    Forgive the user if they pass in a chain without memory instead of a chain
    factory. It's a common mistake. Raise a more helpful error message as well.
    """
    if isinstance(llm_or_chain_factory, Chain):
        chain = llm_or_chain_factory
        chain_class = chain.__class__.__name__
        if llm_or_chain_factory.memory is not None:
            memory_class = chain.memory.__class__.__name__
            msg = (
                "Cannot directly evaluate a chain with stateful memory."
                " To evaluate this chain, pass in a chain constructor"
                " that initializes fresh memory each time it is called."
                "  This will safeguard against information"
                " leakage between dataset examples."
                "\nFor example:\n\n"
                "def chain_constructor():\n"
                f"    new_memory = {memory_class}(...)\n"
                f"    return {chain_class}"
                "(memory=new_memory, ...)\n\n"
                f'run_on_dataset("{dataset_name}", chain_constructor, ...)'
            )
            raise ValueError(msg)
        return lambda: chain
    if isinstance(llm_or_chain_factory, BaseLanguageModel):
        return llm_or_chain_factory
    if isinstance(llm_or_chain_factory, Runnable):
        # Memory may exist here, but it's not elegant to check all those cases.
        lcf = llm_or_chain_factory
        return lambda: lcf
    if callable(llm_or_chain_factory):
        if is_traceable_function(llm_or_chain_factory):
            runnable_ = as_runnable(cast("Callable", llm_or_chain_factory))
            return lambda: runnable_
        try:
            _model = llm_or_chain_factory()  # type: ignore[call-arg]
        except TypeError:
            # It's an arbitrary function, wrap it in a RunnableLambda
            user_func = cast("Callable", llm_or_chain_factory)
            sig = inspect.signature(user_func)
            logger.info("Wrapping function %s as RunnableLambda.", sig)
            wrapped = RunnableLambda(user_func)
            return lambda: wrapped
        constructor = cast("Callable", llm_or_chain_factory)
        if isinstance(_model, BaseLanguageModel):
            # It's not uncommon to do an LLM constructor instead of raw LLM,
            # so we'll unpack it for the user.
            return _model
        if is_traceable_function(cast("Callable", _model)):
            runnable_ = as_runnable(cast("Callable", _model))
            return lambda: runnable_
        if not isinstance(_model, Runnable):
            # This is unlikely to happen - a constructor for a model function
            return lambda: RunnableLambda(constructor)
        # Typical correct case
        return constructor
    return llm_or_chain_factory  # type: ignore[unreachable]

Domain

Subdomains

Frequently Asked Questions

What does _wrap_in_chain_factory() do?
_wrap_in_chain_factory() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/smith/evaluation/runner_utils.py.
Where is _wrap_in_chain_factory() defined?
_wrap_in_chain_factory() is defined in libs/langchain/langchain_classic/smith/evaluation/runner_utils.py at line 183.
What does _wrap_in_chain_factory() call?
_wrap_in_chain_factory() calls 1 function(s): run_on_dataset.
What calls _wrap_in_chain_factory()?
_wrap_in_chain_factory() is called by 2 function(s): _prepare_eval_run, prepare.

Analyze Your Own Codebase

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

Try Supermodel Free