Skip to main content

deepinfra

CI PyPI version Python Version License

The official Python SDK for the DeepInfra API: Sandboxes (isolated microVMs for running untrusted code) and inference.

Installation

pip install deepinfra

Authentication uses your DeepInfra API key — pass api_key= or set the DEEPINFRA_API_KEY environment variable (the same key you use for inference, from deepinfra.com/dash/api_keys).

Sandboxes

Create an isolated Linux microVM, run bash/python inside it, move files in and out, and tear it down — in a few lines:

from deepinfra import Sandbox

sb = Sandbox.create(plan="medium", timeout="10m")     # blocks until running

r = sb.exec("bash", "-c", "pip install pandas && python -c 'import pandas; print(pandas.__version__)'")
print(r.stdout, r.stderr, r.returncode)

out = sb.run_python("print(21 * 2)").check()          # .check() raises on non-zero exit
print(out.stdout)                                     # "42"

sb.fs.write("/workspace/in.csv", b"a,b\n1,2\n")
data = sb.fs.read("/workspace/in.csv")

sb.stop()        # frees compute, keeps disk; blocks until stopped
sb.start()       # resumes on the same disk; blocks until running
sb.terminate()   # deletes the sandbox (stays fetchable by id as "deleted" briefly)

/workspace is the only location fs accepts, and the only one that survives stop()/start() — the rest of the filesystem comes back from the base image, so packages installed at runtime are gone after a restart. Sandboxes also auto-stop once idle for timeout (1 hour by default), which has the same effect.

Every network method has an async twin prefixed with a:

sb = await Sandbox.acreate(plan="small")
r = await sb.aexec("uname", "-a")
await sb.aterminate()

The zero-config async calls share one process-wide client whose connection pool binds to the first event loop that uses it. If your program calls asyncio.run() more than once, create a DeepInfraClient per loop and pass it via client= (closing it with await client.aclose() before the loop exits), instead of relying on the default client.

Useful patterns:

# Auto-terminate with a context manager
with Sandbox.create(plan="small") as sb:
    sb.run_python("open('/workspace/out.txt', 'w').write('hi')")
    print(sb.fs.read("/workspace/out.txt"))

# Find existing sandboxes
sb = Sandbox.from_id("sb_...")
etl_boxes = Sandbox.list(tags={"job": "etl-42"})

# List available plans (id, vcpu, ram_gb, disk_gb, price_per_hour)
for plan in Sandbox.catalog():
    print(plan.id, plan.vcpu, plan.ram_gb, plan.price_per_hour)

# Large scripts: upload, then run
sb.fs.write("/workspace/script.py", open("script.py").read())
sb.exec("python3", "/workspace/script.py", timeout="30m")

Errors are typed: AuthenticationError (401), NotFoundError (404), ConflictError (409, e.g. exec on a stopped sandbox), RateLimitError (429; for sandboxes that's the per-account cap — TooManySandboxesError is an alias), CapacityError (503), plus SDK-side SandboxTimeoutError / SandboxFailedError / CommandFailedError. If Sandbox.create(wait=True) fails while waiting, the raised error carries .sandbox_id so you can inspect or terminate the sandbox it created.

Roadmap (API designed, lands in an upcoming release): exec_stream (live output), snapshot() / Sandbox.from_snapshot(), expose_port(), fs.upload_dir().

Inference

The inference wrappers predate the SDK's OpenAI-compatible endpoints and remain supported:

Automatic Speech Recognition

from deepinfra import AutomaticSpeechRecognition

asr = AutomaticSpeechRecognition("openai/whisper-base")

body = {"audio": "path/to/audio/file"}  # or a URL, or raw bytes
transcription = asr.generate(body)
print(transcription.text)

Text Generation

from deepinfra import TextGeneration

llm = TextGeneration("mistralai/Mixtral-8x22B-Instruct-v0.1")
res = llm.generate({"input": "What is the capital of France?"})
print(res.results[0].generated_text)

Embeddings and TextToImage work the same way. For chat-style LLM usage you can also point the official OpenAI client at https://api.deepinfra.com/v1/openai.

Development

pip install -e ".[dev]"
pytest tests          # unit tests (no network)
mypy && ruff check .  # types + lint

Metadata

Release files for deepinfra 0.3.0

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

Source distribution (sdist)

Source distribution for deepinfra 0.3.0
File Size Uploaded
deepinfra-0.3.0.tar.gz 26.5 kB Details

Built distribution (wheel)

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

Total release size: 56.3 kB

Release files / deepinfra-0.3.0.tar.gz

Download URL deepinfra-0.3.0.tar.gz
Size 26.5 kB
Tags Source
SHA-256 checksum
How to use checksums
acaf5ac4db0f440cef89d84c8f593d4d300b151c937e985c9af9ef9dc120a7a6
BLAKE2b-256 checksum
How to use checksums
8f6d5513ec603e2eb01d5ef028f4da7469080c3bcb6661d2414f5478c8b0383f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.

Transparency log

Release files / deepinfra-0.3.0-py3-none-any.whl

Download URL deepinfra-0.3.0-py3-none-any.whl
Size 29.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d01726f675be27684c892fc821f57b879d08d01cb3b9e998daf116ef43624280
BLAKE2b-256 checksum
How to use checksums
11447d81a441568e90ec70d1c633f6333b1493f52e5a18321c37763c3ce57a85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

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