Skip to content

Commit 0252881

Browse files
committed
fix(core): keep a schema-only tool's declared outputSchema
1 parent 634fe8d commit 0252881

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎agentscope-core/src/main/java/io/agentscope/core/tool/SchemaOnlyTool.java‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class SchemaOnlyTool extends ToolBase {
5757

5858
private final Boolean strict;
5959
private final Boolean deferLoading;
60+
private final Map<String, Object> outputSchema;
6061

6162
/**
6263
* Creates a new SchemaOnlyTool from a ToolSchema.
@@ -71,7 +72,8 @@ public SchemaOnlyTool(ToolSchema schema) {
7172
schema.getDescription(),
7273
schema.getParameters(),
7374
schema.getStrict(),
74-
schema.getDeferLoading());
75+
schema.getDeferLoading(),
76+
schema.getOutputSchema());
7577
}
7678

7779
/**
@@ -109,6 +111,32 @@ public SchemaOnlyTool(
109111
Map<String, Object> parameters,
110112
Boolean strict,
111113
Boolean deferLoading) {
114+
this(name, description, parameters, strict, deferLoading, null);
115+
}
116+
117+
/**
118+
* Creates a new SchemaOnlyTool with the specified name, description, parameters, strict mode,
119+
* deferred loading, and output schema.
120+
*
121+
* <p>The parameters and output schema maps are defensively copied to prevent external
122+
* mutations from affecting the tool's internal state.
123+
*
124+
* @param name The tool name
125+
* @param description The tool description
126+
* @param parameters The tool parameters schema
127+
* @param strict Whether the tool should use strict schema validation (null if unspecified)
128+
* @param deferLoading Whether to defer loading this tool's schema until tool search (null if
129+
* unspecified)
130+
* @param outputSchema The structured output schema the tool declares, if any
131+
* @throws NullPointerException if name or description is null
132+
*/
133+
public SchemaOnlyTool(
134+
String name,
135+
String description,
136+
Map<String, Object> parameters,
137+
Boolean strict,
138+
Boolean deferLoading,
139+
Map<String, Object> outputSchema) {
112140
super(
113141
ToolBase.builder()
114142
.name(Objects.requireNonNull(name, "name cannot be null"))
@@ -123,6 +151,10 @@ public SchemaOnlyTool(
123151
.concurrencySafe(true));
124152
this.strict = strict;
125153
this.deferLoading = deferLoading;
154+
this.outputSchema =
155+
outputSchema != null
156+
? Collections.unmodifiableMap(new HashMap<>(outputSchema))
157+
: null;
126158
}
127159

128160
/**
@@ -152,6 +184,11 @@ public Boolean getDeferLoading() {
152184
return deferLoading;
153185
}
154186

187+
@Override
188+
public Map<String, Object> getOutputSchema() {
189+
return outputSchema;
190+
}
191+
155192
/**
156193
* Throws a ToolSuspendException to signal that this tool requires external execution.
157194
*

‎agentscope-core/src/test/java/io/agentscope/core/tool/SchemaOnlyToolTest.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,49 @@ void testDeferLoadingPreservedFromToolSchema() {
8989
assertEquals(Boolean.TRUE, tool.getDeferLoading());
9090
}
9191

92+
@Test
93+
@DisplayName("Should preserve outputSchema when creating from ToolSchema")
94+
void testOutputSchemaPreservedFromToolSchema() {
95+
Map<String, Object> outputSchema =
96+
Map.of("type", "object", "properties", Map.of("answer", Map.of("type", "string")));
97+
ToolSchema schemaWithOutput =
98+
ToolSchema.builder()
99+
.name("external_lookup")
100+
.description("Look something up externally")
101+
.parameters(Map.of("type", "object", "properties", Map.of()))
102+
.outputSchema(outputSchema)
103+
.build();
104+
105+
SchemaOnlyTool tool = new SchemaOnlyTool(schemaWithOutput);
106+
107+
assertEquals(outputSchema, tool.getOutputSchema());
108+
}
109+
110+
@Test
111+
@DisplayName("Should keep the declared outputSchema through Toolkit.getToolSchemas")
112+
void testOutputSchemaSurvivesToolkitRegistration() {
113+
Map<String, Object> outputSchema =
114+
Map.of("type", "object", "properties", Map.of("answer", Map.of("type", "string")));
115+
ToolSchema schema =
116+
ToolSchema.builder()
117+
.name("external_lookup")
118+
.description("Look something up externally")
119+
.parameters(Map.of("type", "object", "properties", Map.of()))
120+
.outputSchema(outputSchema)
121+
.build();
122+
Toolkit toolkit = new Toolkit();
123+
124+
toolkit.registerSchema(schema);
125+
126+
assertEquals(
127+
outputSchema,
128+
toolkit.getToolSchemas().stream()
129+
.filter(exposed -> "external_lookup".equals(exposed.getName()))
130+
.findFirst()
131+
.orElseThrow()
132+
.getOutputSchema());
133+
}
134+
92135
@Test
93136
@DisplayName("Should create SchemaOnlyTool with name, description, parameters")
94137
void testCreateWithParameters() {

0 commit comments

Comments
 (0)