Skip to content

Commit 2a7a3dd

Browse files
fix(langchain): support encoding in stream method (#9329)
1 parent 58e664c commit 2a7a3dd

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

libs/langchain/src/agents/runtime.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ type CreateAgentPregelOptions =
118118
| "maxConcurrency"
119119
| "timeout";
120120

121+
/**
122+
* Pregel stream options that are propagated to the agent
123+
*/
124+
type CreateAgentPregelStreamOptions = "encoding" | "streamMode";
125+
121126
/**
122127
* Decide whether provided configuration requires a context
123128
*/
@@ -161,15 +166,15 @@ export type StreamConfiguration<ContextSchema extends Record<string, any>> =
161166
? Partial<
162167
Pick<
163168
PregelOptions<any, any, any>,
164-
CreateAgentPregelOptions | "streamMode"
169+
CreateAgentPregelOptions | CreateAgentPregelStreamOptions
165170
>
166171
> & {
167172
context?: Partial<ContextSchema>;
168173
}
169174
: Partial<
170175
Pick<
171176
PregelOptions<any, any, any>,
172-
CreateAgentPregelOptions | "streamMode"
177+
CreateAgentPregelOptions | CreateAgentPregelStreamOptions
173178
>
174179
> &
175180
WithMaybeContext<ContextSchema>;

libs/langchain/src/agents/tests/reactAgent.test-d.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from "zod/v3";
22
import { BaseMessage, HumanMessage } from "@langchain/core/messages";
33
import { LanguageModelLike } from "@langchain/core/language_models/base";
44
import { describe, it, expectTypeOf } from "vitest";
5+
import type { IterableReadableStream } from "@langchain/core/utils/stream";
56

67
import { createAgent } from "../index.js";
78

@@ -61,6 +62,54 @@ describe("reactAgent", () => {
6162
});
6263
});
6364

65+
it("supports streaming", async () => {
66+
const agent = createAgent({
67+
model: "openai:gpt-4",
68+
});
69+
const stream = await agent.stream(
70+
{
71+
messages: [new HumanMessage("Hello, world!")],
72+
},
73+
{
74+
encoding: "text/event-stream",
75+
streamMode: ["values", "updates", "messages"],
76+
configurable: {
77+
thread_id: "test-123",
78+
},
79+
recursionLimit: 10,
80+
}
81+
);
82+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
83+
expectTypeOf(stream).toEqualTypeOf<IterableReadableStream<any>>();
84+
85+
await agent.invoke(
86+
{
87+
messages: [new HumanMessage("Hello, world!")],
88+
},
89+
{
90+
// @ts-expect-error encoding is not a valid property
91+
encoding: "text/event-stream",
92+
configurable: {
93+
thread_id: "test-123",
94+
},
95+
recursionLimit: 10,
96+
}
97+
);
98+
await agent.invoke(
99+
{
100+
messages: [new HumanMessage("Hello, world!")],
101+
},
102+
{
103+
// @ts-expect-error encoding is not a valid property
104+
streamMode: ["values", "updates", "messages"],
105+
configurable: {
106+
thread_id: "test-123",
107+
},
108+
recursionLimit: 10,
109+
}
110+
);
111+
});
112+
64113
it("should allow a state schema that makes invoke calls require to pass in a state", async () => {
65114
const agent = createAgent({
66115
model: "openai:gpt-4",

0 commit comments

Comments
 (0)