Skip to content

Docs / Python

BrokenGPT Python API quickstart

Send your first BrokenGPT chat completion from Python, keep the API key server-side, stream output, and handle production errors safely.

UPDATED 01 Aug 20267 MIN READTECHNICAL GUIDE
01

Install a client and provide the key through the environment.

  1. Create an API key in the BrokenGPT dashboard and copy it once.
  2. Install a current OpenAI-compatible Python client with pip install openai.
  3. Set BROKENGPT_API_KEY in the server process or secret manager, never in committed code.

ENVIRONMENT

# PowerShell
$env:BROKENGPT_API_KEY="your-key"

# bash / zsh
export BROKENGPT_API_KEY="your-key"
02

Send one non-streamed request first.

The base URL ends at /v1 and the public model alias is broken-one. A non-streamed call is the easiest way to validate authentication, request fields, and response parsing.

PYTHON

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["BROKENGPT_API_KEY"],
    base_url="https://brokengpt.com/v1",
)

response = client.chat.completions.create(
    model="broken-one",
    messages=[{"role": "user", "content": "Explain SSE in three sentences."}],
)

print(response.choices[0].message.content)
03

Stream text only after the basic call succeeds.

The chat-completions route streams server-sent events. Your client should tolerate empty deltas, inspect finish reasons, stop cleanly on completion, and handle a connection that closes before the terminal event.

PYTHON STREAM

stream = client.chat.completions.create(
    model="broken-one",
    messages=[{"role": "user", "content": "Write a short launch checklist."}],
    stream=True,
)

for chunk in stream:
    text = chunk.choices[0].delta.content if chunk.choices else None
    if text:
        print(text, end="", flush=True)
04

Add bounded retries, timeouts, and application validation.

  • Do not retry malformed requests or invalid credentials.
  • Retry 429 and temporary 5xx responses with exponential backoff, jitter, and a maximum attempt count.
  • Log the returned request ID without logging the API key or sensitive prompt content.
  • Validate tool arguments and structured output before using them.
  • Set application timeouts and propagate cancellation when the caller disconnects.

STRAIGHT ANSWERS

Frequently asked questions

01Which model name should Python send?

Use the stable public alias broken-one unless the live BrokenGPT docs explicitly change the contract.

02Can I put the key in a notebook?

Use an environment variable or notebook secret store. Do not save the secret in the notebook file or output.

03Why start without streaming?

A normal JSON response makes authentication, validation, and parsing failures easier to isolate before adding SSE state.

BUILD WITH THE LIVE CONTRACT

Test one real request end to end.

Create a scoped key, send a representative prompt, and verify output, usage, limits, and error handling before expanding the integration.

Create an account