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
102 changes: 102 additions & 0 deletions js/src/instrumentation/plugins/openai-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,108 @@ describe("parseMetricsFromUsage", () => {
});

describe("aggregateChatCompletionChunks", () => {
describe("reasoning_content", () => {
it("concatenates reasoning separately from content for each choice", () => {
const chunks = [
{
choices: [
{
index: 1,
delta: { role: "assistant", reasoning_content: "Let " },
},
{
index: 0,
delta: { role: "assistant", reasoning_content: "Think " },
},
],
},
{
choices: [
{ index: 0, delta: { reasoning_content: "first.", content: "An" } },
{
index: 1,
delta: { reasoning_content: "me think.", content: "Other" },
},
],
},
{
choices: [
{
index: 1,
delta: { reasoning_content: null },
finish_reason: "length",
},
{ index: 0, delta: { content: "swer" }, finish_reason: "stop" },
],
},
{
choices: [],
usage: { prompt_tokens: 2, completion_tokens: 8, total_tokens: 10 },
},
];
const originalChunks = structuredClone(chunks);

expect(aggregateChatCompletionChunks(chunks)).toEqual({
output: [
{
index: 0,
message: {
role: "assistant",
content: "Answer",
reasoning_content: "Think first.",
},
logprobs: null,
finish_reason: "stop",
},
{
index: 1,
message: {
role: "assistant",
content: "Other",
reasoning_content: "Let me think.",
},
logprobs: null,
finish_reason: "length",
},
],
metrics: { prompt_tokens: 2, completion_tokens: 8, tokens: 10 },
});
expect(chunks).toEqual(originalChunks);
});

it.each([
{ fragments: [undefined, undefined], expected: undefined },
{ fragments: [null, undefined, null], expected: null },
{ fragments: ["", undefined, ""], expected: "" },
{ fragments: [null, "", null], expected: "" },
{
fragments: [null, "Think", undefined, null, "", " more", null],
expected: "Think more",
},
])(
"preserves reasoning fragments $fragments as $expected",
({ fragments, expected }) => {
const chunks = fragments.map((reasoning_content) => ({
choices: [
{
index: 0,
delta:
reasoning_content === undefined ? {} : { reasoning_content },
},
],
}));
const { message } = aggregateChatCompletionChunks(chunks).output[0];

if (expected === undefined) {
expect(message).not.toHaveProperty("reasoning_content");
} else {
expect(message).toHaveProperty("reasoning_content", expected);
}
expect(message.content).toBeUndefined();
},
);
});

describe("basic aggregation", () => {
it("should aggregate simple text chunks", () => {
const chunks = [
Expand Down
15 changes: 14 additions & 1 deletion js/src/instrumentation/plugins/openai-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ type AggregatedChatChoice = {
index: number;
role: string | undefined;
content: string | undefined;
reasoning_content: string | null | undefined;
refusal: string | undefined;
audio: NonNullable<OpenAIChatChoice["message"]["audio"]> | undefined;
toolCallsByIndex: Map<
Expand All @@ -424,6 +425,7 @@ function createAggregatedChatChoice(index: number): AggregatedChatChoice {
index,
role: undefined,
content: undefined,
reasoning_content: undefined,
refusal: undefined,
audio: undefined,
toolCallsByIndex: new Map(),
Expand All @@ -442,6 +444,9 @@ function toChatChoice(choice: AggregatedChatChoice): OpenAIChatChoice {
message: {
role: choice.role,
content: choice.content,
...(choice.reasoning_content !== undefined
? { reasoning_content: choice.reasoning_content }
: {}),
...(choice.refusal !== undefined ? { refusal: choice.refusal } : {}),
...(choice.audio !== undefined ? { audio: choice.audio } : {}),
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
Expand All @@ -453,7 +458,7 @@ function toChatChoice(choice: AggregatedChatChoice): OpenAIChatChoice {

/**
* Aggregate chat completion chunks into a single response.
* Combines role (first), content and audio transcripts (concatenated),
* Combines role (first), content, reasoning_content and audio transcripts (concatenated),
* tool_calls (by index), finish_reason (last), and usage (last chunk).
*/
export function aggregateChatCompletionChunks(
Expand Down Expand Up @@ -515,6 +520,14 @@ export function aggregateChatCompletionChunks(
(aggregatedChoice.content || "") + delta.content;
}

if (typeof delta.reasoning_content === "string") {
aggregatedChoice.reasoning_content =
(aggregatedChoice.reasoning_content ?? "") + delta.reasoning_content;
} else if (delta.reasoning_content === null) {
// Retain explicit nulls, but never discard text from earlier chunks.
aggregatedChoice.reasoning_content ??= null;
}

if (delta.refusal) {
aggregatedChoice.refusal =
(aggregatedChoice.refusal || "") + delta.refusal;
Expand Down
2 changes: 2 additions & 0 deletions js/src/vendor-sdk-types/openai-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ interface OpenAIChatAudio {
export interface OpenAIChatMessage {
role?: string;
content?: unknown;
reasoning_content?: string | null;
refusal?: string;
audio?: OpenAIChatAudio | null;
tool_calls?: OpenAIChatToolCall[];
Expand Down Expand Up @@ -135,6 +136,7 @@ export interface OpenAIChatCompletion {
interface OpenAIChatDelta {
role?: string;
content?: string;
reasoning_content?: string | null;
refusal?: string;
audio?: OpenAIChatAudio | null;
tool_calls?: OpenAIChatToolCallDelta[];
Expand Down
Loading