Test OpenAI Tool Calls
A promptfoo example for testing OpenAI tool-calling accuracy against the Chat Completions API.
Why it matters
Evaluate and test the effectiveness of OpenAI's tool-calling capabilities within your development workflow. Ensure your API integrations are robust and function as expected.
Outcomes
What it gets done
Define and configure tool usage for the Chat Completions API.
Test and validate the output of tool calls.
Integrate tool-calling logic into your codebase.
Debug issues related to tool execution.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/pfoo-openai-tools-call | bash Steps
Steps in the chain
Overview
Openai Tools Call
A promptfoo example testing OpenAI's tool-calling accuracy through the Chat Completions API, validating tool-call structure and argument correctness across a range of cities. Use it to validate tool-call structure and argument correctness for a tool-using OpenAI model. Requires an OpenAI API key.
What it does
This promptfoo example evaluates OpenAI's tools-calling capability through the Chat Completions API. It defines a custom weather-lookup tool and tests whether the model correctly calls that function when asked about weather, passes the right location parameter for the city mentioned, handles a variety of cities including international ones, and formats its responses consistently.
When to use - and when NOT to
Use this example when you need to validate that a model reliably calls the right tool with the right arguments across varied inputs, rather than just checking the final text response. It's scoped to the Chat Completions tools API specifically - if you need to validate output transformation and JavaScript-based assertion logic more broadly, the same techniques shown here apply, but the example itself is built around one weather tool.
Inputs and outputs
npx promptfoo@latest init --example openai-tools-call
cd openai-tools-call
npx promptfoo eval
npx promptfoo view
The is-valid-openai-tools-call assertion validates that the returned tool call is structurally correct, while custom JavaScript assertions can isolate and check specific parts of the response, such as the parsed location argument, for more detailed validation than a structural check alone provides.
Integrations
Requires OPENAI_API_KEY, set via a .env file or the environment. Full detail on tool definitions and provider configuration is in OpenAI's function-calling guide and promptfoo's OpenAI provider documentation for tools and functions.
Who it's for
Developers building tool-using OpenAI agents who need a working reference for asserting on tool-call structure and argument correctness across a range of inputs before trusting the behavior in production.
Source README
openai-tools-call (OpenAI Tools Call Example)
This example demonstrates how to use promptfoo to evaluate OpenAI's tools calling capabilities. It shows how to define and test tool usage with the Chat Completions API.
Features Demonstrated
- Defining tools for AI models to use
- Testing tool call outputs
- Validating AI-generated function arguments
- Transforming outputs for assertions
Environment Variables
This example requires the following environment variables:
OPENAI_API_KEY- Your OpenAI API key
You can set this in a .env file or directly in your environment.
Running the Example
You can run this example with:
npx promptfoo@latest init --example openai-tools-call
### and then
cd openai-tools-call
### Run the evaluation
npx promptfoo eval
### View the results
npx promptfoo view
What This Example Does
The configuration defines a custom tool for getting weather information. It then tests the model's ability to:
- Correctly call the weather function when asked about weather
- Pass the correct location parameter based on the city mentioned
- Handle various cities, including international ones
- Format responses consistently
Key Features
- Uses
is-valid-openai-tools-callassertion to validate the function call structure - Demonstrates output transformation to isolate and test specific parts of the response
- Shows how to use JavaScript assertions for detailed validation
- Tests with a variety of locations to ensure robust behavior
Documentation
For more details, see:
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.