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:
Auto-Routing: Setting
auto_routing: true lets Gloo AI automatically select the optimal model for your tool use request. This is the recommended V2 approach. You can also specify a direct model like "model": "gloo-anthropic-claude-sonnet-4.5" if you need explicit control.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
Working Code Sample
View Complete Code
Clone or browse the complete working examples for all 6 languages (JavaScript, TypeScript, Python, PHP, Go, Java) with setup instructions.
Next Steps
Now that you understand how to use tool use for structured output with V2, consider exploring:- Completions V2 Guide - Learn about auto-routing and model selection options
- Tool Use Guide - For more advanced tool use patterns and multi-step workflows
- Completions API Reference - Full API documentation
- Custom Schemas - Adapt this pattern for your specific use cases

