Skip to content

Commit

Permalink
fix (ai/core): correct spread apply order to fix extract reasoning mi…
Browse files Browse the repository at this point in the history
…ddleware with generateText (#5079)
  • Loading branch information
shaper authored Mar 6, 2025
1 parent 9dcc12d commit ee1c787
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tricky-squids-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix (ai/core): correct spread apply order to fix extract reasoning middleware with generateText
25 changes: 25 additions & 0 deletions packages/ai/core/middleware/extract-reasoning-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ describe('extractReasoningMiddleware', () => {
'analyzing the request</think>Here is the response',
);
});

it('should preserve reasoning property even when rest contains other properties', async () => {
const mockModel = new MockLanguageModelV1({
async doGenerate() {
return {
text: '<think>analyzing the request</think>Here is the response',
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 10 },
rawCall: { rawPrompt: '', rawSettings: {} },
reasoning: undefined,
};
},
});

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

expect(result.reasoning).toStrictEqual('analyzing the request');
expect(result.text).toStrictEqual('Here is the response');
});
});

describe('wrapStream', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function extractReasoningMiddleware({
afterMatch;
}

return { text: textWithoutReasoning, reasoning, ...rest };
return { ...rest, text: textWithoutReasoning, reasoning };
},

wrapStream: async ({ doStream }) => {
Expand Down

0 comments on commit ee1c787

Please sign in to comment.