From 10f72287e83458c18d16ca4170ffbc378f2de471 Mon Sep 17 00:00:00 2001 From: Jo Liss Date: Mon, 1 Jan 2024 21:36:45 +0100 Subject: [PATCH] RunnableWithFallbacks: Pass exceptions to fallback runnables --- libs/core/langchain_core/runnables/fallbacks.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libs/core/langchain_core/runnables/fallbacks.py b/libs/core/langchain_core/runnables/fallbacks.py index 5f6dbf11bf378..2733043ca0ffb 100644 --- a/libs/core/langchain_core/runnables/fallbacks.py +++ b/libs/core/langchain_core/runnables/fallbacks.py @@ -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: @@ -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: @@ -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: @@ -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, @@ -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,