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

fix(openai): Allow o1 to stream a single chunk #7616

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 11 additions & 2 deletions libs/langchain-openai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,8 @@ export class ChatOpenAI<
this.reasoningEffort = fields?.reasoningEffort;

if (this.model === "o1") {
this.disableStreaming = true;
this.disableStreaming =
fields?.disableStreaming != null ? fields?.disableStreaming : true;
bracesproul marked this conversation as resolved.
Show resolved Hide resolved
}

this.streaming = fields?.streaming ?? false;
Expand Down Expand Up @@ -1310,6 +1311,15 @@ export class ChatOpenAI<
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
if (this.streaming === false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we fix this in core instead?

Copy link
Collaborator

@jacoblee93 jacoblee93 Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

streaming doesn't exist on BaseChatModel

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok - why can't we just set disableStreaming: true?

const result = await this._generate(messages, options, runManager);
yield new ChatGenerationChunk({
message: new AIMessageChunk({ ...result.generations[0].message }),
text: result.generations[0].text,
});
return;
}

const messagesMapped: OpenAICompletionParam[] =
_convertMessagesToOpenAIParams(messages, this.model);
const params = {
Expand All @@ -1320,7 +1330,6 @@ export class ChatOpenAI<
stream: true as const,
};
let defaultRole: OpenAIRoleEnum | undefined;

const streamIterable = await this.completionWithRetry(params, options);
let usage: OpenAIClient.Completions.CompletionUsage | undefined;
for await (const data of streamIterable) {
Expand Down
24 changes: 24 additions & 0 deletions libs/langchain-openai/src/tests/chat_models.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1285,3 +1285,27 @@ test.skip("Allow overriding", async () => {
console.log(chunk);
}
});

test("Streaming with o1 will yield at least one chunk with content", async () => {
const model = new ChatOpenAI({
model: "o1",
disableStreaming: false,
streaming: false,
});

const result = model.streamEvents([["user", "What color is the sky?"]], {
version: "v2",
});

let content = "";
let numStreamChunks = 0;
for await (const chunk of result) {
if (chunk.event === "on_chat_model_stream") {
content += chunk.data.chunk.content;
numStreamChunks += 1;
}
}

expect(content.length).toBeGreaterThan(10);
expect(numStreamChunks).toBe(1);
});
Loading