Merge pull request #608 from buster-so/dallin/hotfix-disable-parallel-tool-calls

Add fetch body modification for tool_choice in Anthropics and Vertex models
This commit is contained in:
dal 2025-07-23 09:35:03 -06:00 committed by GitHub
commit 3a4498c4fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 0 deletions

View File

@ -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

View File

@ -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