Skip to content

Commit a3f768e

Browse files
authored
chore: restructure reasoning support (#5922)
## Background Different providers have different reasoning options that are specific to them, e.g. Anthropic (signatures, redacted reasoning) and OpenAI (reasoning id and parts). It is also unclear that e.g. the signature is provider-specific. ## Summary Replaces existing reasoning with more flexible mechanism based on providerOptions and providerMetadata.
1 parent 24ceaab commit a3f768e

File tree

70 files changed

+1397
-1372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1397
-1372
lines changed

‎.changeset/nice-tips-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/provider': major
3+
---
4+
5+
chore: restructure reasoning support

‎examples/ai-core/src/generate-text/anthropic-reasoning-chatbot.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
1+
import { createAnthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
22
import { CoreMessage, generateText } from 'ai';
33
import 'dotenv/config';
44
import * as readline from 'node:readline/promises';
55
import { weatherTool } from '../tools/weather-tool';
66

7+
const anthropic = createAnthropic({
8+
// example fetch wrapper that logs the input to the API call:
9+
fetch: async (url, options) => {
10+
console.log('URL', url);
11+
console.log('Headers', JSON.stringify(options!.headers, null, 2));
12+
console.log(
13+
`Body ${JSON.stringify(JSON.parse(options!.body! as string), null, 2)}`,
14+
);
15+
return await fetch(url, options);
16+
},
17+
});
18+
719
const terminal = readline.createInterface({
820
input: process.stdin,
921
output: process.stdout,

‎examples/ai-core/src/stream-text/amazon-bedrock-fullstream.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ async function main() {
2525
for await (const part of result.fullStream) {
2626
switch (part.type) {
2727
case 'reasoning': {
28-
if (part.reasoningType === 'text') {
29-
if (!enteredReasoning) {
30-
enteredReasoning = true;
31-
console.log('\nREASONING:\n');
32-
}
33-
process.stdout.write(part.text);
28+
if (!enteredReasoning) {
29+
enteredReasoning = true;
30+
console.log('\nREASONING:\n');
3431
}
32+
process.stdout.write(part.text);
3533
break;
3634
}
3735

‎examples/ai-core/src/stream-text/amazon-bedrock-reasoning-chatbot.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,8 @@ async function main() {
6060

6161
process.stdout.write('\nAssistant: ');
6262
for await (const part of result.fullStream) {
63-
if (part.type === 'reasoning' && part.reasoningType === 'text') {
63+
if (part.type === 'reasoning') {
6464
process.stdout.write('\x1b[34m' + part.text + '\x1b[0m');
65-
} else if (
66-
part.type === 'reasoning' &&
67-
part.reasoningType === 'redacted'
68-
) {
69-
process.stdout.write('\x1b[31m' + '<redacted>' + '\x1b[0m');
7065
} else if (part.type === 'text') {
7166
process.stdout.write(part.text);
7267
}

‎examples/ai-core/src/stream-text/amazon-bedrock-reasoning-fullstream.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ async function main() {
2727
for await (const part of result.fullStream) {
2828
switch (part.type) {
2929
case 'reasoning': {
30-
if (part.reasoningType === 'text') {
31-
if (!enteredReasoning) {
32-
enteredReasoning = true;
33-
console.log('\nREASONING:\n');
34-
}
35-
process.stdout.write(part.text);
30+
if (!enteredReasoning) {
31+
enteredReasoning = true;
32+
console.log('\nREASONING:\n');
3633
}
34+
process.stdout.write(part.text);
3735
break;
3836
}
3937

‎examples/ai-core/src/stream-text/amazon-bedrock-reasoning.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ async function main() {
2020
});
2121

2222
for await (const part of result.fullStream) {
23-
if (part.type === 'reasoning' && part.reasoningType === 'text') {
23+
if (part.type === 'reasoning') {
2424
process.stdout.write('\x1b[34m' + part.text + '\x1b[0m');
25-
} else if (part.type === 'reasoning' && part.reasoningType === 'redacted') {
26-
process.stdout.write('\x1b[31m' + '<redacted>' + '\x1b[0m');
2725
} else if (part.type === 'text') {
2826
process.stdout.write(part.text);
2927
}

‎examples/ai-core/src/stream-text/anthropic-reasoning-chatbot.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,8 @@ async function main() {
6060

6161
process.stdout.write('\nAssistant: ');
6262
for await (const part of result.fullStream) {
63-
if (part.type === 'reasoning' && part.reasoningType === 'text') {
63+
if (part.type === 'reasoning') {
6464
process.stdout.write('\x1b[34m' + part.text + '\x1b[0m');
65-
} else if (
66-
part.type === 'reasoning' &&
67-
part.reasoningType === 'redacted'
68-
) {
69-
process.stdout.write('\x1b[31m' + '<redacted>' + '\x1b[0m');
7065
} else if (part.type === 'text') {
7166
process.stdout.write(part.text);
7267
}

‎examples/ai-core/src/stream-text/anthropic-reasoning-fullstream.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ async function main() {
3636
for await (const part of result.fullStream) {
3737
switch (part.type) {
3838
case 'reasoning': {
39-
if (part.reasoningType === 'text') {
40-
if (!enteredReasoning) {
41-
enteredReasoning = true;
42-
console.log('\nREASONING:\n');
43-
}
44-
process.stdout.write(part.text);
39+
if (!enteredReasoning) {
40+
enteredReasoning = true;
41+
console.log('\nREASONING:\n');
4542
}
43+
process.stdout.write(part.text);
4644
break;
4745
}
4846

‎examples/ai-core/src/stream-text/anthropic-reasoning.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ async function main() {
1919
});
2020

2121
for await (const part of result.fullStream) {
22-
if (part.type === 'reasoning' && part.reasoningType === 'text') {
22+
if (part.type === 'reasoning') {
2323
process.stdout.write('\x1b[34m' + part.text + '\x1b[0m');
24-
} else if (part.type === 'reasoning' && part.reasoningType === 'redacted') {
25-
process.stdout.write('\x1b[31m' + '<redacted>' + '\x1b[0m');
24+
25+
if (part.providerMetadata?.anthropic?.redactedData != null) {
26+
process.stdout.write('\x1b[31m' + '<redacted>' + '\x1b[0m');
27+
}
2628
} else if (part.type === 'text') {
2729
process.stdout.write(part.text);
2830
}

‎examples/ai-core/src/stream-text/deepseek-reasoning.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function main() {
99
});
1010

1111
for await (const part of result.fullStream) {
12-
if (part.type === 'reasoning' && part.reasoningType === 'text') {
12+
if (part.type === 'reasoning') {
1313
process.stdout.write('\x1b[34m' + part.text + '\x1b[0m');
1414
} else if (part.type === 'text') {
1515
process.stdout.write(part.text);

0 commit comments

Comments
 (0)