mirror of
https://github.com/hwchase17/langchain
synced 2024-11-04 06:00:26 +00:00
Fix Runnable.transform() for false-y inputs (#10893)
--------- Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
parent
fcb5aba9f0
commit
ea26c12b23
@ -286,16 +286,19 @@ class Runnable(Generic[Input, Output], ABC):
|
|||||||
Subclasses should override this method if they can start producing output while
|
Subclasses should override this method if they can start producing output while
|
||||||
input is still being generated.
|
input is still being generated.
|
||||||
"""
|
"""
|
||||||
final: Union[Input, None] = None
|
final: Input
|
||||||
|
got_first_val = False
|
||||||
|
|
||||||
for chunk in input:
|
for chunk in input:
|
||||||
if final is None:
|
if not got_first_val:
|
||||||
final = chunk
|
final = chunk
|
||||||
|
got_first_val = True
|
||||||
else:
|
else:
|
||||||
# Make a best effort to gather, for any type that supports `+`
|
# Make a best effort to gather, for any type that supports `+`
|
||||||
# This method should throw an error if gathering fails.
|
# This method should throw an error if gathering fails.
|
||||||
final += chunk # type: ignore[operator]
|
final += chunk # type: ignore[operator]
|
||||||
if final:
|
|
||||||
|
if got_first_val:
|
||||||
yield from self.stream(final, config, **kwargs)
|
yield from self.stream(final, config, **kwargs)
|
||||||
|
|
||||||
async def atransform(
|
async def atransform(
|
||||||
@ -309,17 +312,19 @@ class Runnable(Generic[Input, Output], ABC):
|
|||||||
Subclasses should override this method if they can start producing output while
|
Subclasses should override this method if they can start producing output while
|
||||||
input is still being generated.
|
input is still being generated.
|
||||||
"""
|
"""
|
||||||
final: Union[Input, None] = None
|
final: Input
|
||||||
|
got_first_val = False
|
||||||
|
|
||||||
async for chunk in input:
|
async for chunk in input:
|
||||||
if final is None:
|
if not got_first_val:
|
||||||
final = chunk
|
final = chunk
|
||||||
|
got_first_val = True
|
||||||
else:
|
else:
|
||||||
# Make a best effort to gather, for any type that supports `+`
|
# Make a best effort to gather, for any type that supports `+`
|
||||||
# This method should throw an error if gathering fails.
|
# This method should throw an error if gathering fails.
|
||||||
final += chunk # type: ignore[operator]
|
final += chunk # type: ignore[operator]
|
||||||
|
|
||||||
if final:
|
if got_first_val:
|
||||||
async for output in self.astream(final, config, **kwargs):
|
async for output in self.astream(final, config, **kwargs):
|
||||||
yield output
|
yield output
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user