Skip to content
Open
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
32 changes: 32 additions & 0 deletions packages/openai-adapters/src/apis/OpenAI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ChatCompletionCreateParams } from "openai/resources/index";
import { describe, expect, it } from "vitest";

import { OpenAIApi } from "./OpenAI.js";

const streamingBody = (): ChatCompletionCreateParams => ({
model: "gpt-4.1",
messages: [{ role: "user", content: "hello" }],
stream: true,
});

describe("OpenAIApi modifyChatBody stream_options", () => {
it("adds stream_options for official OpenAI streaming requests", () => {
const api = new OpenAIApi({ provider: "openai", apiKey: "test-key" });

const modifiedBody = api.modifyChatBody(streamingBody());

expect((modifiedBody as any).stream_options).toEqual({ include_usage: true });
});

it("does not add OpenAI-only stream_options for compatible endpoints", () => {
const api = new OpenAIApi({
provider: "openai",
apiKey: "test-key",
apiBase: "https://example.cloud.databricks.com/serving-endpoints/chat/invocations",
});

const modifiedBody = api.modifyChatBody(streamingBody());

expect((modifiedBody as any).stream_options).toBeUndefined();
});
});
7 changes: 4 additions & 3 deletions packages/openai-adapters/src/apis/OpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ export class OpenAIApi implements BaseLlmApi {
});
}
modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T {
// Add stream_options to include usage in streaming responses
if (body.stream) {
const isOfficialOpenAIAPI = this.apiBase === "https://api.openai.com/v1/";

// Add stream_options to include usage in official OpenAI streaming responses
if (body.stream && isOfficialOpenAIAPI) {
(body as any).stream_options = { include_usage: true };
}

Expand All @@ -65,7 +67,6 @@ export class OpenAIApi implements BaseLlmApi {
}

// o-series models - only apply for official OpenAI API
const isOfficialOpenAIAPI = this.apiBase === "https://api.openai.com/v1/";
if (isOfficialOpenAIAPI) {
if (body.model.startsWith("o") || body.model.includes("gpt-5")) {
// a) use max_completion_tokens instead of max_tokens
Expand Down
Loading