fix: include empty tools param when conversation has tool history (#150)

Anthropic (via LiteLLM/proxy) requires the `tools` parameter to be
present when messages include tool_calls or tool role messages,
even if no tools are currently being provided.

This adds a `hasToolHistory()` helper to detect if the conversation
contains tool calls or tool results, and ensures `tools: []` is
included in the request params when needed.

Fixes #149

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Ubuntu <ubuntu@ip-172-31-50-87.us-west-2.compute.internal>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Tiago Freitas 2025-12-08 20:30:43 +00:00 committed by GitHub
parent 00370cab39
commit 8f67e00160
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -26,6 +26,26 @@ import { parseStreamingJson } from "../utils/json-parse.js";
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
import { transformMessages } from "./transorm-messages.js";
import type { Message } from "../types.js";
/**
* Check if conversation messages contain tool calls or tool results.
* This is needed because Anthropic (via proxy) requires the tools param
* to be present when messages include tool_calls or tool role messages.
*/
function hasToolHistory(messages: Message[]): boolean {
for (const msg of messages) {
if (msg.role === "toolResult") {
return true;
}
if (msg.role === "assistant") {
if (msg.content.some((block) => block.type === "toolCall")) {
return true;
}
}
}
return false;
}
export interface OpenAICompletionsOptions extends StreamOptions {
toolChoice?: "auto" | "none" | "required" | { type: "function"; function: { name: string } };
@ -296,6 +316,9 @@ function buildParams(model: Model<"openai-completions">, context: Context, optio
if (context.tools) {
params.tools = convertTools(context.tools);
} else if (hasToolHistory(context.messages)) {
// Anthropic (via LiteLLM/proxy) requires tools param when conversation has tool_calls/tool_results
params.tools = [];
}
if (options?.toolChoice) {