import json
import os
import sys
import httpx
from openai import APIError, APIStatusError, OpenAI
# No automatic retries: retrying an interrupted generation can create a new charge.
with OpenAI(
api_key=os.environ["NANOGPT_API_KEY"],
base_url="https://api.nano-gpt.com/api/v1",
timeout=60.0,
max_retries=0,
) as client:
try:
with client.chat.completions.with_streaming_response.create(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "Say hello in one sentence."}],
stream=True,
stream_options={"include_usage": True},
) as response:
print("Request ID:", response.headers.get("x-request-id", "not supplied"))
tools = {}
seen_choices, finished_choices = set(), set()
for chunk in response.parse():
if chunk.usage:
print("Usage:", chunk.usage.model_dump(), file=sys.stderr)
# Usage-only frames can have no choices; never assume choices[0].
for choice in chunk.choices or []:
seen_choices.add(choice.index)
if choice.finish_reason == "error":
raise RuntimeError("Generation failed in-stream; save the request ID before retrying.")
if choice.finish_reason is not None:
finished_choices.add(choice.index)
delta = choice.delta
if delta is None:
continue
reasoning = getattr(delta, "reasoning_content", None) or getattr(delta, "reasoning", None)
if reasoning:
print(reasoning, end="", file=sys.stderr, flush=True)
if delta.content:
print(delta.content, end="", flush=True)
for part in delta.tool_calls or []:
key = (choice.index, part.index)
tool = tools.setdefault(key, {"id": "", "name": "", "arguments": ""})
if part.id:
tool["id"] = part.id
if part.function:
tool["name"] += part.function.name or ""
tool["arguments"] += part.function.arguments or ""
if not seen_choices or seen_choices != finished_choices:
raise RuntimeError("Stream ended before completion; save the request ID before retrying.")
print()
# Inspect completed calls; validate names/arguments before executing anything.
for tool in tools.values():
print("Tool call:", json.dumps(tool), file=sys.stderr)
except APIStatusError as error:
print("Request ID:", error.request_id, file=sys.stderr)
print("HTTP error:", error.status_code, error.response.text, file=sys.stderr)
raise
except (APIError, httpx.HTTPError, json.JSONDecodeError, RuntimeError) as error:
# Mid-stream reads can raise httpx errors directly, outside the SDK wrapper.
print("Generation failed:", error, file=sys.stderr)
if isinstance(error, APIError) and error.body is not None:
print("Error details:", error.body, file=sys.stderr)
raise