Skip to main content

Provides Python types, Pydantic models, and helpers to extend the OpenAI client for the Braintrust proxy, improving developer experience, typechecking, and editor integration.

Project description

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
        )
    )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

braintrust_proxy-0.1.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

braintrust_proxy-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file braintrust_proxy-0.1.0.tar.gz.

File metadata

  • Download URL: braintrust_proxy-0.1.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.10

File hashes

Hashes for braintrust_proxy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5e37a779cefacd2871be2578bfbbbb10024b38c015b086f3c4520066a96b16ab
MD5 0f20d97b65ad2025c54248ffdfcf9ecf
BLAKE2b-256 7cca7c7193bdc7db69dbbfa7354cdfe3be6605b822dd29110484cd1b83009075

See more details on using hashes here.

File details

Details for the file braintrust_proxy-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for braintrust_proxy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 198e0f8eb2078f28a8a1724e0257ec094079bfeb252960a13007a2c51484fc33
MD5 3ba029a601dc3c4b7608c92e6065e1ec
BLAKE2b-256 272e6a5a213922aca96816a1edbca88d6ac9cfce0986f03d2b87a74ec8a2958d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page