Braintrust API Python Client

0.6.0 · active · verified Thu Apr 16

The `braintrust-api` library is the official Python client for interacting with the Braintrust REST API. It provides convenient access to log, trace, and evaluate AI applications. The library includes type definitions for all request parameters and response fields, supporting both synchronous and asynchronous clients. It is actively maintained with frequent updates, with the current version being 0.6.0.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Braintrust API client using an API key from an environment variable and creates a new project. This demonstrates basic client initialization and a synchronous API call.

import os
from braintrust_api import Braintrust

# Ensure BRAINTRUST_API_KEY is set in your environment variables
# e.g., export BRAINTRUST_API_KEY="bt_your_api_key_here"
api_key = os.environ.get("BRAINTRUST_API_KEY")

if not api_key:
    raise ValueError("BRAINTRUST_API_KEY environment variable not set.")

client = Braintrust(api_key=api_key)

try:
    # Create a new project
    project = client.projects.create(name="My New Project")
    print(f"Created project with ID: {project.id} and Name: {project.name}")

    # Optionally, list projects
    projects_list = client.projects.list(limit=1)
    for p in projects_list.objects:
        print(f"Existing project: {p.name} ({p.id})")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →