langchain/libs/core/langchain_core/runnables
Eugene Yurtsev 9120cf5df2
core[patch]: Deduplicate of callback handlers in merge_configs (#22478)
This PR adds deduplication of callback handlers in merge_configs.

Fix for this issue:
https://github.com/langchain-ai/langchain/issues/22227

The issue appears when the code is:

1) running python >=3.11
2) invokes a runnable from within a runnable
3) binds the callbacks to the child runnable from the parent runnable
using with_config

In this case, the same callbacks end up appearing twice: (1) the first
time from with_config, (2) the second time with langchain automatically
propagating them on behalf of the user.


Prior to this PR this will emit duplicate events:

```python
@tool
async def get_items(question: str, callbacks: Callbacks):  # <--- Accept callbacks
    """Ask question"""
    template = ChatPromptTemplate.from_messages(
        [
            (
                "human",
                "'{question}"
            )
        ]
    )
    chain = template | chat_model.with_config(
        {
            "callbacks": callbacks,  # <-- Propagate callbacks
        }
    )
    return await chain.ainvoke({"question": question})
```

Prior to this PR this will work work correctly (no duplicate events):

```python
@tool
async def get_items(question: str, callbacks: Callbacks):  # <--- Accept callbacks
    """Ask question"""
    template = ChatPromptTemplate.from_messages(
        [
            (
                "human",
                "'{question}"
            )
        ]
    )
    chain = template | chat_model
    return await chain.ainvoke({"question": question}, {"callbacks": callbacks})
```

This will also work (as long as the user is using python >= 3.11) -- as
langchain will automatically propagate callbacks

```python
@tool
async def get_items(question: str,):  
    """Ask question"""
    template = ChatPromptTemplate.from_messages(
        [
            (
                "human",
                "'{question}"
            )
        ]
    )
    chain = template | chat_model
    return await chain.ainvoke({"question": question})
```
2024-06-04 16:19:00 -04:00
..
__init__.py docs: runnable module description (#17966) 2024-03-01 10:01:32 -08:00
base.py Use immutable sequence type for batch/batch_as_completed types (#22433) 2024-06-04 08:04:09 -07:00
branch.py [Enhancement] Add support for directly providing a run_id (#18990) 2024-03-18 15:03:04 -07:00
config.py core[patch]: Deduplicate of callback handlers in merge_configs (#22478) 2024-06-04 16:19:00 -04:00
configurable.py core[patch[: docstring update (#21036) 2024-04-29 15:35:34 -04:00
fallbacks.py core[patch]: allow access RunnableWithFallbacks.runnable attrs (#22139) 2024-05-28 13:18:09 -07:00
graph_ascii.py infra: rm unused # noqa violations (#22049) 2024-05-22 15:21:08 -07:00
graph_mermaid.py core: allow mermaid node labels to have any characters (#21385) 2024-05-07 12:16:49 -07:00
graph_png.py core[patch[: docstring update (#21036) 2024-04-29 15:35:34 -04:00
graph.py core[patch[: docstring update (#21036) 2024-04-29 15:35:34 -04:00
history.py core[patch]: fix runnable history and add docs (#22283) 2024-05-30 11:26:41 -07:00
learnable.py [Enhancement] Add support for directly providing a run_id (#18990) 2024-03-18 15:03:04 -07:00
passthrough.py core[patch[: docstring update (#21036) 2024-04-29 15:35:34 -04:00
retry.py core[patch]: docstring update (#16813) 2024-02-09 12:47:41 -08:00
router.py docs: Add docs for RouterRunnable (#19191) 2024-03-17 00:48:00 +00:00
schema.py core[patch]: docstring update (#16813) 2024-02-09 12:47:41 -08:00
utils.py [Core] Check is async callable (#21714) 2024-05-15 10:49:49 -07:00