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

core[minor]: Stream events v2 #5539

Merged
merged 9 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ sidebar_label: v0.2

LangChain v0.2 was released in May 2024. This release includes a number of breaking changes and deprecations. This document contains a guide on upgrading to 0.2.x, as well as a list of deprecations and breaking changes.

:::note Reference

- [Migrating to Astream Events v2](/docs/versions/v0_2/migrating_astream_events)

:::

## Migration

This documentation will help you upgrade your code to LangChain `0.2.x.`. To prepare for migration, we first recommend you take the following steps:
Expand Down
125 changes: 125 additions & 0 deletions docs/core_docs/docs/versions/v0_2/migrating_astream_events.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
sidebar_position: 2
sidebar_label: streamEvents v2
---

# Migrating to streamEvents v2

:::danger

This migration guide is a work in progress and is not complete.

:::

We've added a `v2` of the [`streamEvents`](/docs/how_to/streaming#using-stream-events) API with the release of `0.2.0`. You can see this [PR](https://github.com/langchain-ai/langchainjs/pull/5539/) for more details.

The `v2` version is a re-write of the `v1` version, and should be more efficient, with more consistent output for the events. The `v1` version of the API will be deprecated in favor of the `v2` version and will be removed in `0.4.0`.

Below is a list of changes between the `v1` and `v2` versions of the API.

### output for `on_chat_model_end`

In `v1`, the outputs associated with `on_chat_model_end` changed depending on whether the
chat model was run as a root level runnable or as part of a chain.

As a root level runnable the output was:

```ts
{
data: {
output: AIMessageChunk((content = "hello world!"), (id = "some id"));
}
}
```

As part of a chain the output was:

```
{
data: {
output: {
generations: [
[
{
generation_info: None,
message: AIMessageChunk(
content="hello world!", id="some id"
),
text: "hello world!",
}
]
],
}
},
}
```

As of `v2`, the output will always be the simpler representation:

```ts
{
data: {
output: AIMessageChunk((content = "hello world!"), (id = "some id"));
}
}
```

:::note
Non chat models (i.e., regular LLMs) will be consistently associated with the more verbose format for now.
:::

### output for `on_retriever_end`

`on_retriever_end` output will always return a list of `Documents`.

This was the output in `v1`:

```ts
{
data: {
output: {
documents: [
Document(...),
Document(...),
...
]
}
}
}
```

And here is the new output for `v2`:

```ts
{
data: {
output: [
Document(...),
Document(...),
...
]
}
}
```

### Removed `on_retriever_stream`

The `on_retriever_stream` event was an artifact of the implementation and has been removed.

Full information associated with the event is already available in the `on_retriever_end` event.

Please use `on_retriever_end` instead.

### Removed `on_tool_stream`

The `on_tool_stream` event was an artifact of the implementation and has been removed.

Full information associated with the event is already available in the `on_tool_end` event.

Please use `on_tool_end` instead.

### Propagating Names

Names of runnables have been updated to be more consistent.

If you're filtering by event names, check if you need to update your filters.
1 change: 1 addition & 0 deletions langchain-core/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/no-this-alias": 0,
camelcase: 0,
"class-methods-use-this": 0,
"import/extensions": [2, "ignorePackages"],
Expand Down
190 changes: 153 additions & 37 deletions langchain-core/src/runnables/base.ts

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions langchain-core/src/runnables/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ export class RemoteRunnable<

_streamEvents(
input: RunInput,
options: Partial<CallOptions> & { version: "v1" },
options: Partial<CallOptions> & { version: "v1" | "v2" },
streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose"> | undefined
): AsyncGenerator<StreamEvent> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
Expand Down Expand Up @@ -611,14 +611,14 @@ export class RemoteRunnable<

streamEvents(
input: RunInput,
options: Partial<CallOptions> & { version: "v1" },
options: Partial<CallOptions> & { version: "v1" | "v2" },
streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">
): IterableReadableStream<StreamEvent>;

streamEvents(
input: RunInput,
options: Partial<CallOptions> & {
version: "v1";
version: "v1" | "v2";
encoding: "text/event-stream";
},
streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">
Expand All @@ -627,14 +627,14 @@ export class RemoteRunnable<
streamEvents(
input: RunInput,
options: Partial<CallOptions> & {
version: "v1";
version: "v1" | "v2";
encoding?: "text/event-stream" | undefined;
},
streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">
): IterableReadableStream<StreamEvent | Uint8Array> {
if (options?.version !== "v1") {
if (options.version !== "v1" && options.version !== "v2") {
throw new Error(
`Only version "v1" of the events schema is currently supported.`
`Only versions "v1" and "v2" of the events schema is currently supported.`
);
}
if (options.encoding !== undefined) {
Expand Down
Loading
Loading