Home / Function/ test_seq_abatch_return_exceptions() — langchain Function Reference

test_seq_abatch_return_exceptions() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  8710f0c7_ddeb_700b_8de4_7a25105fc4b1["test_seq_abatch_return_exceptions()"]
  26df6ad8_0189_51d0_c3c1_6c3248893ff5["test_runnable.py"]
  8710f0c7_ddeb_700b_8de4_7a25105fc4b1 -->|defined in| 26df6ad8_0189_51d0_c3c1_6c3248893ff5
  e24518e7_67af_1c57_2674_94d33184f37b["_abatch()"]
  8710f0c7_ddeb_700b_8de4_7a25105fc4b1 -->|calls| e24518e7_67af_1c57_2674_94d33184f37b
  8652094c_ec57_c551_fc44_9566d00cf872["abatch()"]
  8710f0c7_ddeb_700b_8de4_7a25105fc4b1 -->|calls| 8652094c_ec57_c551_fc44_9566d00cf872
  593bb72a_4695_6c93_b95c_e277aca006ae["batch()"]
  8710f0c7_ddeb_700b_8de4_7a25105fc4b1 -->|calls| 593bb72a_4695_6c93_b95c_e277aca006ae
  fb618d44_c03b_ea8b_385b_2278dfb173d4["invoke()"]
  8710f0c7_ddeb_700b_8de4_7a25105fc4b1 -->|calls| fb618d44_c03b_ea8b_385b_2278dfb173d4
  style 8710f0c7_ddeb_700b_8de4_7a25105fc4b1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/runnables/test_runnable.py lines 4318–4456

async def test_seq_abatch_return_exceptions(mocker: MockerFixture) -> None:
    class ControlledExceptionRunnable(Runnable[str, str]):
        def __init__(self, fail_starts_with: str) -> None:
            self.fail_starts_with = fail_starts_with

        @override
        def invoke(
            self, input: Any, config: RunnableConfig | None = None, **kwargs: Any
        ) -> Any:
            raise NotImplementedError

        async def _abatch(
            self,
            inputs: list[str],
        ) -> list[str | Exception]:
            outputs: list[str | Exception] = []
            for value in inputs:
                if value.startswith(self.fail_starts_with):
                    outputs.append(
                        ValueError(
                            f"ControlledExceptionRunnable({self.fail_starts_with}) "
                            f"fail for {value}"
                        )
                    )
                else:
                    outputs.append(value + "a")
            return outputs

        async def abatch(
            self,
            inputs: list[str],
            config: RunnableConfig | list[RunnableConfig] | None = None,
            *,
            return_exceptions: bool = False,
            **kwargs: Any,
        ) -> list[str]:
            return await self._abatch_with_config(
                self._abatch,
                inputs,
                config,
                return_exceptions=return_exceptions,
                **kwargs,
            )

    chain = (
        ControlledExceptionRunnable("bux")
        | ControlledExceptionRunnable("bar")
        | ControlledExceptionRunnable("baz")
        | ControlledExceptionRunnable("foo")
    )

    assert isinstance(chain, RunnableSequence)

    # Test abatch
    with pytest.raises(
        ValueError, match=re.escape("ControlledExceptionRunnable(bar) fail for bara")
    ):
        await chain.abatch(["foo", "bar", "baz", "qux"])

    spy = mocker.spy(ControlledExceptionRunnable, "abatch")
    tracer = FakeTracer()
    inputs = ["foo", "bar", "baz", "qux"]
    outputs = await chain.abatch(
        inputs, {"callbacks": [tracer]}, return_exceptions=True
    )
    assert len(outputs) == 4
    assert isinstance(outputs[0], ValueError)
    assert isinstance(outputs[1], ValueError)
    assert isinstance(outputs[2], ValueError)
    assert outputs[3] == "quxaaaa"
    assert spy.call_count == 4
    inputs_to_batch = [c[0][1] for c in spy.call_args_list]
    assert inputs_to_batch == [
        # inputs to sequence step 0
        # same as inputs to sequence.batch()
        ["foo", "bar", "baz", "qux"],
        # inputs to sequence step 1
        # == outputs of sequence step 0 as no exceptions were raised
        ["fooa", "bara", "baza", "quxa"],
        # inputs to sequence step 2
        # 'bar' was dropped as it raised an exception in step 1

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free