- Define tools using JSON Schema (name, description, parameters)
- Model analyzes the query and decides whether to call a tool
- If needed, model returns structured tool calls with parameters
- You execute the tool and send results back for the final response
Quick example
Define tools and send a request - the model will return structured tool calls when needed:For best results with tool calling, use a low temperature (0.0-0.3) to reduce hallucinated parameter values and ensure more deterministic tool selection.
Complete workflow: Execute tools and get final response
Complete workflow: Execute tools and get final response
Defining tools
Tools are defined using JSON Schema format. Each tool requires:- name: Function identifier (a-z, A-Z, 0-9, underscores, dashes; max 64 characters)
- description: Clear explanation of what the function does (used by the model to decide when to call it)
- parameters: JSON Schema object describing the function’s parameters
Write detailed descriptions and parameter definitions. The model relies on these to select the correct tool and provide appropriate arguments.
Parameter types
JSON Schema supports:string, number, integer, object, array, boolean, and null. You can also:
- Use
enumto restrict values to specific options - Mark parameters as
requiredor optional - Provide descriptions for each parameter
Example: Defining multiple tools
Example: Defining multiple tools
Additional configurations
tool_choice
Thetool_choice parameter controls how the model uses tools:
auto(default): Model decides whether to call a tool or respond directlynone: Model will not call any toolsrequired: Model must call at least one tool- Specific function: Force the model to call a particular function
Some models support parallel tool calling, where multiple tools can be called in a single response. Check the model’s capabilities before relying on this feature.
Streaming
Using tool calls with streaming responses
Using tool calls with streaming responses
Tool calls work with streaming responses. Arguments are sent incrementally as the model generates them:
Troubleshooting
Model isn't calling tools when expected
Model isn't calling tools when expected
- Check that your tool descriptions are clear and detailed
- Ensure the user query clearly indicates a need for the tool
- Try using
tool_choice="required"to force tool usage - Verify your model supports tool calling (check
supportsToolsfield)
Tool arguments are incorrect or malformed
Tool arguments are incorrect or malformed
- Add more detailed parameter descriptions
- Use lower temperature (0.0-0.3) for more deterministic outputs
- Provide examples in parameter descriptions
- Use
enumto constrain values to specific options
Getting JSON parsing errors
Getting JSON parsing errors
- Always validate tool call arguments before parsing
- Handle partial or malformed JSON gracefully in production
- Use try-catch blocks when parsing
tool_call.function.arguments