Skip to main content

swapllm

Swap between LLM providers — Groq, OpenAI, Anthropic — through one interface, with automatic fallback on rate-limit/timeout/outage and optional schema-validated responses. Write your call site once against Router.complete(), and changing providers (or falling back to a second one when the first is down) is a config change, not a code change.

Install

pip install swapllm

Quickstart

from swapllm import Router, GroqProvider, OpenAIProvider, AnthropicProvider

router = Router(
    providers=[
        GroqProvider(api_key=..., model="llama-3.1-70b"),
        OpenAIProvider(api_key=..., model="gpt-4o-mini"),
        AnthropicProvider(api_key=..., model="claude-haiku-4-5"),
    ],
    fallback_order=["groq", "openai", "anthropic"],
)

response = router.complete(
    messages=[{"role": "user", "content": "..."}],
    schema=MyPydanticSchema,   # optional — validates before returning
)

Fallback behavior

Router.complete() tries each provider in fallback_order, in order, and returns the first successful completion.

Triggers fallback to the next provider:

  • Rate limit (429)
  • Timeout, or the request never reaching the provider at all
  • A 5xx from the provider
  • A response that fails schema validation (when schema= is given) — a provider "responding" with unusable content isn't success

Does not trigger fallback — propagates immediately instead:

  • ProviderRequestError (e.g. 400/401/403, a malformed request, a bad API key) — this is almost always a caller misconfiguration, and switching providers would silently mask it rather than surface it
  • A plain ValueError from malformed input (e.g. multiple or misplaced system messages) — same reasoning: a different provider won't fix bad input

There is also no same-provider retry: a rate-limited or 5xx-ing provider won't un-limit itself in the next few seconds, so a failure always advances to the next provider, never the same one again. If every provider in fallback_order fails, Router.complete() raises AllProvidersFailedError with every provider's individual failure reason attached — it never silently returns None or an empty string.

Schema validation

schema= is optional. Pass a Pydantic model and Router.complete() parses the winning provider's text as JSON, validates it against the schema, and returns the validated model instance instead of a raw string. A leading/trailing markdown code fence (```json ... ```) is stripped first, since providers without strict JSON mode routinely wrap structured output that way.

Validation failure is treated as a provider failure, not a caller error — it triggers fallback to the next provider like any other retryable failure, since a provider returning malformed JSON isn't meaningfully different from a provider returning nothing at all.

Skip schema= entirely for the common case of just wanting a chat reply back as a string.

Testing / contributing

python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest

Tests are contract tests against mocked provider HTTP responses — no real API calls are made, and none are required to run the suite.

License

MIT — see LICENSE.

Metadata

Release files for swapllm 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swapllm 0.1.1
File Size Uploaded Provenance Actions
swapllm-0.1.1.tar.gz 21.5 kB Without provenance Details for swapllm-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swapllm 0.1.1
File Interpreter ABI Platform Actions
swapllm-0.1.1-py3-none-any.whl Python 3 none any Details for swapllm-0.1.1-py3-none-any.whl

Total release size: 38.9 kB

Release files / swapllm-0.1.1.tar.gz

Download URL swapllm-0.1.1.tar.gz
Size 21.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5684e4e165dbc984a2269d9710175a4beca602373e65bc147885d8e3f3c60728
BLAKE2b-256 checksum
How to use checksums
a7a11d8b399d5da018a462507cddf5ff3f0dd9644f19dcc72087e68ab77b1e73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Release files / swapllm-0.1.1-py3-none-any.whl

Download URL swapllm-0.1.1-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3db92b0138cdf80443d1ae8409332a92ce850157101e145c69372c2ecc4314b8
BLAKE2b-256 checksum
How to use checksums
efb73b7ae63a7349ecc88c6865b917e2e4a46be689fba8d8fb6de68bf89aa493
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Security for swapllm 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page