Skip to main content

A client library for accessing Circuit Breaker Labs API

Project description

Circuit Breaker Labs Python Client

Python Version Ruff MyPy uv PyPI - Version

[!Note] This project was automatically generated by OpenAPI Python Client from this OpenAPI spec.

Installation

Install from PyPi directly:

uv pip install circuit-breaker-labs

Or install using a wheel/sdist distributed with each release.

Usage

First, create a client:

from circuit_breaker_labs import Client

client = Client(base_url="https://api.circuitbreakerlabs.ai/v1/")

Now build a request and use it when calling an endpoint

import os

from circuit_breaker_labs.api.evaluations import singleturn_evaluate_system_prompt_post
from circuit_breaker_labs.models import SingleTurnEvaluateSystemPromptRequest

with client as client:
    request = SingleTurnEvaluateSystemPromptRequest(
        threshold=0.5,
        variations=3,
        maximum_iteration_layers=2,
        system_prompt=os.getenv("SYSTEM_PROMPT"),
        openrouter_model_name="anthropic/claude-3.7-sonnet",
    )

    run_tests_response = singleturn_evaluate_system_prompt_post.sync(
        client=client,
        cbl_api_key=os.getenv("CBL_API_KEY"),
        body=request,
    )

Or do the same thing with an async version:

import os

from circuit_breaker_labs.api.evaluations import singleturn_evaluate_system_prompt_post
from circuit_breaker_labs.models import SingleTurnEvaluateSystemPromptRequest

async with client as client:
    request = SingleTurnEvaluateSystemPromptRequest(
        threshold=0.5,
        variations=3,
        maximum_iteration_layers=2,
        system_prompt=os.getenv("SYSTEM_PROMPT"),
        openrouter_model_name="anthropic/claude-3.7-sonnet",
    )

    run_tests_response = await singleturn_evaluate_system_prompt_post.asyncio(
        client=client,
        cbl_api_key=os.getenv("CBL_API_KEY"),
        body=request,
    )

Want to test multi-turn conversations instead? Use the multi-turn endpoint (async version also available):

import os

from circuit_breaker_labs.api.evaluations import multi_turn_evaluate_system_prompt_post
from circuit_breaker_labs.models import (
    MultiTurnEvaluateSystemPromptRequest,
    MultiTurnTestType,
    TestCasePack,
)

with client as client:
    request = MultiTurnEvaluateSystemPromptRequest(
        threshold=0.6,
        max_turns=6,
        test_types=[
            MultiTurnTestType.SEMANTIC_CHUNKS,
            MultiTurnTestType.USER_PERSONA,
        ],
        system_prompt=os.getenv("SYSTEM_PROMPT"),
        openrouter_model_name="anthropic/claude-3.7-sonnet",
        test_case_packs=[TestCasePack.SUICIDAL_IDEATION],
    )

    run_tests_response = multi_turn_evaluate_system_prompt_post.sync(
        client=client,
        cbl_api_key=os.getenv("CBL_API_KEY"),
        body=request,
    )

Things to know:

  1. Every path/method combo becomes a Python module with four functions:

    1. sync: Blocking request that returns parsed data (if successful) or None
    2. sync_detailed: Blocking request that always returns a Request, optionally with parsed set if the request was successful.
    3. asyncio: Like sync but async instead of blocking
    4. asyncio_detailed: Like sync_detailed but async instead of blocking
  2. All path/query params, and bodies become method arguments.

  3. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)

  4. Any endpoint which did not have a tag will be in circuit_breaker_labs.api.default

Advanced customizations

There are more settings on the generated Client class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying httpx.Client or httpx.AsyncClient (depending on your use-case):

from circuit_breaker_labs import Client

def log_request(request):
    print(f"Request event hook: {request.method} {request.url} - Waiting for response")

def log_response(response):
    request = response.request
    print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")

client = Client(
    base_url="https://api.circuitbreakerlabs.ai/v1/",
    httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
)

# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()

You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):

import httpx
from circuit_breaker_labs import Client

client = Client(
    base_url="https://api.circuitbreakerlabs.ai/v1/",
)
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
client.set_httpx_client(httpx.Client(base_url="https://api.circuitbreakerlabs.ai/v1/", proxies="http://localhost:8030"))

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

circuit_breaker_labs-1.0.7.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

circuit_breaker_labs-1.0.7-py3-none-any.whl (44.0 kB view details)

Uploaded Python 3

File details

Details for the file circuit_breaker_labs-1.0.7.tar.gz.

File metadata

  • Download URL: circuit_breaker_labs-1.0.7.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for circuit_breaker_labs-1.0.7.tar.gz
Algorithm Hash digest
SHA256 97b22d6a035c42082c4dece12d07dfd7adbeea730fd0229bce5df17578661cd1
MD5 608310e408c41496bf5f2b9290b3cde9
BLAKE2b-256 9e0fede4d989196eeeee06c19e7764efd81da1be420c43b56baf8df4d6f85edc

See more details on using hashes here.

Provenance

The following attestation bundles were made for circuit_breaker_labs-1.0.7.tar.gz:

Publisher: release.yml on circuitbreakerlabs/circuitbreakerlabs-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file circuit_breaker_labs-1.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for circuit_breaker_labs-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 caddcd4522ca2a2eb078e664d4c1964f2705ce2d836c03d6af60a0395c31680e
MD5 bb3a9041b136be75525e03bf96b5eb88
BLAKE2b-256 937ee35d4154505a644b4e406462c36ae31a8f42939afa3824d5dd360945d620

See more details on using hashes here.

Provenance

The following attestation bundles were made for circuit_breaker_labs-1.0.7-py3-none-any.whl:

Publisher: release.yml on circuitbreakerlabs/circuitbreakerlabs-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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