Skip to main content

Python SDK for the Shard distributed inference network

Project description

Shard Python SDK

Python client for the Shard distributed inference network.

Installation

pip install shard-inference

Or install from source:

cd sdk/python
pip install -e .

Quick Start

from shard import Shard

# Connect to local daemon (default: http://localhost:9091)
client = Shard()

# Non-streaming request
response = client.chat.completions.create(
    model="shard-hybrid",
    messages=[{"role": "user", "content": "Explain quantum computing simply."}]
)
print(response.choices[0].message.content)

# Streaming request
stream = client.chat.completions.create(
    model="shard-hybrid",
    messages=[{"role": "user", "content": "Count to 5"}],
    stream=True
)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

client.close()

OpenAI-Compatible Drop-In

The SDK is designed to be a drop-in replacement for the OpenAI client:

# Instead of:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:9091/v1", api_key="...")

# You can use:
from shard import Shard
client = Shard()  # Defaults to http://localhost:9091

All standard OpenAI parameters are supported:

  • messages - Chat messages
  • model - Model identifier
  • temperature - Sampling temperature (0.0-2.0)
  • max_tokens - Maximum tokens to generate
  • top_p - Nucleus sampling parameter
  • stream - Enable streaming responses

Async Usage

import asyncio
from shard import AsyncShard

async def main():
    async with AsyncShard() as client:
        response = await client.chat.completions.create(
            model="shard-hybrid",
            messages=[{"role": "user", "content": "Hello!"}]
        )
        print(response.choices[0].message.content)

asyncio.run(main())

API Key & Authentication

# With API key
client = Shard(api_key="sk-your-key-here")

# Connect to remote server
client = Shard(base_url="https://your-shard-server.com", api_key="sk-...")

Private Mesh Routing

For sensitive requests that should bypass public bootstrap peers:

response = client.chat.completions.create(
    model="shard-hybrid",
    messages=[...],
    sensitive=True  # Routes via X-Shard-Route: private
)

Configuration

client = Shard(
    api_key="sk-...",           # API key (optional)
    base_url="http://localhost:9091",  # Daemon URL
    timeout=30.0,               # Request timeout (seconds)
    max_retries=3,              # Retry attempts on failure
)

Exceptions

The SDK provides typed exceptions:

  • ShardError - Base exception
  • ShardAPIError - API errors (4xx/5xx responses)
  • ShardAuthError - Authentication failures (401)
  • ShardTimeoutError - Request timeouts
  • ShardConnectionError - Connection failures
from shard import Shard, ShardAPIError

try:
    client = Shard()
    response = client.chat.completions.create(messages=[...])
except ShardAPIError as e:
    print(f"API Error: {e.status_code} - {e.response_body}")
except ShardTimeoutError:
    print("Request timed out")

Requirements

  • Python 3.9+
  • httpx >= 0.25.0
  • pydantic >= 2.0

For async support:

  • aiohttp >= 3.9.0

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shard_inference-0.5.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shard_inference-0.5.1-cp314-cp314-win_amd64.whl (12.2 MB view details)

Uploaded CPython 3.14Windows x86-64

File details

Details for the file shard_inference-0.5.1.tar.gz.

File metadata

  • Download URL: shard_inference-0.5.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for shard_inference-0.5.1.tar.gz
Algorithm Hash digest
SHA256 cd5e7182b029ca2c22d534f2ced7eb67be7e9f27b037ba95451f8cf482fd1296
MD5 fa59f2db92398cc242ccbd9150e15b2e
BLAKE2b-256 c33d982065f4108eb19129328cf3347f1a5b76b4b555383e3b148334fc67db05

See more details on using hashes here.

File details

Details for the file shard_inference-0.5.1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for shard_inference-0.5.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 6f6722f8294ad3bcb565e0724e950f3627011e778d67bf6d2c0019cab5de4514
MD5 1e133405e9977bc89e8bef2ba067bfad
BLAKE2b-256 b55785f692502a1377b80e20b6f5ba4f413f3c52c35808ff144d85e315dbc2ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page