Runnable that delegates calls to another Runnable with each element of the input sequence. It allows you to call multiple inputs with the bounded Runnable. RunnableEach makes it easy to run multiple inputs for the runnable. In the below example, we associate and run three inputs
| 4327 | |
| 4328 | |
| 4329 | class RunnableEach(RunnableEachBase[Input, Output]): |
| 4330 | """Runnable that delegates calls to another Runnable |
| 4331 | with each element of the input sequence. |
| 4332 | |
| 4333 | It allows you to call multiple inputs with the bounded Runnable. |
| 4334 | |
| 4335 | RunnableEach makes it easy to run multiple inputs for the runnable. |
| 4336 | In the below example, we associate and run three inputs |
| 4337 | with a Runnable: |
| 4338 | |
| 4339 | .. code-block:: python |
| 4340 | |
| 4341 | from langchain_core.runnables.base import RunnableEach |
| 4342 | from langchain_openai import ChatOpenAI |
| 4343 | from langchain_core.prompts import ChatPromptTemplate |
| 4344 | from langchain_core.output_parsers import StrOutputParser |
| 4345 | prompt = ChatPromptTemplate.from_template("Tell me a short joke about |
| 4346 | {topic}") |
| 4347 | model = ChatOpenAI() |
| 4348 | output_parser = StrOutputParser() |
| 4349 | runnable = prompt | model | output_parser |
| 4350 | runnable_each = RunnableEach(bound=runnable) |
| 4351 | output = runnable_each.invoke([{'topic':'Computer Science'}, |
| 4352 | {'topic':'Art'}, |
| 4353 | {'topic':'Biology'}]) |
| 4354 | print(output) # noqa: T201 |
| 4355 | """ |
| 4356 | |
| 4357 | @classmethod |
| 4358 | def get_lc_namespace(cls) -> List[str]: |
| 4359 | """Get the namespace of the langchain object.""" |
| 4360 | return ["langchain", "schema", "runnable"] |
| 4361 | |
| 4362 | def get_name( |
| 4363 | self, suffix: Optional[str] = None, *, name: Optional[str] = None |
| 4364 | ) -> str: |
| 4365 | name = name or self.name or f"RunnableEach<{self.bound.get_name()}>" |
| 4366 | return super().get_name(suffix, name=name) |
| 4367 | |
| 4368 | def bind(self, **kwargs: Any) -> RunnableEach[Input, Output]: |
| 4369 | return RunnableEach(bound=self.bound.bind(**kwargs)) |
| 4370 | |
| 4371 | def with_config( |
| 4372 | self, config: Optional[RunnableConfig] = None, **kwargs: Any |
| 4373 | ) -> RunnableEach[Input, Output]: |
| 4374 | return RunnableEach(bound=self.bound.with_config(config, **kwargs)) |
| 4375 | |
| 4376 | def with_listeners( |
| 4377 | self, |
| 4378 | *, |
| 4379 | on_start: Optional[ |
| 4380 | Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] |
| 4381 | ] = None, |
| 4382 | on_end: Optional[ |
| 4383 | Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] |
| 4384 | ] = None, |
| 4385 | on_error: Optional[ |
| 4386 | Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] |
no outgoing calls
no test coverage detected