Prompt Chain

Integrate Python with OpenAI for Promptfoo

Promptfoo example for writing a custom Python provider that calls OpenAI, with config loaded from external files.

Works with openai

74
Spark score
out of 100
Updated 7 days ago
Version 0.121.20
Models

Add to Favorites

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

01

Develop custom Python providers for promptfoo.

02

Integrate promptfoo with the OpenAI API.

03

Automate prompt testing and evaluation using Python scripts.

04

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

01
Set up environment variables
02
Install Python dependencies
03
Initialize the example project
04
Review provider implementation
05
Configure file references
06
Run the evaluation

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:

  1. Call APIs from Python libraries
  2. Implement custom logic before or after calling LLMs
  3. Process responses in specific ways
  4. 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 API
  • promptfooconfig.yaml - Configuration for promptfoo evaluation with proper YAML schema reference
  • configs/ directory:
    • fileConfig.yaml - YAML configuration for model settings
    • fileConfig.js - JavaScript configuration for formatting options
    • fileConfig.py - Python configuration for additional parameters

Implementation Details

The Python provider is defined in provider.py and includes:

  1. A call_api function that makes API calls to OpenAI
  2. Token usage extraction from the API response
  3. 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:

  1. The prompts being submitted to your Python provider
  2. Responses from the OpenAI API
  3. Token usage statistics for each completion
  4. 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:

  1. YAML file (configs/fileConfig.yaml): Contains model settings like temperature and max tokens
  2. JavaScript file (configs/fileConfig.js): Provides formatting options through a function export
  3. 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.