Skip to content

openaiChatCompletionsStreaming.ts:Fuzzy logic #275

@mumu-sild

Description

@mumu-sild

Describe the Bug

There are several issues in the logic for content indexing and final content assembly in the streaming message handling:

1. Incorrect index calculation logic; final content assembly ignores the index

// Line 53 - text content index
!state.refusal_content_index_and_output ? 0 : 1

// Line 71 - refusal content index
!state.text_content_index_and_output ? 0 : 1
if (state.text_content_index_and_output) {
  assistant_msg.content.push(state.text_content_index_and_output[1]);
}
if (state.refusal_content_index_and_output) {
  assistant_msg.content.push(state.refusal_content_index_and_output[1]);
}
  • The previously calculated indexes are completely ignored.
  • Content is pushed in code execution order, not based on the calculated index.
  • This may lead to content being ordered incorrectly.

3. Index is stored but never used

text_content_index_and_output: [index, content]  // index is never used

Only the content part is used in the end; the index is computed but discarded.

4. Data structure is unnecessarily complex

If the index is unused, using a [number, Content] tuple is unnecessarily complicated:

text_content_index_and_output: [number, protocol.OutputText] | null

It can be simplified to:

text_content: protocol.OutputText | null

Suggested Improvement

type StreamingState = {
  started: boolean;
  text_content: protocol.OutputText | null;
  refusal_content: protocol.Refusal | null;
  function_calls: Record<number, protocol.FunctionCallItem>;
};

Content assembly becomes much cleaner:

const content = [];
if (state.text_content) content.push(state.text_content);
if (state.refusal_content) content.push(state.refusal_content);

Debug Information

  • Agents SDK version: (e.g. v0.0.14)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions