Skip to content

Commit d306260

Browse files
authored
feat (ai): replace maxSteps with continueUntil (streamText) (#6393)
## Background The `maxSteps` parameter is not flexible enough for use cases that require more fine grained controlled over when the steps loop should end, e.g. when token consumption is exhausted or when a specific tool has been called. ## Summary Replace `maxSteps` in `streamText` with `continueUntil`, which takes a more flexible `StopCondition`. ## Future Work - Array support for `continueUntil`
1 parent 257224b commit d306260

36 files changed

+1200
-89
lines changed

‎.changeset/unlucky-bikes-fry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': major
3+
---
4+
5+
feat (ai): replace maxSteps with continueUntil (streamText)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bedrock } from '@ai-sdk/amazon-bedrock';
2-
import { ModelMessage, streamText, tool } from 'ai';
2+
import { maxSteps, ModelMessage, streamText, tool } from 'ai';
33
import 'dotenv/config';
44
import * as readline from 'node:readline/promises';
55
import { z } from 'zod';
@@ -33,7 +33,7 @@ async function main() {
3333
}),
3434
}),
3535
},
36-
maxSteps: 5,
36+
continueUntil: maxSteps(5),
3737
messages,
3838
});
3939

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bedrock } from '@ai-sdk/amazon-bedrock';
2-
import { streamText, ToolCallPart, ToolResultPart } from 'ai';
2+
import { maxSteps, streamText, ToolCallPart, ToolResultPart } from 'ai';
33
import 'dotenv/config';
44
import { z } from 'zod';
55
import { weatherTool } from '../tools/weather-tool';
@@ -14,7 +14,7 @@ async function main() {
1414
},
1515
},
1616
prompt: 'What is the weather in San Francisco?',
17-
maxSteps: 5,
17+
continueUntil: maxSteps(5),
1818
});
1919

2020
let enteredReasoning = false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';
2-
import { ModelMessage, streamText, tool } from 'ai';
2+
import { maxSteps, ModelMessage, streamText, tool } from 'ai';
33
import 'dotenv/config';
44
import * as readline from 'node:readline/promises';
55
import { z } from 'zod';
@@ -46,7 +46,7 @@ async function main() {
4646
}),
4747
}),
4848
},
49-
maxSteps: 5,
49+
continueUntil: maxSteps(5),
5050
maxRetries: 0,
5151
providerOptions: {
5252
bedrock: {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bedrock } from '@ai-sdk/amazon-bedrock';
2-
import { streamText, ToolCallPart, ToolResultPart } from 'ai';
2+
import { maxSteps, streamText, ToolCallPart, ToolResultPart } from 'ai';
33
import 'dotenv/config';
44
import { weatherTool } from '../tools/weather-tool';
55

@@ -15,7 +15,7 @@ async function main() {
1515
reasoningConfig: { type: 'enabled', budgetTokens: 1024 },
1616
},
1717
},
18-
maxSteps: 5,
18+
continueUntil: maxSteps(5),
1919
maxRetries: 5,
2020
});
2121

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bedrock } from '@ai-sdk/amazon-bedrock';
2-
import { streamText } from 'ai';
2+
import { maxSteps, streamText } from 'ai';
33
import 'dotenv/config';
44

55
async function main() {
@@ -16,7 +16,7 @@ async function main() {
1616
},
1717
},
1818
maxRetries: 0,
19-
maxSteps: 5,
19+
continueUntil: maxSteps(5),
2020
});
2121

2222
for await (const part of result.fullStream) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { anthropic } from '@ai-sdk/anthropic';
2-
import { ModelMessage, streamText, tool } from 'ai';
2+
import { maxSteps, ModelMessage, streamText, tool } from 'ai';
33
import 'dotenv/config';
44
import * as readline from 'node:readline/promises';
55
import { z } from 'zod';
@@ -33,7 +33,7 @@ async function main() {
3333
}),
3434
}),
3535
},
36-
maxSteps: 5,
36+
continueUntil: maxSteps(5),
3737
messages,
3838
});
3939

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AnthropicProviderOptions, createAnthropic } from '@ai-sdk/anthropic';
2-
import { ModelMessage, streamText, tool } from 'ai';
2+
import { maxSteps, ModelMessage, streamText, tool } from 'ai';
33
import 'dotenv/config';
44
import * as readline from 'node:readline/promises';
55
import { z } from 'zod';
@@ -46,7 +46,7 @@ async function main() {
4646
}),
4747
}),
4848
},
49-
maxSteps: 5,
49+
continueUntil: maxSteps(5),
5050
maxRetries: 0,
5151
providerOptions: {
5252
anthropic: {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { anthropic } from '@ai-sdk/anthropic';
22
import {
33
extractReasoningMiddleware,
4+
maxSteps,
45
streamText,
56
ToolCallPart,
67
ToolResultPart,
@@ -28,7 +29,7 @@ async function main() {
2829
weather: weatherTool,
2930
},
3031
prompt: 'What is the weather in San Francisco?',
31-
maxSteps: 5,
32+
continueUntil: maxSteps(5),
3233
});
3334

3435
let enteredReasoning = false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cohere } from '@ai-sdk/cohere';
2-
import { ModelMessage, streamText, tool } from 'ai';
2+
import { maxSteps, ModelMessage, streamText, tool } from 'ai';
33
import 'dotenv/config';
44
import * as readline from 'node:readline/promises';
55
import { z } from 'zod';
@@ -31,7 +31,7 @@ async function main() {
3131
}),
3232
}),
3333
},
34-
maxSteps: 5,
34+
continueUntil: maxSteps(5),
3535
messages,
3636
});
3737

0 commit comments

Comments
 (0)