Files
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00
..

config-extension-api (Custom Extensions Example for promptfoo)

You can run this example with:

npx promptfoo@latest init --example config-extension-api
cd config-extension-api

This example demonstrates how to leverage promptfoo's powerful extensions API to implement custom setup and teardown hooks for individual tests. These extensions can be defined in either Python or JavaScript, providing flexibility for your preferred programming environment.

Extension Hooks Overview

promptfoo supports four types of extension hooks, each triggered at a specific point in the evaluation lifecycle:

  1. beforeAll: Executed once before the entire test suite begins
  2. afterAll: Executed once after the entire test suite completes
  3. beforeEach: Executed before each individual test
  4. afterEach: Executed after each individual test

Each hook receives a hookName parameter and a context object containing relevant data for that specific hook type.

The beforeAll and beforeEach hooks should return the context object, while the afterAll and afterEach hooks should not return anything.

For comprehensive information on implementing and using these hooks, refer to the Extension Hooks section in the official documentation.

Configuring Extensions

Specify your extensions in the promptfooconfig.yaml file:

extensions:
  - file://path/to/your/extension.py:extension_hook
  - file://path/to/your/extension.js:extensionHook

Note: You must include the function name after the file path, separated by a colon (:).

Why Use Extensions?

Extensions in promptfoo empower you to:

  1. Enhance test functionality with custom pre- and post-test actions
  2. Dynamically prepare and clean up test environments
  3. Seamlessly integrate with external systems or services for comprehensive testing
  4. Implement custom logging or reporting mechanisms

These extensions function similarly to "before" and "after" hooks in popular unit testing frameworks, allowing you to set up and tear down test environments with precision and control.

Practical Use Cases for Extensions

  1. Environment Setup/Teardown: Dynamically create and remove files or resources for each test.
  2. AI Agent Testing: Configure complex scenarios, such as deploying Kubernetes manifests before evaluation.
  3. Model Loading: Efficiently load ML models into memory before tests and unload them afterward.
  4. Local Server Management: Programmatically start and stop local language model servers (e.g., llama.cpp, ollama) for testing.
  5. Connection Management: Automate the opening and closing of connection tunnels or database connections.
  6. Credential Handling: Securely load and manage credentials or authentication tokens for testing environments.

Running the Example

To execute this example, use the following command in your terminal:

LOG_LEVEL=debug promptfoo eval

Then

promptfoo view