Skip to main content

Enterprise Drop-in SDK for Polyswitch AI Gateway — 1-line migration from OpenAI

Project description

polyswitch (Python SDK)

1-line drop-in replacement for the OpenAI Python SDK — route all your AI traffic through Polyswitch Enterprise AI Gateway with zero code changes.

PyPI version Python Versions License: MIT


Installation

pip install polyswitch

Quick Start

Replace your existing openai import — everything else stays exactly the same:

# Before
from openai import OpenAI
client = OpenAI(api_key="sk-...")

# After — 1-line change
from polyswitch import Polyswitch
client = Polyswitch(api_key="pr_your_key_here")

# Or use the alias for truly zero-change migration
from polyswitch import OpenAI
client = OpenAI()  # reads POLYSWITCH_API_KEY from env automatically

Environment Variables

Variable Description Default
POLYSWITCH_API_KEY Your Polyswitch API key pr_guest (demo mode)
POLYSWITCH_GATEWAY_URL Gateway endpoint http://localhost:3000/api/v1
OPENAI_API_KEY Fallback — also accepted
OPENAI_BASE_URL Fallback — also accepted

Routing Strategies

Control how Polyswitch routes your requests across AI providers:

from polyswitch import Polyswitch, Strategies

client = Polyswitch(
    strategy=Strategies.auto_smart()               # Auto-pick best model (default)
    # strategy=Strategies.cost_optimized(1.0)      # Stay under $1/M tokens
    # strategy=Strategies.hedged_latency(500, "gpt-4o-mini")  # Hedge on latency
    # strategy=Strategies.kv_affinity("user-session-123")     # Sticky routing
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

PII Redaction

Automatically strip PII from all requests before they leave your network:

client = Polyswitch(pii_redact=True)

Async Support

Full async support via AsyncPolyswitch:

import asyncio
from polyswitch import AsyncPolyswitch, AsyncOpenAI

async def main():
    client = AsyncPolyswitch()
    response = await client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(response.choices[0].message.content)

asyncio.run(main())

Extra APIs

The SDK exposes additional Polyswitch-native APIs beyond standard OpenAI:

Agents

result = client.agents.run(
    persona={"name": "Aria", "role": "analyst", "systemPrompt": "You are a data analyst."},
    task_goal="Summarize Q4 revenue trends",
    tools=[{"name": "fetch_data", "description": "Fetch financial data"}],
    limits={"maxToolCalls": 5}
)

OCR

with open("document.pdf", "rb") as f:
    result = client.ocr.extract(f, model="mistral/mistral-ocr")

Rerank

result = client.rerank.create(
    query="What is the capital of France?",
    documents=["Paris is in France.", "London is in England."],
    model="cohere/rerank-english-v3.0",
    top_n=1
)

Search

result = client.search.query(
    query="latest AI research papers",
    max_results=5
)

Quota

quota = client.quota.get()

Type Hints

Full type hints included. Compatible with mypy and pyright:

from polyswitch import Polyswitch, Strategies
from typing import Optional

def create_client(strategy: Optional[str] = None) -> Polyswitch:
    return Polyswitch(strategy=strategy or Strategies.auto_smart())

License

MIT © Polyswitch

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

polyswitch-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

polyswitch-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file polyswitch-0.1.0.tar.gz.

File metadata

  • Download URL: polyswitch-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for polyswitch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a5feeb60e145cccddeda8edbc2850b23ed99c4d4578f37b9d09aab817ba545c4
MD5 bb132819e6d7dcd9700c144683729289
BLAKE2b-256 fce3526c7a3373b4a936035925a3f4fd9904fedbf32ddaa7ca34dedf4fb46e07

See more details on using hashes here.

File details

Details for the file polyswitch-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: polyswitch-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for polyswitch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1a199a59a223de3ca85ba237959266de82babdd2259d9af428d22555eeadf36
MD5 beb93853409e0aba33385a9bcb4c445c
BLAKE2b-256 0f5d02efd5e75b3e89671e1e524108d8c30ec6d58181a7c88a02e3f4d7ec961e

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