From 5aa781a536721e870fac437893c61b9b4034f5a6 Mon Sep 17 00:00:00 2001 From: dal Date: Wed, 23 Jul 2025 09:32:07 -0600 Subject: [PATCH] Add fetch body modification for tool_choice in Anthropics and Vertex models - Implemented a custom fetch function in both anthropic.ts and vertex.ts to modify the request body. - If tool_choice is present, added disable_parallel_tool_use to the request body. - Included error handling for JSON parsing to ensure fallback to original request on failure. --- .../src/utils/models/providers/anthropic.ts | 34 +++++++++++++++++++ .../ai/src/utils/models/providers/vertex.ts | 34 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/packages/ai/src/utils/models/providers/anthropic.ts b/packages/ai/src/utils/models/providers/anthropic.ts index 819f41392..f4b611eaf 100644 --- a/packages/ai/src/utils/models/providers/anthropic.ts +++ b/packages/ai/src/utils/models/providers/anthropic.ts @@ -6,6 +6,40 @@ export const anthropicModel = (modelId: string) => { headers: { 'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14', }, + fetch: ((url, options) => { + if (options?.body) { + try { + // Parse existing body if it's a string + const existingBody = + typeof options.body === 'string' ? JSON.parse(options.body) : options.body; + + // Append disable_parallel_tool_use if tool_choice is present + const modifiedBody = { + ...existingBody, + }; + + if (modifiedBody.tool_choice) { + modifiedBody.tool_choice = { + ...modifiedBody.tool_choice, + disable_parallel_tool_use: true, + }; + } + + // Return modified options + return fetch(url, { + ...options, + body: JSON.stringify(modifiedBody), + }); + } catch (error) { + console.error('Failed to parse request body:', error); + // If body parsing fails, fall back to original request + return fetch(url, options); + } + } + + // For requests without body, pass through unchanged + return fetch(url, options); + }) as typeof fetch, }); // Wrap the model with Braintrust tracing and return it diff --git a/packages/ai/src/utils/models/providers/vertex.ts b/packages/ai/src/utils/models/providers/vertex.ts index c4f3ca934..3c2cafcbc 100644 --- a/packages/ai/src/utils/models/providers/vertex.ts +++ b/packages/ai/src/utils/models/providers/vertex.ts @@ -34,6 +34,40 @@ export const vertexModel = (modelId: string): LanguageModelV1 => { headers: { 'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14', }, + fetch: ((url, options) => { + if (options?.body) { + try { + // Parse existing body if it's a string + const existingBody = + typeof options.body === 'string' ? JSON.parse(options.body) : options.body; + + // Append disable_parallel_tool_use if tool_choice is present + const modifiedBody = { + ...existingBody, + }; + + if (modifiedBody.tool_choice) { + modifiedBody.tool_choice = { + ...modifiedBody.tool_choice, + disable_parallel_tool_use: true, + }; + } + + // Return modified options + return fetch(url, { + ...options, + body: JSON.stringify(modifiedBody), + }); + } catch (error) { + console.error('Failed to parse request body:', error); + // If body parsing fails, fall back to original request + return fetch(url, options); + } + } + + // For requests without body, pass through unchanged + return fetch(url, options); + }) as typeof fetch, }); // Wrap the model with Braintrust tracing