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
1 change: 1 addition & 0 deletions packages/config-yaml/src/schemas/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ const baseModelFields = {
promptTemplates: promptTemplatesSchema.optional(),
useLegacyCompletionsEndpoint: z.boolean().optional(),
useResponsesApi: z.boolean().optional(),
streamOptions: z.boolean().optional(),
env: z
.record(z.string(), z.union([z.string(), z.boolean(), z.number()]))
.optional(),
Expand Down
2 changes: 1 addition & 1 deletion packages/openai-adapters/src/apis/OpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class OpenAIApi implements BaseLlmApi {
}
modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T {
// Add stream_options to include usage in streaming responses
if (body.stream) {
if (body.stream && this.config.streamOptions !== false) {
(body as any).stream_options = { include_usage: true };
}

Expand Down
28 changes: 27 additions & 1 deletion packages/openai-adapters/src/test/openai-adapter.vitest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { OpenAIApi } from "../apis/OpenAI.js";
import { createAdapterTests } from "./adapter-test-utils.js";

// Mock the fetch package (not needed for OpenAI but required by the shared test utils)
Expand All @@ -25,4 +26,29 @@ describe("OpenAI Adapter Tests", () => {
accept: "application/json",
},
});

it("includes streaming usage by default unless stream options are disabled", () => {
const body = {
model: "gpt-4",
messages: [{ role: "user" as const, content: "hello" }],
stream: true as const,
};
const defaultApi = new OpenAIApi({
provider: "openai",
apiKey: "test-api-key",
});
const disabledApi = new OpenAIApi({
provider: "openai",
apiKey: "test-api-key",
streamOptions: false,
});

expect(defaultApi.modifyChatBody({ ...body })).toHaveProperty(
"stream_options.include_usage",
true,
);
expect(disabledApi.modifyChatBody({ ...body })).not.toHaveProperty(
"stream_options",
);
});
});
1 change: 1 addition & 0 deletions packages/openai-adapters/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const BasePlusConfig = BaseConfig.extend({
// OpenAI and compatible
export const OpenAIConfigSchema = BasePlusConfig.extend({
useResponsesApi: z.boolean().optional(),
streamOptions: z.boolean().optional(),
provider: z.union([
z.literal("openai"),
z.literal("mistral"),
Expand Down
Loading