langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
88.69k stars 13.94k forks source link

Incorrect listeners parameters for Runnable.with_listeners() and .map() #20509

Closed skozlovf closed 1 month ago

skozlovf commented 2 months ago

Checked other resources

Example Code

from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

def fake_chain(inputs: dict) -> dict:
    return {**inputs, "key": "extra"}

def on_start(run: Run):
    print("on_start:", run.inputs)

def on_end(run: Run):
    print("on_end:  ", run.outputs)

chain = RunnableLambda(fake_chain).with_listeners(on_end=on_end, on_start=on_start)
chain = chain.map()

data = [{"name": "one"}, {"name": "two"}]
out = chain.invoke(data, config={"max_concurrency": 1})
print("result:  ", out)

max_concurrency is added for simplicity.

Error Message and Stack Trace (if applicable)

No response

Description

I want to store fake_chain output using listeners. with_listeners() allows to hook only top level runnable (according to its docstring). But run object is incorrect if use map().

I expect to see

on_start: {'name': 'one'}
on_start: {'name': 'two'}
on_end:   {'name': 'one', 'key': 'extra'}
on_end:   {'name': 'two', 'key': 'extra'}
result:   [{'name': 'one', 'key': 'extra'}, {'name': 'two', 'key': 'extra'}]

but get

on_start: {'name': 'one'}
on_start: {'name': 'one'}  # <!
on_end:   {'name': 'one', 'key': 'extra'}
on_end:   {'name': 'one', 'key': 'extra'}   # <!
result:   [{'name': 'one', 'key': 'extra'}, {'name': 'two', 'key': 'extra'}]

I didn't dive deeper, but smth wrong happens in the RunnableBindingBase.batch() -> _merge_configs() (a guess).

System Info

$ pip freeze | grep langchain
langchain==0.1.16               
langchain-anthropic==0.1.4      
langchain-community==0.0.33     
langchain-core==0.1.43          
langchain-google-genai==0.0.11  
langchain-google-vertexai==0.1.2
langchain-groq==0.0.1           
langchain-openai==0.0.8         
langchain-text-splitters==0.0.1 

platform: linux python: 3.11.8

eyurtsev commented 2 months ago

@skozlovf thank you for including an example that's so easy to reproduce!

liugddx commented 2 months ago

I will try to solve it

liugddx commented 2 months ago
image

Already fixed