Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the astream_log() method, you cannot use the bind method with RunnableLambda. #17241

Open
eyurtsev opened this issue Feb 8, 2024 Discussed in #16446 · 1 comment
Open
Assignees
Labels
01 bug Confirmed bug 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules

Comments

@eyurtsev
Copy link
Collaborator

eyurtsev commented Feb 8, 2024

Discussed in #16446

Originally posted by jason1315 January 23, 2024
In my project, I need to implement the following logic. Here is a simple:

import asyncio

from langchain_core.runnables import *

from lang_chain.llm.llms import llm

def _test(_dict):
print("value:", _dict)
return _dict

@chain
def my_method(_dict, **keywords):
print(keywords)
return RunnablePassthrough.assign(key=lambda x: keywords.get("i")) | RunnableLambda(_test)

if name == 'main':

loop = asyncio.new_event_loop()

my_list = ["1", "2", "3", " 4", "5"]

head = RunnablePassthrough()
for i in my_list:
    head = head | my_method.bind(i=i)
stream = head.invoke({})
#
# async def __stream(stream1):
#     async for i in stream1:
#         print(i)
#
# loop.run_until_complete(__stream(stream))

When I use the .invoke({}) method, it outputs the following results correctly:

{'i': '1'}
value: {'key': '1'}
{'i': '2'}
value: {'key': '2'}
{'i': '3'}
value: {'key': '3'}
{'i': ' 4'}
value: {'key': ' 4'}
{'i': '5'}
value: {'key': '5'}

But if I use the astream_log({}) method, it throws an error:

File "F:\py3.11\Lib\concurrent\futures\thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: RunnableLambda._atransform.<locals>.func() got an unexpected keyword argument 'i'

Why is it designed like this? Do I need to implement a runnable similar to the model if I want to achieve the above logic?

@eyurtsev eyurtsev self-assigned this Feb 8, 2024
@dosubot dosubot bot added Ɑ: models Related to LLMs or chat model modules 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Feb 8, 2024
@langchain-ai langchain-ai deleted a comment from dosubot bot Feb 13, 2024
@eyurtsev eyurtsev added 01 bug Confirmed bug and removed confirmed labels Feb 14, 2024
baskaryan pushed a commit that referenced this issue Feb 21, 2024
**Description:** Here is a minimal example to illustrate behavior:
```python
from langchain_core.runnables import RunnableLambda

def my_function(*args, **kwargs):
    return 3 + kwargs.get("n", 0)

runnable = RunnableLambda(my_function).bind(n=1)


assert 4 == runnable.invoke({})
assert [4] == list(runnable.stream({}))

assert 4 == await runnable.ainvoke({})
assert [4] == [item async for item in runnable.astream({})]
```
Here, `runnable.invoke({})` and `runnable.stream({})` work fine, but
`runnable.ainvoke({})` raises
```
TypeError: RunnableLambda._ainvoke.<locals>.func() got an unexpected keyword argument 'n'
```
and similarly for `runnable.astream({})`:
```
TypeError: RunnableLambda._atransform.<locals>.func() got an unexpected keyword argument 'n'
```
Here we assume that this behavior is undesired and attempt to fix it.

**Issue:** #17241,
#16446
k8si pushed a commit to Mozilla-Ocho/langchain that referenced this issue Feb 22, 2024
…n-ai#17739)

**Description:** Here is a minimal example to illustrate behavior:
```python
from langchain_core.runnables import RunnableLambda

def my_function(*args, **kwargs):
    return 3 + kwargs.get("n", 0)

runnable = RunnableLambda(my_function).bind(n=1)


assert 4 == runnable.invoke({})
assert [4] == list(runnable.stream({}))

assert 4 == await runnable.ainvoke({})
assert [4] == [item async for item in runnable.astream({})]
```
Here, `runnable.invoke({})` and `runnable.stream({})` work fine, but
`runnable.ainvoke({})` raises
```
TypeError: RunnableLambda._ainvoke.<locals>.func() got an unexpected keyword argument 'n'
```
and similarly for `runnable.astream({})`:
```
TypeError: RunnableLambda._atransform.<locals>.func() got an unexpected keyword argument 'n'
```
Here we assume that this behavior is undesired and attempt to fix it.

**Issue:** langchain-ai#17241,
langchain-ai#16446
al1p pushed a commit to al1p/langchain that referenced this issue Feb 27, 2024
…n-ai#17739)

**Description:** Here is a minimal example to illustrate behavior:
```python
from langchain_core.runnables import RunnableLambda

def my_function(*args, **kwargs):
    return 3 + kwargs.get("n", 0)

runnable = RunnableLambda(my_function).bind(n=1)


assert 4 == runnable.invoke({})
assert [4] == list(runnable.stream({}))

assert 4 == await runnable.ainvoke({})
assert [4] == [item async for item in runnable.astream({})]
```
Here, `runnable.invoke({})` and `runnable.stream({})` work fine, but
`runnable.ainvoke({})` raises
```
TypeError: RunnableLambda._ainvoke.<locals>.func() got an unexpected keyword argument 'n'
```
and similarly for `runnable.astream({})`:
```
TypeError: RunnableLambda._atransform.<locals>.func() got an unexpected keyword argument 'n'
```
Here we assume that this behavior is undesired and attempt to fix it.

**Issue:** langchain-ai#17241,
langchain-ai#16446
haydeniw pushed a commit to haydeniw/langchain that referenced this issue Feb 27, 2024
…n-ai#17739)

**Description:** Here is a minimal example to illustrate behavior:
```python
from langchain_core.runnables import RunnableLambda

def my_function(*args, **kwargs):
    return 3 + kwargs.get("n", 0)

runnable = RunnableLambda(my_function).bind(n=1)


assert 4 == runnable.invoke({})
assert [4] == list(runnable.stream({}))

assert 4 == await runnable.ainvoke({})
assert [4] == [item async for item in runnable.astream({})]
```
Here, `runnable.invoke({})` and `runnable.stream({})` work fine, but
`runnable.ainvoke({})` raises
```
TypeError: RunnableLambda._ainvoke.<locals>.func() got an unexpected keyword argument 'n'
```
and similarly for `runnable.astream({})`:
```
TypeError: RunnableLambda._atransform.<locals>.func() got an unexpected keyword argument 'n'
```
Here we assume that this behavior is undesired and attempt to fix it.

**Issue:** langchain-ai#17241,
langchain-ai#16446
@UtkarshaGupte
Copy link
Contributor

@baskaryan - Is this issue fixed by PR ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 bug Confirmed bug 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules
Projects
None yet
Development

No branches or pull requests

2 participants