sference Python SDK
Installable package: sference-sdk (import: sference_sdk), Python 3.11+. Used by the sference CLI and your own automation.
Install
uv add sference-sdk
Fallback:
pip install sference-sdk
From a clone of this repo:
uv sync --package sference-sdk
Usage
Set SFERENCE_API_KEY, or pass api_key= to the client.
Completion window: async workloads use "24h" (the only supported value) on background responses (metadata.completion_window), streams (window=), and batches (window=). Sync realtime endpoints (/v1/chat/completions, /v1/messages, blocking /v1/responses) do not take a window.
./workload.jsonl
Batch APIs take a JSONL file: one JSON object per line. OpenAI-compatible lines include custom_id, method, url, and body (only custom_id + inner body are sent to POST /v1/batches; method/url are ignored). Content-only lines are {"content": "..."} (then pass model= on submit).
Inner body accepts chat completions (messages) or Responses (input, max_output_tokens, …). Responses fields are normalized to chat format at create and validated before enqueue. Invalid rows return HTTP 400 with requests[i] and optional custom_id.
Example workload.jsonl:
{"custom_id":"example-1","method":"POST","url":"/v1/chat/completions","body":{"model":"deepseek-ai/DeepSeek-V4.1-Flash-1017","messages":[{"role":"user","content":"Say hello in exactly one word."}]}}
{"custom_id":"example-2","method":"POST","url":"/v1/chat/completions","body":{"model":"deepseek-ai/DeepSeek-V4.1-Flash-1017","messages":[{"role":"system","content":"You reply with one short sentence only."},{"role":"user","content":"What is 2+2?"}]}}
{"custom_id":"example-3","method":"POST","url":"/v1/responses","body":{"model":"deepseek-ai/DeepSeek-V4.1-Flash-1017","input":[{"role":"user","content":"Reply with one word."}],"max_output_tokens":32}}
Batches (sync)
Best for a fixed JSONL workload: one submit, poll until terminal, then fetch structured results or download JSONL via the API.
from sference_sdk import SferenceClient
client = SferenceClient(api_key="sk_...")
batch = client.submit_batch(
input_file="./workload.jsonl",
model="deepseek-ai/DeepSeek-V4.1-Flash-1017",
window="24h",
)
done = client.wait_for_completion(batch.id) # polls until terminal; pass timeout= to bound it
results = client.get_results(done.id)
by_id = results.index_by_custom_id()
print(by_id["row-a"].completion_text)
Or in one call:
by_id = client.get_results_indexed(done.id)
Build chat rows without hand-assembling body.messages:
from sference_sdk.models import InferenceRequest
req = InferenceRequest.chat(
custom_id="row-a",
user_content="Summarize this.",
system_content="One sentence only.",
model="deepseek-ai/DeepSeek-V4.1-Flash-1017",
temperature=0,
)
batch = client.submit_batch(requests=[req], window="24h")
Use a model supported by your sference deployment.
OpenAI-compatible responses (sync)
Standalone or stream-associated jobs via POST /v1/responses. Keys need responses:read and responses:write (default on newly issued keys).
from sference_sdk import SferenceClient
client = SferenceClient(api_key="sk_...")
created = client.create_response(
model="deepseek-ai/DeepSeek-V4.1-Flash-1017",
input=[{"role": "user", "content": "Hello"}],
metadata={"completion_window": "24h"},
)
row = client.get_response(created.id)
For a stream, add stream_id inside metadata next to completion_window.
create_response also takes tools, tool_choice, reasoning, include, text, prompt_cache_key and max_queue_wait_s. Two options matter for cost and reliability:
service_tier="flex"gives discounted tokens in exchange for lower scheduling priority (pair it withbackground=True);"priority"is scheduled ahead of default traffic and billed at a premium.response.service_tieris the tier actually served.idempotency_key="..."lets you retry a create after a network error without running the request twice.
Streaming output (SSE)
For interactive use, stream_response takes the same options as create_response and yields events while the model generates. This is HTTP streaming of one response, unrelated to sference streams (create_stream), which group many responses. The events follow the OpenAI Responses streaming format:
for event in client.stream_response(
model="deepseek-ai/DeepSeek-V4.1-Flash-1017",
input="Write a haiku about queues.",
):
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
elif event.type in ("response.completed", "response.incomplete", "response.failed"):
final = event.response # full Response with output and usage
Reasoning arrives as response.reasoning_summary_text.delta events. If the server fails mid-stream, iteration raises ApiError. Stopping iteration early disconnects, which cancels the generation. AsyncSferenceClient.stream_response works the same way with async for.
Models, usage and billing
models = client.list_models() # typed catalog: modality, pricing, capabilities, retires_on
live = [m.id for m in models.data if not m.retires_on]
client.get_billing_balance().credit_balance_micros # micro-USD
client.get_usage_summary(days=7).summary.total_cost_micros
client.get_usage_by_key(month="2026-11").items # spend per API key
page = client.list_billing_transactions(limit=50) # next page: starting_after=page.next_starting_after
Batches, streams and completion events also report cost in micro-USD (total_cost_micros, cost_micros).
Errors
Every non-2xx response raises sference_sdk.ApiError with status_code, type, code, message and the raw body:
from sference_sdk import ApiError
try:
client.create_response(model="deepseek-ai/DeepSeek-V4.1-Flash-1017", input="hi")
except ApiError as err:
if err.status_code == 429:
... # back off and retry
Timeouts
Most calls time out after 30 seconds. Calls that the server keeps open while it works get longer waits that sit just above the server's own limits, so you get the server's error (a 408 or 504, or a failed stream event) instead of a client-side timeout. A blocking create_response waits up to 31 minutes, stream_response up to 6 minutes between events, and create_decision 45 seconds. Passing timeout= to the client sets one timeout for every call instead.
Decisions (realtime classification)
POST /v1/decisions answers up to 64 questions about one state (text or any JSON value) in a single call. Pick a model with modality == "decisions" from list_models(). Question types: choice (pick one label), score (ordinal scale, index 0 = lowest) and noul (probability of true). Billed on input tokens only, including image tokens.
Both clients accept images=["data:image/png;base64,..."] or
images=[{"content_type": "image/jpeg", "base64": "..."}] (also available as
sference_sdk.DecisionImage). Images precede the state in array order and travel
inline without separate uploads. Clef accepts up to 4 PNG/JPEG/WebP images,
4 MiB and 16 megapixels each, 8 MiB total decoded bytes, with a 13 MiB request
body limit. Remote URLs are not accepted. Accepted images are EXIF-oriented and
downscaled to at most 2,097,152 pixels before inference; usage counts the resulting
image tokens. The 16-megapixel limit applies to the original uploaded image.
from sference_sdk import ChoiceQuestion, NoulQuestion, ScoreQuestion, SferenceClient
client = SferenceClient(api_key="sk_...")
decision = client.create_decision(
model="Cloudflare/clef",
state="I was charged twice this month, please refund me.",
questions={
"route": ChoiceQuestion(criteria={"billing": "Payments and invoices", "support": "Everything else"}),
"urgency": ScoreQuestion(criteria=["Low", "Medium", "High"]),
"refund": NoulQuestion(instructions="Does the customer ask for a refund?"),
},
)
decision.answers["route"].choice # "billing"
decision.answers["urgency"].score # 0..2, expected value over the scale
decision.answers["refund"].noul # probability of true
Questions may also be plain dicts ({"type": "noul"}). 429 means no decision capacity right now and is safe to retry; 504 means the deadline passed and nothing was charged.
OpenAI Python SDK (openai package)
If you already use the official OpenAI client, point it at sference’s /v1 endpoint and the same API key (with responses:read and responses:write).
pip install openai
import asyncio
import os
from openai import AsyncOpenAI
async def main() -> None:
client = AsyncOpenAI(
base_url="https://api.sference.com/v1",
api_key=os.environ["SFERENCE_API_KEY"],
)
response = await client.responses.create(
model="deepseek-ai/DeepSeek-V4.1-Flash-1017",
input=[{"role": "user", "content": "Hello, world!"}],
background=True,
)
# Poll GET /v1/responses/{id} until terminal; your openai version may expose
# something like await client.responses.retrieve(response.id), or use
# AsyncSferenceClient.get_response(response.id) with the same API key.
asyncio.run(main())
Metadata: to set completion_window or stream_id like the native SDK, pass them in the request body your openai version supports (for example metadata= on create, or extra_body={"metadata": {...}} if the helper does not list those fields yet).
Async client — batches
AsyncSferenceClient uses httpx.AsyncClient so batch polling can run alongside other async I/O without blocking threads.
Use case: You already know the full set of prompts (for example a JSONL file) and want one scheduled unit of work with a clear terminal state and bulk results.
Benefits: Simple lifecycle (submit → wait → fetch results), fits large static workloads and JSONL-heavy pipelines.
import asyncio
from sference_sdk import AsyncSferenceClient
async def main() -> None:
async with AsyncSferenceClient(api_key="sk_...") as client:
batch = await client.submit_batch(
input_file="./workload.jsonl",
model="deepseek-ai/DeepSeek-V4.1-Flash-1017",
window="24h",
)
done = await client.wait_for_completion(batch.id, poll_interval=2.0, timeout=3600.0)
results = await client.get_results(done.id)
print(results.status, results.output_url)
asyncio.run(main())
Async client — streams
Stream-associated jobs use create_response(..., metadata={"stream_id": ..., "completion_window": "24h"}). Consume completions with list_responses_events / iter_responses_events (optional stream_id, wait_ms long-poll; optional checkpoints align with CLI sference responses tail).
Use case: Work arrives over time, or you want one id to group many responses and observe completions as they land.
Benefits: Independent submits with aggregated progress, stream-level status in the API/UI, and efficient event tailing.
import asyncio
from sference_sdk import AsyncSferenceClient
async def main() -> None:
async with AsyncSferenceClient(api_key="sk_...") as client:
stream = await client.create_stream(name="sdk-demo", window="24h")
await client.create_response(
model="deepseek-ai/DeepSeek-V4.1-Flash-1017",
input=[{"role": "user", "content": "Hello"}],
metadata={"stream_id": stream.id, "completion_window": "24h"},
)
async for ev in client.iter_responses_events(stream_id=stream.id, checkpoint=False):
print(ev.completion_id, ev.status)
asyncio.run(main())
CLI
For sference batch … and sference stream … commands, see the CLI README.
Metadata
Release files for sference-sdk 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sference_sdk-0.4.0.tar.gz | 24.0 kB | With provenance | Details for sference_sdk-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sference_sdk-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for sference_sdk-0.4.0-py3-none-any.whl |
Total release size: 55.5 kB
Release files / sference_sdk-0.4.0.tar.gz
| Download URL | sference_sdk-0.4.0.tar.gz |
|---|---|
| Size | 24.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8245544f9fcbf1146a0b35c839678ebb11cad5300fe37454b8378d4b0255418a
|
|
BLAKE2b-256 checksum How to use checksums |
8a95a4295a3408e341cf1e9319106905a023e5ea543f3bf46ac5014d4ce115e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 26, 2026.
Transparency logRelease files / sference_sdk-0.4.0-py3-none-any.whl
| Download URL | sference_sdk-0.4.0-py3-none-any.whl |
|---|---|
| Size | 31.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c3870a6f66a80fe4259b98eb499fe9be2cf52c463da3f4bb1040b20ee8d02b38
|
|
BLAKE2b-256 checksum How to use checksums |
72c001b058706a48f393e550983cbadefaccc93943e0045864d461131b6d93b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 26, 2026.
Transparency logSecurity for sference-sdk 0.4.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for sference_sdk-0.4.0.tar.gz
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 Nov 26, 2026.
Transparency logA 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.