From 637e00e07c5b38d7bed851ab9b15cb5e0c6fae64 Mon Sep 17 00:00:00 2001 From: extio-de Date: Sun, 18 Oct 2026 00:09:05 +0200 Subject: [PATCH] fix: strip whitespace from ToolCall fields in multiple clients --- .../de/extio/lmlib/client/anthropic/AnthropicClient.java | 7 +++++-- .../client/oai/completion/chat/ChatCompletionClient.java | 6 +++++- .../lmlib/client/oai/responses/ResponsesApiClient.java | 4 ++-- .../java/de/extio/lmlib/client/ollama/OllamaClient.java | 6 +++--- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/extio/lmlib/client/anthropic/AnthropicClient.java b/src/main/java/de/extio/lmlib/client/anthropic/AnthropicClient.java index 21566fa..a77825e 100644 --- a/src/main/java/de/extio/lmlib/client/anthropic/AnthropicClient.java +++ b/src/main/java/de/extio/lmlib/client/anthropic/AnthropicClient.java @@ -21,7 +21,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatusCode; import org.springframework.web.client.RestClient; import com.fasterxml.jackson.annotation.JsonInclude; @@ -621,7 +620,7 @@ public class AnthropicClient implements Client { } break; case "tool_use": - toolCalls.add(new ToolCall(block.id(), "tool_use", block.name(), this.serializeToolInput(block.input()))); + toolCalls.add(new ToolCall(strip(block.id()), "tool_use", strip(block.name()), this.serializeToolInput(block.input()))); break; default: LOGGER.debug("Ignoring unsupported response content block type: {}", block.type()); @@ -791,6 +790,10 @@ public class AnthropicClient implements Client { return this.anthropicDialect == null ? DEFAULT_ANTHROPIC_DIALECT : this.anthropicDialect; } + private static String strip(final String text) { + return text == null ? null : text.strip(); + } + private static final class StreamBlock { private String type; diff --git a/src/main/java/de/extio/lmlib/client/oai/completion/chat/ChatCompletionClient.java b/src/main/java/de/extio/lmlib/client/oai/completion/chat/ChatCompletionClient.java index 53b094f..8f14b84 100755 --- a/src/main/java/de/extio/lmlib/client/oai/completion/chat/ChatCompletionClient.java +++ b/src/main/java/de/extio/lmlib/client/oai/completion/chat/ChatCompletionClient.java @@ -295,10 +295,14 @@ public class ChatCompletionClient extends AbstractCompletionClient { } return chatToolCalls.stream() .filter(Objects::nonNull) - .map(toolCall -> new ToolCall(toolCall.getId(), toolCall.getType(), toolCall.getFunction() != null ? toolCall.getFunction().getName() : null, toolCall.getFunction() != null ? toolCall.getFunction().getArguments() : null)) + .map(toolCall -> new ToolCall(strip(toolCall.getId()), strip(toolCall.getType()), toolCall.getFunction() != null ? strip(toolCall.getFunction().getName()) : null, toolCall.getFunction() != null ? strip(toolCall.getFunction().getArguments()) : null)) .toList(); } + private static String strip(final String text) { + return text == null ? null : text.strip(); + } + private List createChats(final Conversation conversation, final ModelProfile modelProfile) { final List chat = new ArrayList<>(); final AtomicInteger tokens = new AtomicInteger(); diff --git a/src/main/java/de/extio/lmlib/client/oai/responses/ResponsesApiClient.java b/src/main/java/de/extio/lmlib/client/oai/responses/ResponsesApiClient.java index 2503d41..ec6aee6 100644 --- a/src/main/java/de/extio/lmlib/client/oai/responses/ResponsesApiClient.java +++ b/src/main/java/de/extio/lmlib/client/oai/responses/ResponsesApiClient.java @@ -609,9 +609,9 @@ public class ResponsesApiClient implements Client { } final var callId = this.text(item, "call_id"); toolCalls.add(new ToolCall( - callId.isBlank() ? this.text(item, "id") : callId, + callId.isBlank() ? this.text(item, "id") : callId.strip(), "function", - this.text(item, "name"), + this.text(item, "name").strip(), this.text(item, "arguments"))); } return List.copyOf(toolCalls); diff --git a/src/main/java/de/extio/lmlib/client/ollama/OllamaClient.java b/src/main/java/de/extio/lmlib/client/ollama/OllamaClient.java index 605a1a9..191da72 100644 --- a/src/main/java/de/extio/lmlib/client/ollama/OllamaClient.java +++ b/src/main/java/de/extio/lmlib/client/ollama/OllamaClient.java @@ -352,9 +352,9 @@ public class OllamaClient implements Client { continue; } toolCalls.add(new ToolCall( - toolCall.getId() != null && !toolCall.getId().isBlank() ? toolCall.getId() : "ollama-tool-call-" + (i + 1), - toolCall.getType() != null && !toolCall.getType().isBlank() ? toolCall.getType() : "function", - toolCall.getFunction().getName(), + toolCall.getId() != null && !toolCall.getId().isBlank() ? toolCall.getId().strip() : "ollama-tool-call-" + (i + 1), + toolCall.getType() != null && !toolCall.getType().isBlank() ? toolCall.getType().strip() : "function", + toolCall.getFunction().getName().strip(), this.serializeArguments(toolCall.getFunction().getArguments()))); } return List.copyOf(toolCalls);