Overview
A ToolType
consists of the following main components:
The type of tool.Constraints: Must be one of the defined tool types.
The tool definition containing the schema.Constraints: Must contain a valid schema.
The type
field uses a discriminated union of predefined tool types.
Function-based tools.Usage: Used for defining callable functions with parameters.
Currently, only one tool type is supported:
// Example of a function tool
const searchTool: ToolType = {
type: "function",
definition: {
schema: {
name: "search_database",
description: "Search for records in the database",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The search query",
},
limit: {
type: "number",
description: "Maximum number of results",
default: 10,
},
},
required: ["query"],
},
},
},
};
Represents a function that can be called by the LLM.
Tool type discriminator.Constraints: Must be "function"
.
Contains the function schema.Constraints: Must contain valid function schema.
The function schema definition.Constraints: Must be a valid function schema.
Function Schema
FunctionType
Defines the structure and behavior of a callable function.
Function name.Constraints: 1-64 characters, alphanumeric and underscore only.
Function description.Constraints: Maximum 4096 characters.
Function parameters schema.Constraints: Flexible parameter definition.
Whether to enforce strict parameter validation.Constraints: Optional field.
Name Validation
The function name must follow these rules:
- Length: 1-64 characters
- Characters: Only letters (a-z, A-Z), numbers (0-9), and underscores (_)
- Pattern:
^[a-zA-Z0-9_]{1,64}$
// Valid function names
"search_database"; // ✅ Valid
"getUserInfo"; // ✅ Valid
"calculate_2024_tax"; // ✅ Valid
// Invalid function names
"search-database"; // ❌ Contains hyphen
"search database"; // ❌ Contains space
"search.database"; // ❌ Contains dot
(""); // ❌ Empty string
("a_very_long_function_name_that_exceeds_sixty_four_characters_limit"); // ❌ Too long
Function Parameters
FunctionParametersType
Defines the structure for function parameters using JSON Schema-like definitions.
Parameter container type.Constraints: Must be "object"
.
Optional title for the parameters.Constraints: Optional field.
Schema definitions for reusable components.Constraints: Optional field.
properties
Record<string, FunctionParameterType>
Parameter property definitions.Constraints: Optional field.
List of required parameter names.Constraints: Optional field.
FunctionParameterType
Defines individual parameter properties with comprehensive JSON Schema support.
Union type definitions.Constraints: Optional field.
type
FunctionParameterTypesType
Parameter data typeConstraints: Optional, see supported types below.
Default value for the parameter.Constraints: Optional field.
Parameter title.Constraints: Optional field.
Parameter description.Constraints: Maximum 4096 characters.
Nested object propertiesConstraints: Optional, for object types.
Required nested properties.Constraints: Optional, for object types.
Minimum array length.Constraints: Optional, for array types, non-negative
Maximum array length.Constraints: Optional, for array types
Array item schema.Constraints: Optional, for array types
enum
Array<string | number | boolean | null>
Allowed values.Constraints: Optional field.
Minimum numeric value.Constraints: Optional, for number types
Maximum numeric value.Constraints: Optional, for number types
Minimum string length.Constraints: Optional, for string types, non-negative
Maximum string length.Constraints: Optional, for string types
FunctionParameterTypesType
Supported parameter data types:
Object/dictionary type.Usage: For complex nested structures.
Array type.Usage: For lists of items.
Numeric type.Usage: For integers and floating-point numbers.
String type.Usage: For text values.
Boolean type.Usage: For true/false values.
Null type.Usage: For null values.
Complete Examples
const simpleFunction: ToolType = {
type: "function",
definition: {
schema: {
name: "get_weather",
description: "Get current weather information for a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
},
required: ["location"],
},
},
},
};
const complexFunction: ToolType = {
type: "function",
definition: {
schema: {
name: "analyze_data",
description: "Analyze data with various filters and options",
parameters: {
type: "object",
properties: {
data_source: {
type: "string",
description: "The data source to analyze",
enum: ["database", "api", "file"],
},
filters: {
type: "object",
properties: {
date_range: {
type: "object",
properties: {
start: {
type: "string",
description: "Start date in ISO format",
},
end: {
type: "string",
description: "End date in ISO format",
},
},
required: ["start", "end"],
},
categories: {
type: "array",
items: {
type: "string",
},
minItems: 1,
maxItems: 10,
description: "Categories to include in analysis",
},
},
},
limit: {
type: "number",
description: "Maximum number of results to return",
minimum: 1,
maximum: 1000,
default: 100,
},
include_metadata: {
type: "boolean",
description: "Whether to include metadata in results",
default: false,
},
},
required: ["data_source"],
},
strict: true,
},
},
};
Function with String Constraints
const stringConstraintsFunction: ToolType = {
type: "function",
definition: {
schema: {
name: "create_user",
description: "Create a new user account",
parameters: {
type: "object",
properties: {
username: {
type: "string",
description: "User's username",
minLength: 3,
maxLength: 20,
},
email: {
type: "string",
description: "User's email address",
},
role: {
type: "string",
description: "User's role in the system",
enum: ["admin", "user", "moderator"],
},
},
required: ["username", "email"],
},
},
},
};
Function with Nested Objects and Arrays
const nestedStructureFunction: ToolType = {
type: "function",
definition: {
schema: {
name: "process_order",
description: "Process a customer order",
parameters: {
type: "object",
properties: {
customer: {
type: "object",
properties: {
id: {
type: "string",
description: "Customer ID",
},
contact: {
type: "object",
properties: {
email: { type: "string" },
phone: { type: "string" },
},
required: ["email"],
},
},
required: ["id"],
},
items: {
type: "array",
description: "Items in the order",
items: {
type: "object",
properties: {
product_id: { type: "string" },
quantity: {
type: "number",
minimum: 1,
},
price: {
type: "number",
minimum: 0,
},
},
required: ["product_id", "quantity", "price"],
},
minItems: 1,
},
},
required: ["customer", "items"],
},
},
},
};
Best Practices
- Descriptive Names: Use clear, descriptive function names that indicate the action being performed
- Comprehensive Descriptions: Provide detailed descriptions for both functions and parameters to help the LLM understand usage
- Appropriate Constraints: Use parameter constraints (min/max, length limits, enums) to ensure valid inputs
- Required vs Optional: Clearly mark which parameters are required vs optional
- Default Values: Provide sensible defaults for optional parameters when appropriate
- Nested Structure: Use nested objects and arrays when dealing with complex data structures