Skip to main content

Python SDK Reference

Full API reference for the PolarGrid Python SDK.

Clients

ClassDescription
PolarGridAsync client (recommended)
PolarGridSyncSynchronous wrapper

Client Methods

Text Inference

MethodDescription
chat_completion(request)Generate chat completion
chat_completion_stream(request)Streaming chat completion
completion(request)Generate text completion
completion_stream(request)Streaming text completion
generate(request)Legacy generate (wraps chat_completion)

Voice / Audio

MethodDescription
text_to_speech(request)Convert text to audio (returns bytes)
text_to_speech_stream(request)Streaming TTS
transcribe(file, request)Transcribe audio to text
translate(file, request)Translate audio to English

Models

MethodDescription
list_models()List available models
load_model(request)Load a model into GPU memory
unload_model(request)Unload a model
unload_all_models()Unload all models
get_model_status()Get model loading status

GPU

MethodDescription
get_gpu_status()Detailed GPU status
get_gpu_memory()Simplified GPU memory info
purge_gpu(request)Clear GPU memory

Health & Region

MethodDescription
health()Service health check
get_region_id()Get current region ID
get_region_name()Get current region name

Error Handling

from polargrid import (
    PolarGrid,
    PolarGridError,
    AuthenticationError,
    ValidationError,
    RateLimitError,
    NetworkError,
    TimeoutError,
    NotFoundError,
    ServerError,
)

try:
    response = await client.chat_completion(request)
except PolarGridError as e:
    print(f"Error: {e.message}")
    print(f"Request ID: {e.request_id}")

    if isinstance(e, AuthenticationError):
        # Invalid or expired API key
        pass
    elif isinstance(e, ValidationError):
        # Invalid request parameters
        print("Details:", e.details)
    elif isinstance(e, RateLimitError):
        # Rate limited
        print(f"Retry after: {e.retry_after}s")
    elif isinstance(e, NetworkError):
        # Network/connection error
        pass
    elif isinstance(e, TimeoutError):
        # Request timed out
        pass

Types

All types are Pydantic models with full type hints:
from polargrid.types import (
    # Config
    PolarGridConfig,
    
    # Text Inference
    ChatCompletionRequest,
    ChatCompletionResponse,
    ChatCompletionChunk,
    CompletionRequest,
    CompletionResponse,
    CompletionChunk,
    GenerateRequest,
    GenerateResponse,
    Message,
    TokenUsage,
    
    # Voice
    TextToSpeechRequest,
    TranscriptionRequest,
    TranscriptionResponse,
    VerboseTranscriptionResponse,
    TranslationRequest,
    TranslationResponse,
    TTSVoice,
    STTModel,
    AudioFormat,
    TranscriptionFormat,
    
    # Models
    ModelInfo,
    ListModelsResponse,
    LoadModelRequest,
    LoadModelResponse,
    UnloadModelRequest,
    UnloadModelResponse,
    UnloadAllModelsResponse,
    ModelStatusResponse,
    
    # GPU
    GPUStatusResponse,
    GPUMemoryResponse,
    GPUPurgeRequest,
    GPUPurgeResponse,
    GPUInfo,
    GPUMemoryInfo,
    GPUProcess,
    
    # Health
    HealthResponse,
    HealthFeatures,
    BackendStatus,
)

Enums

from polargrid.types import TTSVoice, STTModel, AudioFormat, TranscriptionFormat

# TTS Voices
TTSVoice.ALLOY      # "alloy"
TTSVoice.ECHO       # "echo"
TTSVoice.NOVA       # "nova"
TTSVoice.AF_BELLA   # "af_bella" (Kokoro)

# STT Models
STTModel.WHISPER_1                    # "whisper-1"
STTModel.WHISPER_LARGE_V3_TURBO      # "whisper-large-v3-turbo"

# Audio Formats
AudioFormat.MP3   # "mp3"
AudioFormat.WAV   # "wav"
AudioFormat.OPUS  # "opus"

# Transcription Formats
TranscriptionFormat.JSON         # "json"
TranscriptionFormat.VERBOSE_JSON # "verbose_json"
TranscriptionFormat.SRT          # "srt"
TranscriptionFormat.VTT          # "vtt"

Regions

from polargrid.client import POLARGRID_REGIONS, REGION_ALIASES

# Available regions
print(POLARGRID_REGIONS)
# {
#   'yvr-01': {'id': 'yvr-01', 'name': 'Vancouver', 'url': '...'},
#   'ymq-01': {'id': 'ymq-01', 'name': 'Montreal', 'url': '...'},
#   'was-01': {'id': 'was-01', 'name': 'Washington', 'url': '...'},
# }

# Aliases: 'vancouver', 'yvr', 'montreal', 'ymq', 'washington', 'was'

File Handling

The transcribe() and translate() methods accept multiple file types:
from pathlib import Path

# Path object
transcription = await client.transcribe(
    file=Path("recording.mp3"),
    request={"model": "whisper-1"}
)

# Bytes
with open("recording.mp3", "rb") as f:
    audio_bytes = f.read()
transcription = await client.transcribe(
    file=audio_bytes,
    request={"model": "whisper-1"}
)

# File-like object
with open("recording.mp3", "rb") as f:
    transcription = await client.transcribe(
        file=f,
        request={"model": "whisper-1"}
    )