batch() — langchain Function Reference
Architecture documentation for the batch() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 2826dfcf_5024_bd4d_0268_8f909fbbae0c["batch()"] 4a62481c_02cb_a5de_1833_50669d5351a6["Runnable"] 2826dfcf_5024_bd4d_0268_8f909fbbae0c -->|defined in| 4a62481c_02cb_a5de_1833_50669d5351a6 e9999cfb_b051_7cee_27e4_2e2746488c79["batch()"] e9999cfb_b051_7cee_27e4_2e2746488c79 -->|calls| 2826dfcf_5024_bd4d_0268_8f909fbbae0c 6623b4f5_0477_1322_996c_6a66715eb7c1["batch()"] 6623b4f5_0477_1322_996c_6a66715eb7c1 -->|calls| 2826dfcf_5024_bd4d_0268_8f909fbbae0c 0ceb56af_9929_a187_7b84_2584155e0d7b["pipe()"] 0ceb56af_9929_a187_7b84_2584155e0d7b -->|calls| 2826dfcf_5024_bd4d_0268_8f909fbbae0c d7c8b78b_bd8b_c15c_ce21_97525a7a5764["_invoke()"] d7c8b78b_bd8b_c15c_ce21_97525a7a5764 -->|calls| 2826dfcf_5024_bd4d_0268_8f909fbbae0c e2ca1dd3_4ae9_2ab3_37d2_60242857703f["invoke()"] 2826dfcf_5024_bd4d_0268_8f909fbbae0c -->|calls| e2ca1dd3_4ae9_2ab3_37d2_60242857703f cb51eea6_993b_93b2_5bc8_da36e6fac2df["map()"] 2826dfcf_5024_bd4d_0268_8f909fbbae0c -->|calls| cb51eea6_993b_93b2_5bc8_da36e6fac2df 6623b4f5_0477_1322_996c_6a66715eb7c1["batch()"] 2826dfcf_5024_bd4d_0268_8f909fbbae0c -->|calls| 6623b4f5_0477_1322_996c_6a66715eb7c1 style 2826dfcf_5024_bd4d_0268_8f909fbbae0c fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/runnables/base.py lines 867–915
def batch(
self,
inputs: list[Input],
config: RunnableConfig | list[RunnableConfig] | None = None,
*,
return_exceptions: bool = False,
**kwargs: Any | None,
) -> list[Output]:
"""Default implementation runs invoke in parallel using a thread pool executor.
The default implementation of batch works well for IO bound runnables.
Subclasses must override this method if they can batch more efficiently;
e.g., if the underlying `Runnable` uses an API which supports a batch mode.
Args:
inputs: A list of inputs to the `Runnable`.
config: A config to use when invoking the `Runnable`. The config supports
standard keys like `'tags'`, `'metadata'` for
tracing purposes, `'max_concurrency'` for controlling how much work
to do in parallel, and other keys.
Please refer to `RunnableConfig` for more details.
return_exceptions: Whether to return exceptions instead of raising them.
**kwargs: Additional keyword arguments to pass to the `Runnable`.
Returns:
A list of outputs from the `Runnable`.
"""
if not inputs:
return []
configs = get_config_list(config, len(inputs))
def invoke(input_: Input, config: RunnableConfig) -> Output | Exception:
if return_exceptions:
try:
return self.invoke(input_, config, **kwargs)
except Exception as e:
return e
else:
return self.invoke(input_, config, **kwargs)
# If there's only one input, don't bother with the executor
if len(inputs) == 1:
return cast("list[Output]", [invoke(inputs[0], configs[0])])
with get_executor_for_config(configs[0]) as executor:
return cast("list[Output]", list(executor.map(invoke, inputs, configs)))
Domain
Subdomains
Defined In
Source
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 867.
What does batch() call?
batch() calls 3 function(s): batch, invoke, map.
What calls batch()?
batch() is called by 4 function(s): _invoke, batch, batch, pipe.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free