Skip to content

Aggregated text delta is wrong #4

@mbuptivo

Description

@mbuptivo

Hi,
I'm having a problem with handling of 'thread.message.delta' events from openai agent. The first delta contains not only the first delta text, but accumulated text from next deltas, so then text is aggregated the result is wrong.
Ex:

DELTA 1: Hi ho <--- this should be "Hi " only
DELTA 2: how a
DELTA 3: re you
...
RESULT: Hi ho how are you...

Instead of:

DELTA 1: Hi
DELTA 2: how a
DELTA 3: re you
...
RESULT: Hi how are you...

It seems that this code for getting events:

for await (const event of this.assistantStream) {

Is incompatible with ".stream" method on thread runs here:

const run = this.openai.beta.threads.runs.stream(this.openAiThread.id, {

As explained in this example form OpenAI: https://github.com/openai/openai-node/blob/master/examples/assistant-stream-raw.ts

	const run = this.openai.beta.threads.runs.stream(this.openAiThread.id, {
		assistant_id: this.assistant.id
	});

should instead be:

	const run = await this.openai.beta.threads.runs.create(this.openAiThread.id, {
		assistant_id: this.assistant.id,
		stream: true
	});

Notice that the returned object is of type Stream<OpenAI.Beta.Assistants.AssistantStreamEvent> instead.

If you want to use ".stream" instead, you should use ".on" events to get deltas. The advantage is that text is already aggregated and provided as "snapshot".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions