Home / Function/ batch() — langchain Function Reference

batch() — langchain Function Reference

Architecture documentation for the batch() function in base.py from the langchain codebase.

Function python LangChainCore Runnables calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  e9999cfb_b051_7cee_27e4_2e2746488c79["batch()"]
  c4bf8d59_69f9_f8e5_efba_837c20df2360["RunnableSequence"]
  e9999cfb_b051_7cee_27e4_2e2746488c79 -->|defined in| c4bf8d59_69f9_f8e5_efba_837c20df2360
  b9fb513b_bf75_3677_fc04_41bba780611c["abatch()"]
  b9fb513b_bf75_3677_fc04_41bba780611c -->|calls| e9999cfb_b051_7cee_27e4_2e2746488c79
  2826dfcf_5024_bd4d_0268_8f909fbbae0c["batch()"]
  e9999cfb_b051_7cee_27e4_2e2746488c79 -->|calls| 2826dfcf_5024_bd4d_0268_8f909fbbae0c
  255c479b_b9fa_44d8_4de5_2562051e06b5["get_name()"]
  e9999cfb_b051_7cee_27e4_2e2746488c79 -->|calls| 255c479b_b9fa_44d8_4de5_2562051e06b5
  style e9999cfb_b051_7cee_27e4_2e2746488c79 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 3207–3332

    def batch(
        self,
        inputs: list[Input],
        config: RunnableConfig | list[RunnableConfig] | None = None,
        *,
        return_exceptions: bool = False,
        **kwargs: Any | None,
    ) -> list[Output]:
        if not inputs:
            return []

        # setup callbacks and context
        configs = get_config_list(config, len(inputs))
        callback_managers = [
            CallbackManager.configure(
                inheritable_callbacks=config.get("callbacks"),
                local_callbacks=None,
                verbose=False,
                inheritable_tags=config.get("tags"),
                local_tags=None,
                inheritable_metadata=config.get("metadata"),
                local_metadata=None,
            )
            for config in configs
        ]
        # start the root runs, one per input
        run_managers = [
            cm.on_chain_start(
                None,
                input_,
                name=config.get("run_name") or self.get_name(),
                run_id=config.pop("run_id", None),
            )
            for cm, input_, config in zip(
                callback_managers, inputs, configs, strict=False
            )
        ]

        # invoke
        try:
            if return_exceptions:
                # Track which inputs (by index) failed so far
                # If an input has failed it will be present in this map,
                # and the value will be the exception that was raised.
                failed_inputs_map: dict[int, Exception] = {}
                for stepidx, step in enumerate(self.steps):
                    # Assemble the original indexes of the remaining inputs
                    # (i.e. the ones that haven't failed yet)
                    remaining_idxs = [
                        i for i in range(len(configs)) if i not in failed_inputs_map
                    ]
                    # Invoke the step on the remaining inputs
                    inputs = step.batch(
                        [
                            inp
                            for i, inp in zip(remaining_idxs, inputs, strict=False)
                            if i not in failed_inputs_map
                        ],
                        [
                            # each step a child run of the corresponding root run
                            patch_config(
                                config,
                                callbacks=rm.get_child(f"seq:step:{stepidx + 1}"),
                            )
                            for i, (rm, config) in enumerate(
                                zip(run_managers, configs, strict=False)
                            )
                            if i not in failed_inputs_map
                        ],
                        return_exceptions=return_exceptions,
                        **(kwargs if stepidx == 0 else {}),
                    )
                    # If an input failed, add it to the map
                    failed_inputs_map.update(
                        {
                            i: inp
                            for i, inp in zip(remaining_idxs, inputs, strict=False)
                            if isinstance(inp, Exception)
                        }
                    )
                    inputs = [inp for inp in inputs if not isinstance(inp, Exception)]

Domain

Subdomains

Called By

Frequently Asked Questions

What does batch() do?
batch() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is batch() defined?
batch() is defined in libs/core/langchain_core/runnables/base.py at line 3207.
What does batch() call?
batch() calls 2 function(s): batch, get_name.
What calls batch()?
batch() is called by 1 function(s): abatch.

Analyze Your Own Codebase

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

Try Supermodel Free