Prerequisites
Before starting, ensure you have:- A Gloo AI Studio account
- Your Client ID and Client Secret from the API Credentials page
- Authentication setup - Complete the Authentication Tutorial first
The Completions API with tool use requires Bearer token authentication. If you haven’t set up authentication yet, follow the Authentication Tutorial to learn how to exchange your credentials for access tokens and manage token expiration.
Step 1: Define Your Tool Schema
The first step is to define a function schema that describes the structured output you want. This tells the AI exactly what format to return:Step 2: Make the API Call
Now make the API request with the tool definition and settool_choice: "required"
to force the AI to use your tool:
Example Response
The API will return structured data in thetool_calls
array:
arguments
field contains a JSON string that matches your tool schema perfectly!
Complete Examples
The following examples combine token retrieval, expiration checking, making the API call with tool use, and parsing the structured response into a complete, runnable script for each language. You’ll want to first set up your environment variables in either an.env
file:
Testing Your Implementation
To test any of the complete examples:- Set up your environment variables with your actual Gloo AI credentials
- Install dependencies according to each language’s requirements
- Run the script and observe the structured output
- Automatically handle token retrieval and expiration
- Make a tool-use API call with the goal “I want to grow in my faith”
- Parse the JSON response into a structured format
- Display the growth plan in a user-friendly format
- Show the raw JSON for developers
Key Benefits
This approach provides several advantages over simple chat completions:- Predictable Structure: The response always follows your defined schema
- Machine-Readable: Easy to parse and use in applications
- Type Safety: Clear data types for each field
- Validation: The API enforces your schema requirements
- Flexibility: Can adapt to any structured output needs
Next Steps
Now that you understand how to use tool use for structured output, consider exploring:- Tool Use Guide - For more advanced tool use patterns
- Completions API - For additional API parameters
- Chat Tutorial - For integrating with stateful conversations
- Custom Schemas - Adapt this pattern for your specific use cases