Braintrust Python SDK

0.14.0 · active · verified Fri Apr 10

The Braintrust Python SDK is the official library for logging, tracing, and evaluating AI applications with Braintrust. It enables developers to instrument LLM calls, manage prompts, run evaluations, and track performance. The current version is 0.14.0, and the library is actively maintained with frequent updates. [2]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to run a simple evaluation using the `braintrust.Eval` class. It defines a basic task and a scoring function, then executes the evaluation. The `BRAINTRUST_API_KEY` environment variable is required for the results to be logged to your Braintrust dashboard. [2, 7]

import os
from braintrust import Eval

def is_equal(expected, output):
    return expected == output

# Ensure BRAINTRUST_API_KEY is set in your environment variables.
# For local execution: export BRAINTRUST_API_KEY="<YOUR_API_KEY>"
# For production: Use secrets management or similar.
api_key = os.environ.get('BRAINTRUST_API_KEY', '')

if not api_key:
    print("Warning: BRAINTRUST_API_KEY environment variable not set. Evaluation will not be logged to Braintrust.")
    # In a real scenario, you might raise an error or handle accordingly.

# The Eval function itself implicitly uses the API key from the environment
# when .run() is called or when integrated with the Braintrust CLI.
Eval(
    "Say Hi Bot",
    data=lambda: [
        {"input": "Foo", "expected": "Hi Foo"},
        {"input": "Bar", "expected": "Hello Bar"},
    ],
    task=lambda input: "Hi " + input,
    scores=[is_equal],
).run()

print("Evaluation complete. Check your Braintrust dashboard.")

view raw JSON →