Skip to main content

Python SDK

Install NanoGPT’s official Python SDK from PyPI. It needs Python 3.9 or later and has no runtime dependencies. The distribution is named nanogpt-python; the import package is nanogpt.
Set NANOGPT_API_KEY to your API key, then:
Responses support typed attributes and dictionary access. Unknown response fields remain available by key.

Streaming

Use the stream_ helpers for streaming; regular methods return one JSON response.

Async

AsyncNanoGPT exposes the same methods. Await regular calls and use async for with streaming helpers:

Errors and retries

API failures raise NanoGPTError, with status-specific subclasses such as AuthenticationError and RateLimitError. Errors expose status, message, request_id, retry_after and response headers. Network failures use standard Python transport exceptions.
The SDK retries eligible 429 and server failures with backoff. Interrupted bodies and streams are not automatically replayed; output received before a stream error may be partial.

What it covers

The SDK supports Chat Completions, Responses, Anthropic Messages, embeddings, model listing, usage, account helpers and the file-backed Batch API. It includes file upload, batch quoting and creation, polling, results and cancellation. The separate Anthropic Message Batches API uses Anthropic’s official SDK. The default host is https://api.nano-gpt.com. Set base_url to use another host. For method details and Batch examples, see the SDK README on PyPI. For other endpoints, use the HTTP API. For an OpenAI SDK integration, use base_url="https://api.nano-gpt.com/api/v1" with the official OpenAI client instead.