Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tidy-reasoning-items-close.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/open-responses': patch
---

fix(open-responses): close unfinished reasoning stream parts with their original IDs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,62 @@ describe('OpenResponsesLanguageModel', () => {
});
});

it('should close unfinished reasoning items with their original ids', async () => {
server.urls[URL].response = {
type: 'stream-chunks',
chunks: [
`data: ${JSON.stringify({
type: 'response.output_item.added',
sequence_number: 0,
output_index: 0,
item: {
id: 'rs_reasoning_item',
type: 'reasoning',
summary: [],
},
})}\n\n`,
`data: ${JSON.stringify({
type: 'response.incomplete',
sequence_number: 1,
response: {
status: 'incomplete',
incomplete_details: { reason: 'max_output_tokens' },
usage: {
input_tokens: 1,
input_tokens_details: { cached_tokens: 0 },
output_tokens: 1,
output_tokens_details: { reasoning_tokens: 1 },
total_tokens: 2,
},
},
})}\n\n`,
'data: [DONE]\n\n',
],
};

const result = await createModel().doStream({
prompt: TEST_PROMPT,
});

const parts = await convertReadableStreamToArray(result.stream);

expect(parts).toContainEqual({
type: 'reasoning-start',
id: 'rs_reasoning_item',
});
expect(parts).toContainEqual({
type: 'reasoning-end',
id: 'rs_reasoning_item',
});
expect(parts.at(-1)).toMatchObject({
type: 'finish',
finishReason: {
unified: 'length',
raw: 'max_output_tokens',
},
});
});

it('should not pollute Object.prototype from tool call item ids', async () => {
const originalArgumentsDescriptor = Object.getOwnPropertyDescriptor(
Object.prototype,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export class OpenResponsesLanguageModel implements LanguageModelV4 {
usage.raw = responseUsage;
};

let isActiveReasoning = false;
let activeReasoningId: string | undefined;
let hasToolCalls = false;
let finishReason: LanguageModelV4FinishReason = {
unified: 'other',
Expand Down Expand Up @@ -503,7 +503,7 @@ export class OpenResponsesLanguageModel implements LanguageModelV4 {
type: 'reasoning-start',
id: chunk.item.id,
});
isActiveReasoning = true;
activeReasoningId = chunk.item.id;
} else if (
(chunk as { type: string }).type ===
'response.reasoning_text.delta'
Expand All @@ -522,7 +522,9 @@ export class OpenResponsesLanguageModel implements LanguageModelV4 {
chunk.item.type === 'reasoning'
) {
controller.enqueue({ type: 'reasoning-end', id: chunk.item.id });
isActiveReasoning = false;
if (activeReasoningId === chunk.item.id) {
activeReasoningId = undefined;
}
}

// Text events
Expand Down Expand Up @@ -565,8 +567,11 @@ export class OpenResponsesLanguageModel implements LanguageModelV4 {
},

flush(controller) {
if (isActiveReasoning) {
controller.enqueue({ type: 'reasoning-end', id: 'reasoning-0' });
if (activeReasoningId != null) {
controller.enqueue({
type: 'reasoning-end',
id: activeReasoningId,
});
}

controller.enqueue({
Expand Down