Skip to content

chore: refactor tool call and tool call delta parts (spec) #5746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 14, 2025
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/rotten-peaches-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/provider': major
---

chore: refactor tool call and tool call delta parts (spec)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async function main() {
finishReason: 'tool-calls',
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'cityAttractions',
Expand Down Expand Up @@ -72,6 +73,7 @@ async function main() {

return newToolCall != null
? {
type: 'tool-call' as const,
toolCallType: 'function' as const,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async function main() {
finishReason: 'tool-calls',
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'cityAttractions',
Expand Down
10 changes: 10 additions & 0 deletions packages/ai/core/generate-object/generate-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -225,6 +226,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -274,6 +276,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -337,6 +340,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -617,6 +621,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -757,6 +762,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -809,6 +815,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -860,6 +867,7 @@ describe('output = "object"', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -1199,6 +1207,7 @@ describe('telemetry', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down Expand Up @@ -1274,6 +1283,7 @@ describe('telemetry', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'tool-call-1',
toolName: 'json',
Expand Down
9 changes: 9 additions & 0 deletions packages/ai/core/generate-text/generate-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ describe('result.toolCalls', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -298,6 +299,7 @@ describe('result.toolResults', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -386,6 +388,7 @@ describe('result.response.messages', () => {
text: 'Hello, world!',
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -523,6 +526,7 @@ describe('options.maxSteps', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -991,6 +995,7 @@ describe('options.abortSignal', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -1092,6 +1097,7 @@ describe('telemetry', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -1127,6 +1133,7 @@ describe('telemetry', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -1202,6 +1209,7 @@ describe('tools with custom schema', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down Expand Up @@ -1572,6 +1580,7 @@ describe('tool execution errors', () => {
...dummyResponseValues,
toolCalls: [
{
type: 'tool-call',
toolCallType: 'function',
toolCallId: 'call-1',
toolName: 'tool1',
Expand Down
22 changes: 19 additions & 3 deletions packages/ai/core/generate-text/generate-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { recordSpan } from '../telemetry/record-span';
import { selectTelemetryAttributes } from '../telemetry/select-telemetry-attributes';
import { TelemetrySettings } from '../telemetry/telemetry-settings';
import { LanguageModel, ToolChoice } from '../types';
import { ProviderMetadata, ProviderOptions } from '../types/provider-metadata';
import { ProviderOptions } from '../types/provider-metadata';
import {
addLanguageModelUsage,
calculateLanguageModelUsage,
Expand Down Expand Up @@ -372,7 +372,15 @@ A function that attempts to repair a tool call that failed to parse.
output: () => result.text,
},
'ai.response.toolCalls': {
output: () => JSON.stringify(result.toolCalls),
output: () =>
JSON.stringify(
result.toolCalls?.map(toolCall => ({
toolCallType: toolCall.toolCallType,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
args: toolCall.args,
})),
),
},
'ai.response.id': responseData.id,
'ai.response.model': responseData.modelId,
Expand Down Expand Up @@ -546,7 +554,15 @@ A function that attempts to repair a tool call that failed to parse.
output: () => currentModelResponse.text,
},
'ai.response.toolCalls': {
output: () => JSON.stringify(currentModelResponse.toolCalls),
output: () =>
JSON.stringify(
currentModelResponse.toolCalls?.map(toolCall => ({
toolCallType: toolCall.toolCallType,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
args: toolCall.args,
})),
),
},

// TODO rename telemetry attributes to inputTokens and outputTokens
Expand Down
8 changes: 8 additions & 0 deletions packages/ai/core/generate-text/parse-tool-call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ToolCallRepairError } from '../../errors/tool-call-repair-error';
it('should successfully parse a valid tool call', async () => {
const result = await parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'testTool',
toolCallId: '123',
Expand Down Expand Up @@ -37,6 +38,7 @@ it('should successfully parse a valid tool call', async () => {
it('should successfully process empty calls for tools that have no parameters', async () => {
const result = await parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'testTool',
toolCallId: '123',
Expand Down Expand Up @@ -64,6 +66,7 @@ it('should throw NoSuchToolError when tools is null', async () => {
await expect(
parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'testTool',
toolCallId: '123',
Expand All @@ -81,6 +84,7 @@ it('should throw NoSuchToolError when tool is not found', async () => {
await expect(
parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'nonExistentTool',
toolCallId: '123',
Expand All @@ -105,6 +109,7 @@ it('should throw InvalidToolArgumentsError when args are invalid', async () => {
await expect(
parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'testTool',
toolCallId: '123',
Expand Down Expand Up @@ -136,6 +141,7 @@ describe('tool call repair', () => {

const result = await parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'testTool',
toolCallId: '123',
Expand Down Expand Up @@ -183,6 +189,7 @@ describe('tool call repair', () => {
await expect(
parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'testTool',
toolCallId: '123',
Expand Down Expand Up @@ -210,6 +217,7 @@ describe('tool call repair', () => {

const resultPromise = parseToolCall({
toolCall: {
type: 'tool-call',
toolCallType: 'function',
toolName: 'testTool',
toolCallId: '123',
Expand Down
10 changes: 5 additions & 5 deletions packages/ai/core/generate-text/parse-tool-call.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LanguageModelV2FunctionToolCall } from '@ai-sdk/provider';
import { LanguageModelV2ToolCall } from '@ai-sdk/provider';
import { safeParseJSON, safeValidateTypes } from '@ai-sdk/provider-utils';
import { Schema, asSchema } from '../util';
import { InvalidToolArgumentsError } from '../../errors/invalid-tool-arguments-error';
import { NoSuchToolError } from '../../errors/no-such-tool-error';
import { ToolCallRepairError } from '../../errors/tool-call-repair-error';
import { CoreMessage } from '../prompt';
import { asSchema } from '../util';
import { ToolCallUnion } from './tool-call';
import { ToolCallRepairFunction } from './tool-call-repair';
import { ToolSet } from './tool-set';
Expand All @@ -16,7 +16,7 @@ export async function parseToolCall<TOOLS extends ToolSet>({
system,
messages,
}: {
toolCall: LanguageModelV2FunctionToolCall;
toolCall: LanguageModelV2ToolCall;
tools: TOOLS | undefined;
repairToolCall: ToolCallRepairFunction<TOOLS> | undefined;
system: string | undefined;
Expand All @@ -39,7 +39,7 @@ export async function parseToolCall<TOOLS extends ToolSet>({
throw error;
}

let repairedToolCall: LanguageModelV2FunctionToolCall | null = null;
let repairedToolCall: LanguageModelV2ToolCall | null = null;

try {
repairedToolCall = await repairToolCall({
Expand Down Expand Up @@ -73,7 +73,7 @@ async function doParseToolCall<TOOLS extends ToolSet>({
toolCall,
tools,
}: {
toolCall: LanguageModelV2FunctionToolCall;
toolCall: LanguageModelV2ToolCall;
tools: TOOLS;
}): Promise<ToolCallUnion<TOOLS>> {
const toolName = toolCall.toolName as keyof TOOLS & string;
Expand Down
6 changes: 3 additions & 3 deletions packages/ai/core/generate-text/tool-call-repair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONSchema7, LanguageModelV2FunctionToolCall } from '@ai-sdk/provider';
import { JSONSchema7, LanguageModelV2ToolCall } from '@ai-sdk/provider';
import { InvalidToolArgumentsError } from '../../errors/invalid-tool-arguments-error';
import { NoSuchToolError } from '../../errors/no-such-tool-error';
import { CoreMessage } from '../prompt';
Expand All @@ -20,8 +20,8 @@ import { ToolSet } from './tool-set';
export type ToolCallRepairFunction<TOOLS extends ToolSet> = (options: {
system: string | undefined;
messages: CoreMessage[];
toolCall: LanguageModelV2FunctionToolCall;
toolCall: LanguageModelV2ToolCall;
tools: TOOLS;
parameterSchema: (options: { toolName: string }) => JSONSchema7;
error: NoSuchToolError | InvalidToolArgumentsError;
}) => Promise<LanguageModelV2FunctionToolCall | null>;
}) => Promise<LanguageModelV2ToolCall | null>;
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ describe('simulateStreamingMiddleware', () => {
text: 'This is a test response',
toolCalls: [
{
type: 'tool-call',
toolCallId: 'tool-1',
toolName: 'calculator',
args: '{"expression": "2+2"}',
toolCallType: 'function',
},
{
type: 'tool-call',
toolCallId: 'tool-2',
toolName: 'weather',
args: '{"location": "New York"}',
Expand Down
5 changes: 1 addition & 4 deletions packages/ai/core/middleware/simulate-streaming-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ export function simulateStreamingMiddleware(): LanguageModelV2Middleware {
argsTextDelta: toolCall.args,
});

controller.enqueue({
type: 'tool-call',
...toolCall,
});
controller.enqueue(toolCall);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/amazon-bedrock/src/bedrock-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export class BedrockChatLanguageModel implements LanguageModelV2 {
toolCalls: response.output?.message?.content
?.filter(part => !!part.toolUse)
?.map(part => ({
type: 'tool-call' as const,
toolCallType: 'function',
toolCallId: part.toolUse?.toolUseId ?? this.config.generateId(),
toolName: part.toolUse?.name ?? `tool-${this.config.generateId()}`,
Expand Down
Loading
Loading