Home / Function/ test_higher_order_lambda_runnable() — langchain Function Reference

test_higher_order_lambda_runnable() — langchain Function Reference

Architecture documentation for the test_higher_order_lambda_runnable() function in test_runnable.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  be9051e1_675b_b440_1772_caff1939fc2f["test_higher_order_lambda_runnable()"]
  26df6ad8_0189_51d0_c3c1_6c3248893ff5["test_runnable.py"]
  be9051e1_675b_b440_1772_caff1939fc2f -->|defined in| 26df6ad8_0189_51d0_c3c1_6c3248893ff5
  593bb72a_4695_6c93_b95c_e277aca006ae["batch()"]
  be9051e1_675b_b440_1772_caff1939fc2f -->|calls| 593bb72a_4695_6c93_b95c_e277aca006ae
  f59d5b6a_111b_6895_b338_7e3d29e63896["invoke()"]
  be9051e1_675b_b440_1772_caff1939fc2f -->|calls| f59d5b6a_111b_6895_b338_7e3d29e63896
  style be9051e1_675b_b440_1772_caff1939fc2f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/runnables/test_runnable.py lines 2935–2990

def test_higher_order_lambda_runnable(
    mocker: MockerFixture, snapshot: SnapshotAssertion
) -> None:
    math_chain = ChatPromptTemplate.from_template(
        "You are a math genius. Answer the question: {question}"
    ) | FakeListLLM(responses=["4"])
    english_chain = ChatPromptTemplate.from_template(
        "You are an english major. Answer the question: {question}"
    ) | FakeListLLM(responses=["2"])
    input_map = RunnableParallel(
        key=lambda x: x["key"],
        input={"question": lambda x: x["question"]},
    )

    def router(params: dict[str, Any]) -> Runnable:
        if params["key"] == "math":
            return itemgetter("input") | math_chain
        if params["key"] == "english":
            return itemgetter("input") | english_chain
        msg = f"Unknown key: {params['key']}"
        raise ValueError(msg)

    chain: Runnable = input_map | router
    assert dumps(chain, pretty=True) == snapshot

    result = chain.invoke({"key": "math", "question": "2 + 2"})
    assert result == "4"

    result2 = chain.batch(
        [
            {"key": "math", "question": "2 + 2"},
            {"key": "english", "question": "2 + 2"},
        ]
    )
    assert result2 == ["4", "2"]

    # Test invoke
    math_spy = mocker.spy(math_chain.__class__, "invoke")
    tracer = FakeTracer()
    assert (
        chain.invoke({"key": "math", "question": "2 + 2"}, {"callbacks": [tracer]})
        == "4"
    )
    assert math_spy.call_args.args[1] == {
        "key": "math",
        "input": {"question": "2 + 2"},
    }
    assert len([r for r in tracer.runs if r.parent_run_id is None]) == 1
    parent_run = next(r for r in tracer.runs if r.parent_run_id is None)
    assert len(parent_run.child_runs) == 2
    router_run = parent_run.child_runs[1]
    assert router_run.name == "router"
    assert len(router_run.child_runs) == 1
    math_run = router_run.child_runs[0]
    assert math_run.name == "RunnableSequence"
    assert len(math_run.child_runs) == 3

Domain

Subdomains

Frequently Asked Questions

What does test_higher_order_lambda_runnable() do?
test_higher_order_lambda_runnable() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/runnables/test_runnable.py.
Where is test_higher_order_lambda_runnable() defined?
test_higher_order_lambda_runnable() is defined in libs/core/tests/unit_tests/runnables/test_runnable.py at line 2935.
What does test_higher_order_lambda_runnable() call?
test_higher_order_lambda_runnable() calls 2 function(s): batch, invoke.

Analyze Your Own Codebase

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

Try Supermodel Free