Concepts
LLM Tool Calling Function Definitions
Tool calling allows language models to use external systems, APIs, and services. The foundation of this lies in JSON Schema definitions. They describe each available tool's interface, parameters, and expected behaviour.
What is a Function Definition?
A function definition is a structured specification that tells the LLM:
A description of what the function does
The parameters it accepts
What parameters are required and what are optional
The expected data types for each parameter
Valid value constraints and formats
JSON Schema Structure
Function definitions use JSON Schema formats with these key components:
Basic Structure
Core Fields
name
(string, required)The function identifier that the LLM will use to call this tool.
Should be descriptive and follow snake_case or camelCase naming conventions.
description
(string, required)This helps the LLM understand when to use this tool.
Should be a clear explanation of what the function does.
It's important it is concise but informative.
parameters
(object, required)Defines the function's input schema using JSON Schema.
Always has
"type": "object"
at the root level.
Parameter Types and Constraints
String Parameters
Number Parameters
Boolean Parameters
Array Parameters
Object Parameters
Enum Parameters
Complete Example
Best Practices
Clear Descriptions
Write the descriptions from the LLM's perspective.
Explain when and why to use the function.
Include information about the expected outcomes.
Parameter Naming
Use descriptive, self-explanatory names.
Follow a consistent naming convention.
Avoid abbreviations unless universally understood.
Validation and Constraints
Set appropriate minimum and maximum values.
Use enums for fixed sets of options.
Include format specifications for dates, emails, URLs.
Set realistic string length limits.
Required vs Optional
Mark any essential parameters as required.
Provide sensible defaults for optional parameters.
Consider the minimum viable function call.
Error Prevention
Use
additionalProperties: false
to prevent unexpected parameters.Include format validations where applicable.
Set realistic bounds on numeric values.
Advanced Features
Conditional Parameters
Use oneOf
, anyOf
, or allOf
for complex parameter relationships:
Dynamic Validation
Testing Your Definitions
When creating function definitions:
Validate JSON Schema - Ensure your schema is syntactically correct.
Test Edge Cases - Consider boundary values and invalid inputs.
Verify Descriptions - Make sure they clearly communicate intent.
Check Required Fields - Ensure minimum viable calls work.
Test with LLM - Verify the model understands when to use your function.
Troubleshooting
Well-crafted function definitions are the key to successful LLM tool integration. This enables models to understand not just what tools are available, but exactly how and when to use them effectively.
LLM doesn't call your function
Check the description clearly indicates when to use it.
Ensure required parameters aren't too restrictive.
Verify the function name is intuitive.
Parameter errors
Review your type definitions and constraints.
Check for typos in parameter names.
Ensure required parameters are actually needed.
Function called incorrectly
Add more specific descriptions.
Use enums to limit parameter values.
Include examples in descriptions where helpful.
Last updated