Skip to content

Commit 46491c0

Browse files
ThomasVitalemarkpollack
authored andcommitted
Relaxed toolCallArguments validation
There seems to be some models not handling correctly the non-nullability of toolCallArguments when defining a tool call. For example, the Ollama implementation is always null-safe (if no tool argument, then '{}' is used). Other implementations might produce a null value. This PR relaxes the toolCallArguments validation to support null toolCallArguments within ToolCallingObservationContext. Fixes gh-3234 Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
1 parent 81137ca commit 46491c0

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

spring-ai-model/src/main/java/org/springframework/ai/tool/observation/ToolCallingObservationContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ public final class ToolCallingObservationContext extends Observation.Context {
4747
private String toolCallResult;
4848

4949
private ToolCallingObservationContext(ToolDefinition toolDefinition, ToolMetadata toolMetadata,
50-
String toolCallArguments, @Nullable String toolCallResult) {
50+
@Nullable String toolCallArguments, @Nullable String toolCallResult) {
5151
Assert.notNull(toolDefinition, "toolDefinition cannot be null");
5252
Assert.notNull(toolMetadata, "toolMetadata cannot be null");
53-
Assert.hasText(toolCallArguments, "toolCallArguments cannot be null or empty");
5453

5554
this.toolDefinition = toolDefinition;
5655
this.toolMetadata = toolMetadata;
57-
this.toolCallArguments = toolCallArguments;
56+
this.toolCallArguments = toolCallArguments != null ? toolCallArguments : "{}";
5857
this.toolCallResult = toolCallResult;
5958
}
6059

spring-ai-model/src/test/java/org/springframework/ai/tool/observation/ToolCallingObservationContextTests.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,30 @@ class ToolCallingObservationContextTests {
3232

3333
@Test
3434
void whenMandatoryRequestOptionsThenReturn() {
35+
var observationContext = ToolCallingObservationContext.builder()
36+
.toolDefinition(ToolDefinition.builder().name("toolA").description("description").inputSchema("{}").build())
37+
.build();
38+
assertThat(observationContext).isNotNull();
39+
}
40+
41+
@Test
42+
void whenToolArgumentsIsNullThenReturn() {
43+
var observationContext = ToolCallingObservationContext.builder()
44+
.toolDefinition(ToolDefinition.builder().name("toolA").description("description").inputSchema("{}").build())
45+
.toolCallArguments(null)
46+
.build();
47+
assertThat(observationContext).isNotNull();
48+
assertThat(observationContext.getToolCallArguments()).isEqualTo("{}");
49+
}
50+
51+
@Test
52+
void whenToolArgumentsIsNotNullThenReturn() {
3553
var observationContext = ToolCallingObservationContext.builder()
3654
.toolDefinition(ToolDefinition.builder().name("toolA").description("description").inputSchema("{}").build())
3755
.toolCallArguments("lizard")
3856
.build();
3957
assertThat(observationContext).isNotNull();
58+
assertThat(observationContext.getToolCallArguments()).isEqualTo("lizard");
4059
}
4160

4261
@Test
@@ -55,22 +74,4 @@ void whenToolMetadataIsNullThenThrow() {
5574
.build()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("toolMetadata cannot be null");
5675
}
5776

58-
@Test
59-
void whenToolCallInputIsNullThenThrow() {
60-
assertThatThrownBy(() -> ToolCallingObservationContext.builder()
61-
.toolDefinition(ToolDefinition.builder().name("toolA").description("description").inputSchema("{}").build())
62-
.toolCallArguments(null)
63-
.build()).isInstanceOf(IllegalArgumentException.class)
64-
.hasMessageContaining("toolCallArguments cannot be null or empty");
65-
}
66-
67-
@Test
68-
void whenToolCallInputIsEmptyThenThrow() {
69-
assertThatThrownBy(() -> ToolCallingObservationContext.builder()
70-
.toolDefinition(ToolDefinition.builder().name("toolA").description("description").inputSchema("{}").build())
71-
.toolCallArguments("")
72-
.build()).isInstanceOf(IllegalArgumentException.class)
73-
.hasMessageContaining("toolCallArguments cannot be null or empty");
74-
}
75-
7677
}

0 commit comments

Comments
 (0)