convertTool method
Convert a Tool to Anthropic API format
Implementation
Map<String, dynamic> convertTool(Tool tool) {
try {
final schema = tool.function.parameters.toJson();
// Anthropic requires input_schema to be a valid JSON Schema object
// According to official docs, it should be type "object"
if (schema['type'] != 'object') {
// Provide helpful error message with suggestion
throw ArgumentError(
'Anthropic tools require input_schema to be of type "object". '
'Tool "${tool.function.name}" has type "${schema['type']}". '
'\n\nTo fix this, update your tool definition:\n'
'ParametersSchema(\n'
' schemaType: "object", // <- Change this to "object"\n'
' properties: {...},\n'
' required: [...],\n'
')\n\n'
'See: https://docs.anthropic.com/en/api/messages#tools');
}
// Ensure required fields are present
final inputSchema = Map<String, dynamic>.from(schema);
// Add properties if missing (empty object is valid)
if (!inputSchema.containsKey('properties')) {
inputSchema['properties'] = <String, dynamic>{};
}
return {
'name': tool.function.name,
'description': tool.function.description.isNotEmpty
? tool.function.description
: 'No description provided',
'input_schema': inputSchema,
};
} catch (e) {
// Re-throw with more context
throw ArgumentError(
'Failed to convert tool "${tool.function.name}" to Anthropic format: $e');
}
}