Skip to main content

Braintrust AI Proxy Python Client

The braintrust-proxy package provides Python types, Pydantic models, and helper functions to simplify interaction with the Braintrust AI proxy. By providing rich Python types that extend the OpenAI client types, it significantly enhances compatibility and developer experience, enabling features like typechecking and improved editor autocomplete when working with proxy-specific extensions (such as reasoning).

Installation

You can install the package using uv (recommended) or pip:

Using uv:

uv pip install braintrust-proxy

Using pip:

pip install braintrust-proxy

Usage

The package provides enhanced types and helper functions, such as as_openai_chat_message_param, to work with messages that might include proxy-specific extensions (like reasoning) while maintaining compatibility with standard OpenAI client inputs.

import os
from openai import OpenAI
import json
from braintrust_proxy import as_openai_chat_message_param

# Initialize the OpenAI client pointing to the Braintrust proxy
# Ensure BRAINTRUST_API_URL and BRAINTRUST_API_KEY environment variables are set
client = OpenAI(
    # if self hosting, otherwise use the hosted API
    base_url=f"{os.getenv('BRAINTRUST_API_URL') or 'https://api.braintrust.dev'}/v1/proxy",
    api_key=os.getenv("BRAINTRUST_API_KEY"),
)

# Example using the helper function with a message that includes 'reasoning'
response = client.chat.completions.create(
    model="claude-3-7-sonnet-latest",
    reasoning_effort=None,
    stream=False,
    messages=[
        {
            "role": "user",
            "content": "How many rs in 'ferrocarril'",
        },
        as_openai_chat_message_param(
            {
                "role": "assistant",
                "content": "There are 4 letter 'r's in the word \"ferrocarril\".",
                "reasoning": [
                    {
                        "id": "",
                        "content": "To count the number of 'r's in the word 'ferrocarril', I'll just go through the word letter by letter.\n\n'ferrocarril' has the following letters:\nf-e-r-r-o-c-a-r-r-i-l\n\nLooking at each letter:\n- 'f': not an 'r'\n- 'e': not an 'r'\n- 'r': This is an 'r', so that's 1.\n- 'r': This is an 'r', so that's 2.\n- 'o': not an 'r'\n- 'c': not an 'r'\n- 'a': not an 'r'\n- 'r': This is an 'r', so that's 3.\n- 'r': This is an 'r', so that's 4.\n- 'i': not an 'r'\n- 'l': not an 'r'\n\nSo there are 4 'r's in the word 'ferrocarril'.",
                    },
                ],
            }
        ),
        {
            "role": "user",
            "content": "How many e in what you said?",
        },
    ],
)

print(
    json.dumps(
        {
            "message": response.choices[0].message.dict(),
            "reasoning": getattr(response.choices[0].message, "reasoning", None),
        },
        indent=2,
    )
)

Streaming

import os
from openai import OpenAI
import json
from braintrust_proxy import as_openai_chat_message_param, from_openai_chat_completion_choice_delta

# Initialize the OpenAI client pointing to the Braintrust proxy
# Ensure BRAINTRUST_API_URL and BRAINTRUST_API_KEY environment variables are set
client = OpenAI(
    # if self hosting, otherwise use the hosted API
    base_url=f"{os.getenv('BRAINTRUST_API_URL') or 'https://api.braintrust.dev'}/v1/proxy",
    api_key=os.getenv("BRAINTRUST_API_KEY"),
)

stream = client.chat.completions.create(
    model="claude-3-7-sonnet-latest",
    reasoning_effort="high",
    stream=True,
    messages=[
        {
            "role": "user",
            "content": "How many rs in 'ferrocarril'",
        },
        as_openai_chat_message_param(
            {
                "role": "assistant",
                "content": "There are 4 letter 'r's in the word \"ferrocarril\".",
                "reasoning": [
                    {
                        "id": "",
                        "content": "To count the number of 'r's in the word 'ferrocarril', I'll just go through the word letter by letter.\n\n'ferrocarril' has the following letters:\nf-e-r-r-o-c-a-r-r-i-l\n\nLooking at each letter:\n- 'f': not an 'r'\n- 'e': not an 'r'\n- 'r': This is an 'r', so that's 1.\n- 'r': This is an 'r', so that's 2.\n- 'o': not an 'r'\n- 'c': not an 'r'\n- 'a': not an 'r'\n- 'r': This is an 'r', so that's 3.\n- 'r': This is an 'r', so that's 4.\n- 'i': not an 'r'\n- 'l': not an 'r'\n\nSo there are 4 'r's in the word 'ferrocarril'.",
                    },
                ],
            }
        ),
        {
            "role": "user",
            "content": "How many e in what you said?",
        },
    ],
)

for event in stream:
    delta = from_openai_chat_completion_choice_delta(event.choices[0].delta)
    print(
        json.dumps(
            {"content": delta.content, "reasoning": delta.reasoning.dict() if delta.reasoning else None}, indent=2
        )
    )

Metadata

Release files for braintrust-proxy 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for braintrust-proxy 0.1.0
File Size Uploaded
braintrust_proxy-0.1.0.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for braintrust-proxy 0.1.0
File Interpreter ABI Platform
braintrust_proxy-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.8 kB

Release files / braintrust_proxy-0.1.0.tar.gz

Download URL braintrust_proxy-0.1.0.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5e37a779cefacd2871be2578bfbbbb10024b38c015b086f3c4520066a96b16ab
BLAKE2b-256 checksum
How to use checksums
7cca7c7193bdc7db69dbbfa7354cdfe3be6605b822dd29110484cd1b83009075
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release files / braintrust_proxy-0.1.0-py3-none-any.whl

Download URL braintrust_proxy-0.1.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
198e0f8eb2078f28a8a1724e0257ec094079bfeb252960a13007a2c51484fc33
BLAKE2b-256 checksum
How to use checksums
272e6a5a213922aca96816a1edbca88d6ac9cfce0986f03d2b87a74ec8a2958d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page