> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nano-gpt.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK

> The official NanoGPT Python client: sync, async, streaming and Batch API

# Python SDK

Install NanoGPT's official Python SDK from [PyPI](https://pypi.org/project/nanogpt-python/). It needs Python 3.9 or later and has no runtime dependencies. The distribution is named `nanogpt-python`; the import package is `nanogpt`.

```bash theme={null}
pip install nanogpt-python
```

Set `NANOGPT_API_KEY` to your [API key](https://nano-gpt.com/api), then:

```python theme={null}
import os
from nanogpt import NanoGPT

client = NanoGPT(api_key=os.environ["NANOGPT_API_KEY"])
completion = client.chat_completion(
    model="openai/gpt-6.1-sol",
    messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
print(completion.choices[0].message.content)
```

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.

```python theme={null}
for chunk in client.stream_chat_completion(
    model="openai/gpt-6.1-sol",
    messages=[{"role": "user", "content": "Count to three."}],
):
    print(chunk.text, end="", flush=True)
```

## Async

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

```python theme={null}
import asyncio
import os
from nanogpt import AsyncNanoGPT

async def main():
    client = AsyncNanoGPT(api_key=os.environ["NANOGPT_API_KEY"])
    response = await client.create_response(
        model="openai/gpt-6.1-sol",
        input="Write a haiku.",
    )
    print(response.output_text)

asyncio.run(main())
```

## 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.

```python theme={null}
from nanogpt import NanoGPTError

try:
    print(client.check_balance())
except NanoGPTError as error:
    print(error.status, error.message, error.request_id)
```

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](https://docs.nano-gpt.com/api-reference/endpoint/batches.md). 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](https://pypi.org/project/nanogpt-python/0.2.0/#description). For other endpoints, use the [HTTP API](https://docs.nano-gpt.com/api-reference/introduction).

For an OpenAI SDK integration, use `base_url="https://api.nano-gpt.com/api/v1"` with the official OpenAI client instead.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.