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/spotty-apples-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix(ai): Unexpected reasoning-start event in extract reasoning middleware
100 changes: 87 additions & 13 deletions packages/ai/core/middleware/extract-reasoning-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,20 +683,12 @@ describe('extractReasoningMiddleware', () => {
"id": "1",
"type": "text-start",
},
{
"id": "reasoning-0",
"type": "reasoning-start",
},
{
"id": "1",
"providerMetadata": undefined,
"text": "ana",
"type": "text",
},
{
"id": "reasoning-0",
"type": "reasoning-start",
},
{
"id": "1",
"providerMetadata": undefined,
Expand All @@ -705,18 +697,100 @@ describe('extractReasoningMiddleware', () => {
"type": "text",
},
{
"id": "reasoning-0",
"type": "reasoning-start",
"id": "1",
"providerMetadata": undefined,
"text": "</think>",
"type": "text",
},
Comment on lines +700 to 704
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be removed?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(wondering if there is another bug hidden here)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this test case and fix, when startWithReasoning equals false, only one reasoning-start event should be sent. Theoretically, there shouldn't be any bug.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for checking!

{
"id": "1",
"providerMetadata": undefined,
"text": "</think>",
"text": "this is the response",
"type": "text",
},
{
"id": "reasoning-0",
"type": "reasoning-start",
"id": "1",
"type": "text-end",
},
{
"finishReason": "stop",
"providerMetadata": undefined,
"response": {
"headers": undefined,
"id": "id-0",
"modelId": "mock-model-id",
"timestamp": 1970-01-01T00:00:00.000Z,
},
"type": "finish-step",
"usage": {
"cachedInputTokens": undefined,
"inputTokens": 5,
"outputTokens": 10,
"reasoningTokens": 3,
"totalTokens": 18,
},
},
{
"finishReason": "stop",
"totalUsage": {
"cachedInputTokens": undefined,
"inputTokens": 5,
"outputTokens": 10,
"reasoningTokens": 3,
"totalTokens": 18,
},
"type": "finish",
},
]
`);
});

it('should keep original text when <think> tag is not present', async () => {
const mockModel = new MockLanguageModelV2({
async doStream() {
return {
stream: convertArrayToReadableStream([
{
type: 'response-metadata',
id: 'id-0',
modelId: 'mock-model-id',
timestamp: new Date(0),
},
{ type: 'text-start', id: '1' },
{ type: 'text-delta', id: '1', delta: 'this is the response' },
{ type: 'text-end', id: '1' },
{
type: 'finish',
finishReason: 'stop',
usage: testUsage,
},
]),
};
},
});

const result = streamText({
model: wrapLanguageModel({
model: mockModel,
middleware: extractReasoningMiddleware({ tagName: 'think' }),
}),
prompt: 'Hello, how can I help?',
});

expect(await convertAsyncIterableToArray(result.fullStream))
.toMatchInlineSnapshot(`
[
{
"type": "start",
},
{
"request": {},
"type": "start-step",
"warnings": [],
},
{
"id": "1",
"type": "text-start",
},
{
"id": "1",
Expand Down
6 changes: 3 additions & 3 deletions packages/ai/core/middleware/extract-reasoning-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ export function extractReasoningMiddleware({
: '';

if (
(activeExtraction.afterSwitch &&
activeExtraction.isReasoning) ||
activeExtraction.isFirstReasoning
activeExtraction.isReasoning &&
(activeExtraction.afterSwitch ||
activeExtraction.isFirstReasoning)
) {
controller.enqueue({
type: 'reasoning-start',
Expand Down
Loading