mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 14:43:07 +00:00
**issue**: #24660 RunnableWithMessageHistory.stream result in error because the [evaluation](https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/runnables/branch.py#L220) of the branch [condition](99eb31ec41/libs/core/langchain_core/runnables/history.py (L328C1-L329C1)) unexpectedly trigger the "[on_end](99eb31ec41/libs/core/langchain_core/runnables/history.py (L332))" (exit_history) callback of the default branch **descriptions** After a lot of investigation I'm convinced that the root cause is that 1. during the execution of the runnable, the [var_child_runnable_config](99eb31ec41/libs/core/langchain_core/runnables/config.py (L122)) is shared between the branch [condition](99eb31ec41/libs/core/langchain_core/runnables/history.py (L328C1-L329C1)) runnable and the [default branch runnable](99eb31ec41/libs/core/langchain_core/runnables/history.py (L332)) within the same context 2. when the default branch runnable runs, it gets the [var_child_runnable_config](99eb31ec41/libs/core/langchain_core/runnables/config.py (L163)) and may unintentionally [add more handlers ](99eb31ec41/libs/core/langchain_core/runnables/config.py (L325))to the callback manager of this config 3. when it is again the turn for the [condition](99eb31ec41/libs/core/langchain_core/runnables/history.py (L328C1-L329C1)) to run, it gets the `var_child_runnable_config` whose callback manager has the handlers added by the default branch. When it runs that handler (`exit_history`) it leads to the error with the assumption that, the `ensure_config` function actually does want to create a immutable copy from `var_child_runnable_config` because it starts with an [`empty` variable ](99eb31ec41/libs/core/langchain_core/runnables/config.py (L156)), i go ahead to do a deepcopy to ensure that future modification to the returned value won't affect the `var_child_runnable_config` variable Having said that I actually 1. don't know if this is a proper fix 2. don't know whether it will lead to other unintended consequence 3. don't know why only "stream" runs into this issue while "invoke" runs without problem so @nfcampos @hwchase17 please help review, thanks! --------- Co-authored-by: Lifu Wu <lifu@nextbillion.ai> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Bagatur <baskaryan@gmail.com>