Integrate Python with OpenAI for Promptfoo
Promptfoo example for writing a custom Python provider that calls OpenAI, with config loaded from external files.
Why it matters
Leverage the power of Python to create custom providers for promptfoo, enabling seamless integration with the OpenAI API for advanced prompt engineering and testing.
Outcomes
What it gets done
Develop custom Python providers for promptfoo.
Integrate promptfoo with the OpenAI API.
Automate prompt testing and evaluation using Python scripts.
Enhance code generation and review processes with AI-powered prompts.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/pfoo-provider-python | bash Steps
Steps in the chain
Overview
Provider Python
A promptfoo example for building a custom Python provider that calls OpenAI, tracks token usage, and loads provider config from external YAML, JavaScript, and Python files via the file:// protocol. Use when you need custom Python logic around an LLM call or want provider config split into reusable external files. Skip it if a built-in provider already fits without custom code.
What it does
This promptfoo example shows how to write a custom Python provider that calls the OpenAI API. It's the pattern to reach for when you need to call APIs from Python libraries, implement custom logic before or after calling the LLM, process responses in a specific way, or track token usage and other metrics yourself - the four use cases the example is built around. The provider.py implementation includes a call_api function, token-usage extraction from the API response, and several sample functions showing different ways to call the API; it defaults to gpt-4.1-mini but can be pointed at other models.
npx promptfoo@latest init --example provider-python
cd provider-python
It also demonstrates loading configuration values from external files with the file:// protocol directly inside promptfooconfig.yaml: a YAML file (configs/fileConfig.yaml) for model settings like temperature and max tokens, a JavaScript file (configs/fileConfig.js) that exports a formatting-options function, and a Python file (configs/fileConfig.py) that supplies additional parameters through a Python function. The loader also accepts JSON, .yaml/.yml, .js/.mjs/.ts/.cjs, .py, and .txt/.md files.
When to use - and when NOT to
Use it when you need Python-side logic wrapped around an LLM call - a Python library dependency, custom pre/post-processing, or non-standard token tracking - or when you want provider settings, formatting, and parameters split across separate reusable files instead of one large YAML block. Skip it if a built-in provider already covers your case without custom code.
Inputs and outputs
Input is an OPENAI_API_KEY (env var or .env) and Python with the openai package installed (pip install openai). Key files: provider.py (the provider implementation), promptfooconfig.yaml (the eval config, with a proper YAML schema reference), and a configs/ directory holding the YAML/JS/Python file-config examples. Run it with npx promptfoo@latest evaluate -c examples/provider-python/promptfooconfig.yaml. When it runs, you'll see each prompt as it's submitted to the Python provider, the corresponding OpenAI response, per-completion token-usage statistics, and the overall evaluation results laid out in a table.
Integrations
Calls OpenAI's API from Python through the openai package; provider config values can be pulled from external YAML, JavaScript, or Python files via the file:// protocol instead of being written inline, with each file type resolved through its own loader.
Who it's for
Developers who need custom Python logic between promptfoo and an LLM provider - extra API calls, custom pre/post-processing, non-standard token tracking - or who want provider configuration split into separate, reusable files rather than one large YAML block.
Source README
provider-python (Python Provider)
This example demonstrates how to create a custom Python provider for promptfoo that integrates with the OpenAI API.
You can run this example with:
npx promptfoo@latest init --example provider-python
cd provider-python
Overview
The Python provider allows you to use Python code as a provider in promptfoo evaluations. This is useful when you need to:
- Call APIs from Python libraries
- Implement custom logic before or after calling LLMs
- Process responses in specific ways
- Track token usage and other metrics
Environment Variables
This example requires the following environment variable:
OPENAI_API_KEY- Your OpenAI API key
You can set this in a .env file or directly in your environment.
Requirements
- Python with the OpenAI package installed (
pip install openai)
Files
provider.py- The Python provider implementation that calls OpenAI's APIpromptfooconfig.yaml- Configuration for promptfoo evaluation with proper YAML schema referenceconfigs/directory:fileConfig.yaml- YAML configuration for model settingsfileConfig.js- JavaScript configuration for formatting optionsfileConfig.py- Python configuration for additional parameters
Implementation Details
The Python provider is defined in provider.py and includes:
- A
call_apifunction that makes API calls to OpenAI - Token usage extraction from the API response
- Multiple sample functions showing different ways to call the API
By default, the example is configured to use gpt-4.1-mini model, but you can modify it to use other models as needed.
Expected Output
When you run this example, you'll see:
- The prompts being submitted to your Python provider
- Responses from the OpenAI API
- Token usage statistics for each completion
- Evaluation results in a table format
File Reference Configuration
The example demonstrates how to load configuration values from external files using the file:// protocol directly in the promptfooconfig.yaml file. It shows three main file types:
- YAML file (
configs/fileConfig.yaml): Contains model settings like temperature and max tokens - JavaScript file (
configs/fileConfig.js): Provides formatting options through a function export - Python file (
configs/fileConfig.py): Supplies additional parameters through a Python function
The provider supports loading from:
- JSON files (
.json) - YAML files (
.yaml,.yml) - JavaScript files (
.js,.mjs,.ts,.cjs) - Python files (
.py) - Text files (
.txt,.md)
You can see how this works in the promptfooconfig.yaml file:
providers:
- id: 'file://provider.py:call_api'
config:
# YAML
settings: 'file://configs/fileConfig.yaml'
# JavaScript file
formatting: 'file://configs/fileConfig.js:getFormatConfig'
nested: # Python file
parameters: 'file://configs/fileConfig.py:get_params'
Run the example with:
npx promptfoo@latest evaluate -c examples/provider-python/promptfooconfig.yaml
Learn More
For more information on creating custom providers, see the promptfoo documentation.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.