Python-Guide

OpenAI Decisions API in Python

OpenAI hat kein SDK für seine Decisions API veröffentlicht — es gibt kein client.decisions.create zum Kopieren. Diese Seite zeigt funktionierendes Python für den Endpoint dieser Seite — eine aufrufbare OpenAI-Decisions-API-Alternative, die decisions-1 liefert und demselben eingeschränkten Entscheidungsmuster folgt.

Aktualisiert

Den Call ausführen

Ein POST an /api/v1/decisions mit einem Bearer-Schlüssel. Der Body besteht aus model, state und questions — 1 bis 6 Fragen, jeweils vom Typ noul, choice oder score. Setze DECISIONS_API_KEY auf einen Schlüssel aus dem Dashboard.

Nutze requests (oder httpx für async — die Call-Form ist identisch). Setze bei jedem Call ein Timeout; eine hängende Entscheidung soll schnell fehlschlagen, nicht die Pipeline blockieren.

Python

import os
import requests

res = requests.post(
    "https://decisions-api.net/api/v1/decisions",
    headers={"Authorization": f"Bearer {os.environ['DECISIONS_API_KEY']}"},
    json={
        "model": "decisions-1",
        "state": "I was charged twice for my subscription this morning.",
        "questions": {
            "refund": {
                "type": "noul",
                "instructions": "Is the customer asking for money back?",
            }
        },
    },
    timeout=30,
)
res.raise_for_status()
answers = res.json()["answers"]
print(answers["refund"]["noul"])

Die Wahrscheinlichkeiten lesen

answers kommt unter deinen Frage-IDs zurück. Eine noul-Antwort ist die Wahrscheinlichkeit, dass die Aussage wahr ist. Eine choice-Antwort enthält den siegenden choice, eine Wahrscheinlichkeit pro Option und einen Konfidenzwert — nutze die Konfidenz, nicht nur den Sieger, um über automatisches Handeln zu entscheiden.

Python

answers = res.json()["answers"]

# noul: probability the statement is true
if answers["refund"]["noul"] >= 0.8:
    route_to_refunds()

# choice: winning label + per-option probabilities + confidence
team = answers["team"]
print(team["choice"], team["probabilities"], team["confidence"])

Timeouts und Retries

429 und 502 lohnen einen kurzen Backoff-Retry — fehlgeschlagene Calls werden nicht berechnet. 402 heißt Guthaben leer: aufladen, nicht erneut versuchen. 422 ist ein Validierungsfehler; die Meldung nennt das Feld, also korrigiere den Body statt zu wiederholen.

Python

import time
import requests

def decide(body, attempts=3):
    for i in range(attempts):
        try:
            res = requests.post(
                "https://decisions-api.net/api/v1/decisions",
                headers={"Authorization": f"Bearer {os.environ['DECISIONS_API_KEY']}"},
                json=body,
                timeout=30,
            )
            if res.status_code in (429, 502):
                time.sleep(2 ** i)
                continue
            if res.status_code == 402:
                raise RuntimeError("out of credits")
            res.raise_for_status()
            return res.json()["answers"]
        except requests.Timeout:
            time.sleep(2 ** i)
    raise RuntimeError("decision call failed")

Den Call in einen Helper kapseln

Aufrufer sollten eine Funktion sehen, die Text nimmt und ein Label zurückgibt — keine HTTP-Details. Timeout, 402-Check und Retry-Schleife einmal in decide() legen, und jede Aufrufstelle bleibt sauber.

Python

# Keep the call behind one function — timeouts, 402s,
# and retries live inside it, not at every call site.
def route_ticket(text: str) -> str:
    answers = decide({
        "model": "decisions-1",
        "state": text,
        "questions": {
            "team": {
                "type": "choice",
                "instructions": "Which team should own this ticket?",
                "criteria": {
                    "payments": "Checkout or billing.",
                    "frontend": "Rendering or browser behavior.",
                    "account": "Login or permissions.",
                },
            }
        },
    })
    team = answers["team"]
    return team["choice"] if team["confidence"] >= 0.7 else "triage"

FAQ

Gibt es ein offizielles OpenAI-SDK-Beispiel für die Decisions API?

Nein. OpenAI hat weder SDK-Methoden noch ein Request-Schema für seine Decisions API veröffentlicht — alles mit client.decisions.create ist erfunden. Diese Seite nutzt schlichtes HTTP.

Kann ich httpx statt requests verwenden?

Ja — der Endpoint ist ein schlichter HTTPS-POST. Nutze httpx.AsyncClient mit denselben Headern, Body, Timeout und Statusbehandlung.

Wie sende ich strukturierten Kontext?

state akzeptiert ein JSON-Objekt oder -Array, nicht nur einen String — übergib dicts direkt im json-Body; das Modell liest sie als Kontext.

Eine Entscheidung im Browser ausführen

Ganz ohne Setup — führe einen echten Call im Playground mit 1 kostenlosem Credit für neue Besucher aus.