SamplingClient generates text completions from a base model or published LoRA weights. Create one with ServiceClient.create_sampling_client for a standalone sampler, or with TrainingClient.save_weights_and_get_sampling_client for the run’s paired sampler. Both expose the same sample method.
Sampler deployments pool HTTP connections, up to 2048 by default. On a VM with a tight port budget, such as behind Cloud NAT, lower the pool with BT_LOOPS_MAX_CONNECTIONS. Serverless samplers multiplex requests over HTTP/2.
Generate a completion from the weights the client serves:
Generate
num_samples completions from prompt (a ModelInput). Pass a SamplingParams instance to control temperature, top-p, top-k, max tokens, seed, stop sequences, and structured outputs; omit it to use defaults. Set include_prompt_logprobs=True to return per-token log-probabilities for the input tokens alongside the output, and set topk_prompt_logprobs above 0 to also return the top-k alternatives at each prompt position. The client selects the base model and adapter, so there’s no per-call model override.Before the first request to a sampler deployment, the SDK waits for the deployment to be ACTIVE. A scaled-to-zero sampler wakes once, and a deployment in a terminal-failure state raises SamplerTerminatedError. If your API key can’t read deployment status, the SDK skips the check and sampler failures surface as request errors. Serverless samplers have no deployment to poll.timeout on ServiceClient.create_sampling_client and sampler_timeout on create_lora_training_client set the read timeout for a single sample() call. Without one, the request waits until the platform’s sampling timeout returns a 504. Set a timeout to raise SampleTimeoutError earlier; the SDK doesn’t retry the generation, and for serverless samplers it also tries to cancel queued work. A timeout doesn’t deactivate the run or sampler.For sampler deployments, sample() retries transient 503 responses, and 408 responses from replicas below the requested policy version, within ready_timeout. A 504 isn’t retried.For serverless samplers, the SDK submits a queued operation and polls its result. It retries transient 408, 502, and 504 responses, and a 503 only when the response includes Retry-After. Gateway rejections and invalid requests fail without readiness retries. See Handle SDK request errors.affinity_key (keyword-only) is sent as the X-Baseten-Session-ID header, so deployments with sticky routing send related requests to the same sampler replica and keep a rollout group’s shared prompt in one replica’s prefix cache. Reuse one key across every rollout and turn in a rollout group; don’t reuse one key for an entire run. The key must be a non-empty string.include_routed_experts controls expert-route capture on router replay models. Pass True to capture the routes with each recorded sample, which requires a client created with record_stability_info=True, or False to opt out. The default, None, leaves the sampler’s configured behavior unchanged.rollout_id (keyword-only, 1-80 visible ASCII characters) identifies the rollout this call belongs to. The SDK sends it as the X-Rollout-Id header for tracing; it doesn’t affect routing. Pass the same ID on every turn of a rollout; if you omit it, the request isn’t attributed to a rollout.Return the per-token log-probabilities for
prompt without generating new tokens. Index 0 is always None because the first token has no preceding context to score against. Other positions may also be None if the sampler can’t compute a log-probability for that token. affinity_key works as in sample(); scoring a prompt is a full prefill, so passing the rollout group’s key routes the request to the replica whose prefix cache already holds the group’s shared prompt.Return the base model ID. For serverless samplers, this is the model the client was created for. For sampler deployments, the method reads the
/v1/models entry with no parent and retries with backoff while the sampler deploys.Return the adapter this client samples from, or
None for a base-model client. For serverless samplers, this is the checkpoint path. For sampler deployments, it’s the first /v1/models entry with a non-null parent, or None if no adapter is loaded.Property. The ID of the Loops sampler this client calls, or
None for a local client. Pass it to ServiceClient.deactivate_sampler() or the Deactivate a sampler API.Property. The
bt:// checkpoint path this client is pinned to, or None when it serves the base model. For a serverless sampler, this is the unique adapter name the publishing call minted ({name}-<uuid>), not the name that call was given.Property. The name
checkpoint_path was published under, or None. Pass it to get_checkpoint_archive_url(name=...) or download_checkpoint(name=...) for the save that created this client. On serverless capacity it can differ from the name you passed to save_weights_and_get_sampling_client.Return the Hugging Face
PreTrainedTokenizer for the base model, or for an hf:// base checkpoint at its pinned commit.Block until a sampler deployment’s status is
ACTIVE. A scaled-to-zero sampler triggers one wake; terminal-failure states raise SamplerTerminatedError. No-op for local and serverless samplers, which have no deployment status to poll.Class method. Block until
deployment reports ready, using a throwaway SamplingClient so you can wait without holding one. Polls up to ready_timeout seconds and applies the same readiness semantics as ensure_ready.Close the client’s HTTP connections and local event-loop resources. This doesn’t deactivate the sampler. To end a paired sampler, deactivate its run. To end a standalone sampler, call
deactivate().Shut down this standalone sampler, which stops its billing. Calls the Deactivate a sampler API. Succeeds if the sampler is already inactive, and works before or after
close(). Raises RuntimeError for a client with no sampler ID, such as a local client. A sampler paired to a run stops with its run: the server refuses it with a 409 error that names the run.