Closed muditjai closed 2 days ago
Hello, @muditjai! I'm here to assist with your issue and help you become a contributor.
To resolve the compatibility issue between LangChain's ReadableStream
and Node's ReadableStream
, you can use the IterableReadableStreamV0
class provided in the @langchain/core/utils/stream
file. This class extends the ReadableStream
and implements the AsyncIterable
interface, ensuring compatibility across different environments.
Here's an example of how you can use the IterableReadableStreamV0
class to wrap a Node ReadableStream
:
import { IterableReadableStreamV0 } from '@langchain/core/utils/stream';
// Assuming you have a Node ReadableStream instance
const nodeReadableStream = getNodeReadableStream(); // Replace with your actual Node ReadableStream
// Wrap the Node ReadableStream with IterableReadableStreamV0
const iterableStream = IterableReadableStreamV0.fromReadableStream(nodeReadableStream);
// Now you can use async iteration with the iterableStream
for await (const chunk of iterableStream) {
console.log(chunk);
}
This approach ensures that the ReadableStream
can be used with async iteration syntax, making it compatible with both LangChain and Node environments. Additionally, the convertEventStreamToIterableReadableDataStream
function in the @langchain/core/utils/stream
file can be used to convert an event stream into an iterable readable data stream, further enhancing compatibility [1][2][3].
To continue talking to Dosu, mention @dosu.
Thanks for the heads up - will have a look as soon as possible
Having some trouble figuring this out :(
Is it just the types? Does it work if you just type it as any
?
Thanks for taking a look.
It's both an issue with type conversion and missing support for .values()
method.
See the stream.values({ preventCancel: true })
example in https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream, without values()
we can't read the stream in 2 separate for loops. In my use case, I need to read first k characters in a loop, do some processing on it and then continue reading rest of stream. Currently I'm implementing this in a while loop using .read()
on a reader instance obtained through getReader()
.
The type conversion I was able to workaround by typing it as any
.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Property 'values' is missing in type 'IterableReadableStream<string>' but required in type 'ReadableStream<string>'.ts(2741) web.d.ts(154, 9): 'values' is declared here.
Description
In my business logic, I am trying to extract a stream from langchain, but also have a node ReadableStream as backup in certain cases.
However, it seems langchain's
ReadableStream
coming fromlib.dom.d.ts
intypescript/lib
is not compatible with node'sReadableStream
coming fromweb.d.ts
innode:stream/web
. It's missingvalues
method.Here's the
ReadableStream
fromnode:stream/web
And here's
ReadableStream
fromtypescript/lib
which langchain depends on -System Info
node -v v20.12.1
Windows