Skip to main content
For concepts, architecture, and usage guidance, see the Sprocket overview.

sprocket.Sprocket

Base class for inference workers.
MethodSignatureDescription
setupsetup(self) -> NoneCalled once at startup. Load models and resources.
predictpredict(self, args: dict) -> dictCalled for each job. Process input and return output.
shutdownshutdown(self) -> NoneCalled on graceful shutdown. Clean up resources. Optional.
Class attributes:
AttributeTypeDefaultDescription
processorType[InputOutputProcessor]InputOutputProcessorCustom I/O processor class
warmup_inputslist[dict][]Inputs to run during cache warmup

sprocket.run

Entry point for starting a Sprocket worker.
ParameterTypeDescription
sprocketSprocketYour Sprocket instance
namestrDeployment name (used for queue routing)
use_torchrunboolEnable multi-GPU mode via torchrun. Default: False

sprocket.FileOutput

Wraps a local file path for automatic upload after predict() returns. Extends pathlib.PosixPath.
The FileOutput is replaced with the public URL in the final job result.

sprocket.emit_info

Report progress updates from inside predict(). Emitted data is available to clients via the info field on the job status endpoint.
ParameterTypeDescription
infodictProgress data to emit. Must serialize to under 4096 bytes JSON.
Updates are batched and merged (later values overwrite earlier ones for the same keys). When using use_torchrun=True, call emit_info() only from rank 0 to avoid duplicate updates.

sprocket.InputOutputProcessor

Override for custom file download/upload behavior. Attach to your Sprocket via the processor class attribute.

Custom I/O Processing

MethodSignatureDescription
process_input_fileprocess_input_file(self, resp: httpx.Response, dst: pathlib.Path) -> NoneCalled after downloading each input file. Write resp.content to dst.
finalizeasync finalize(self, request_id: str, inputs: dict, outputs: dict) -> dictCalled after predict(), before FileOutput upload. Return modified outputs.
Default behavior:
  • process_input_file: writes resp.content to dst
  • finalize: returns outputs unchanged

HTTP Endpoints

EndpointMethodResponse
/healthGET200 {"status": "healthy"} or 503 {"status": "unhealthy"}
/metricsGETrequests_inflight 0.0 or 1.0 (Prometheus format)
/generatePOSTDirect HTTP inference (non-queue mode)

CLI Arguments

ArgumentDefaultDescription
--queuefalseEnable queue worker mode
--port8000HTTP server port

Environment Variables

VariableDefaultDescription
TOGETHER_API_KEYRequiredAPI key for queue authentication
TOGETHER_API_BASE_URLhttps://api.together.aiAPI base URL
TERMINATION_GRACE_PERIOD_SECONDS300Max time for graceful shutdown and prediction timeout
WORLD_SIZE1Number of GPU processes (set automatically by torchrun)

Complete Examples

Image Classification

Video Generation with File Output

Multi-Model Pipeline