#!/usr/bin/env python3 """Probe gpt-5.4-mini's reasoning_effort / stream_options / tool-call contract. Needed to translate Ollama's `think` flag and streaming shape onto the cloud fallback. Reads the key from the environment and never prints it. """ import json import os import sys import urllib.error import urllib.request KEY = os.environ.get("OPENAI_API_KEY", "") MODEL = os.environ.get("FORGEWAY_CLOUD_MODEL", "gpt-5.4-mini") if not KEY: sys.exit("OPENAI_API_KEY not present in the environment") BASE = {"model": MODEL, "messages": [{"role": "user", "content": "say PONG"}], "max_completion_tokens": 64} CASES = { "reasoning_effort=none": {"reasoning_effort": "none"}, "reasoning_effort=low": {"reasoning_effort": "low"}, "reasoning_effort=medium": {"reasoning_effort": "medium"}, "reasoning_effort=high": {"reasoning_effort": "high"}, "stream+include_usage": {"stream": True, "stream_options": {"include_usage": True}}, "response_format=json_object": {"response_format": {"type": "json_object"}, "messages": [{"role": "user", "content": "Return JSON {\"a\":1}"}]}, "tools": {"tools": [{"type": "function", "function": {"name": "get_weather", "description": "Weather for a city", "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}}], "messages": [{"role": "user", "content": "What is the weather in Berlin? Use the tool."}]}, } for label, extra in CASES.items(): body = {**BASE, **extra} req = urllib.request.Request( "https://api.openai.com/v1/chat/completions", data=json.dumps(body).encode(), headers={"Content-Type": "application/json", "Authorization": f"Bearer {KEY}"}, method="POST", ) try: with urllib.request.urlopen(req, timeout=90) as resp: raw = resp.read().decode() if body.get("stream"): lines = [l for l in raw.splitlines() if l.startswith("data: ")] last = json.loads(lines[-2][6:]) if len(lines) >= 2 and lines[-1].strip() == "data: [DONE]" else json.loads(lines[-1][6:]) print(f"{label:30s} -> {resp.status} sse_lines={len(lines)} last_has_usage={'usage' in last and last['usage'] is not None} last_choices={len(last.get('choices', []))}") else: d = json.loads(raw) msg = d["choices"][0]["message"] print(f"{label:30s} -> {resp.status} finish={d['choices'][0]['finish_reason']} content={msg.get('content')!r:.40} tool_calls={json.dumps(msg.get('tool_calls'))[:120] if msg.get('tool_calls') else None}") except urllib.error.HTTPError as exc: detail = exc.read().decode("utf-8", "replace") try: detail = json.loads(detail)["error"]["message"] except Exception: detail = detail[:160] print(f"{label:30s} -> {exc.code} {detail}")