Skip to content

Commit

Permalink
RunnableWithFallbacks: Pass exceptions to fallback runnables
Browse files Browse the repository at this point in the history
  • Loading branch information
joliss committed Jan 8, 2024
1 parent 4c47f39 commit 10f7228
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions libs/core/langchain_core/runnables/fallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def runnables(self) -> Iterator[Runnable[Input, Output]]:
yield self.runnable
yield from self.fallbacks

def _configurable(self, error) -> dict[str, Any]:
return {"exception": error} if error else {}

def invoke(
self, input: Input, config: Optional[RunnableConfig] = None, **kwargs: Any
) -> Output:
Expand All @@ -148,7 +151,8 @@ def invoke(
try:
output = runnable.invoke(
input,
patch_config(config, callbacks=run_manager.get_child()),
patch_config(config, callbacks=run_manager.get_child(),
configurable=self._configurable(first_error)),
**kwargs,
)
except self.exceptions_to_handle as e:
Expand Down Expand Up @@ -184,7 +188,8 @@ async def ainvoke(
try:
output = await runnable.ainvoke(
input,
patch_config(config, callbacks=run_manager.get_child()),
patch_config(config, callbacks=run_manager.get_child(),
configurable=self._configurable(first_error)),
**kwargs,
)
except self.exceptions_to_handle as e:
Expand Down Expand Up @@ -248,7 +253,8 @@ def batch(
inputs,
[
# each step a child run of the corresponding root run
patch_config(config, callbacks=rm.get_child())
patch_config(config, callbacks=rm.get_child(),
configurable=self._configurable(first_error))
for rm, config in zip(run_managers, configs)
],
return_exceptions=return_exceptions,
Expand Down Expand Up @@ -320,7 +326,8 @@ async def abatch(
inputs,
[
# each step a child run of the corresponding root run
patch_config(config, callbacks=rm.get_child())
patch_config(config, callbacks=rm.get_child(),
configurable=self._configurable(first_error))
for rm, config in zip(run_managers, configs)
],
return_exceptions=return_exceptions,
Expand Down

0 comments on commit 10f7228

Please sign in to comment.