Skip to content

Commit 8419f55

Browse files
committed
Ensures that the type field for each AnthropicToolSchema is serialized when we construct the request body.
1 parent 7f69dba commit 8419f55

File tree

2 files changed

+44
-4
lines changed
  • prompt/prompt-executor/prompt-executor-clients

2 files changed

+44
-4
lines changed

prompt/prompt-executor/prompt-executor-clients/prompt-executor-anthropic-client/src/commonMain/kotlin/ai/koog/prompt/executor/clients/anthropic/DataModel.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,20 +249,23 @@ public data class AnthropicTool(
249249
* Represents a schema definition for an Anthropic tool utilized in LLM clients. This data class
250250
* defines the structure expected for tools, including the type of schema, properties, and required fields.
251251
*
252-
* This API is internal and should not be used outside of its intended scope, as it might be subject
252+
* This API is internal and should not be used outside its intended scope, as it might be subject
253253
* to changes or removal without notice.
254254
*
255-
* @property type The type of the schema, defaulting to "object".
256255
* @property properties A JSON object representing the properties within this schema.
257256
* @property required A list of property names that are mandatory within this schema.
258257
*/
259258
@InternalLLMClientApi
260259
@Serializable
261260
public data class AnthropicToolSchema(
262-
val type: String = "object",
263261
val properties: JsonObject,
264262
val required: List<String>
265-
)
263+
) {
264+
/**
265+
* The type of the schema. Always returns "object" for Anthropic tool schemas.
266+
*/
267+
val type: String = "object"
268+
}
266269

267270
/**
268271
* Represents the response structure from an Anthropic API call.

prompt/prompt-executor/prompt-executor-clients/prompt-executor-bedrock-client/src/jvmTest/kotlin/ai/koog/prompt/executor/clients/bedrock/modelfamilies/anthropic/BedrockAnthropicClaudeSerializationTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,41 @@ class BedrockAnthropicClaudeSerializationTest {
394394
val content = BedrockAnthropicClaudeSerialization.parseAnthropicStreamChunk(chunkJson)
395395
assertEquals("", content)
396396
}
397+
398+
@Test
399+
fun `createAnthropicRequest with tools serializes type field correctly`() {
400+
val tools = listOf(
401+
ToolDescriptor(
402+
name = toolName,
403+
description = toolDescription,
404+
requiredParameters = listOf(
405+
ToolParameterDescriptor("city", "The city name", ToolParameterType.String)
406+
),
407+
optionalParameters = listOf(
408+
ToolParameterDescriptor("units", "Temperature units", ToolParameterType.String)
409+
)
410+
)
411+
)
412+
val prompt = Prompt.build("test", params = LLMParams(toolChoice = LLMParams.ToolChoice.Auto)) {
413+
user(userMessageQuestion)
414+
}
415+
val request = BedrockAnthropicClaudeSerialization.createAnthropicRequest(prompt, model, tools)
416+
assertNotNull(request)
417+
assertNotNull(request.tools)
418+
assertEquals(1, request.tools?.size)
419+
val tool = request.tools?.get(0)
420+
assertNotNull(tool)
421+
assertEquals(toolName, tool.name)
422+
assertEquals(toolDescription, tool.description)
423+
val schema = tool.inputSchema
424+
assertNotNull(schema)
425+
426+
// Verify that the type field is always "object" and gets serialized
427+
assertEquals("object", schema.type)
428+
429+
assertEquals(listOf("city"), schema.required)
430+
val properties = schema.properties.jsonObject
431+
assertNotNull(properties["city"])
432+
assertNotNull(properties["units"])
433+
}
397434
}

0 commit comments

Comments
 (0)