Skip to main content

generation.generate()

Generates text from a prompt. Runs remotely when API or JWT credentials are configured. Runs locally when mx.set_execution_mode("local") is set, or in auto mode when a local model is loaded and no credentials are configured.

Core Parameters

string
required
The input text to generate a continuation for.
integer
default:"128"
Maximum number of tokens to generate.
string
default:"top-k"
Token sampling strategy. See Sampling Methods below.
float
default:"0.7"
Controls randomness. Lower values (0.1-0.5) produce more focused output; higher values (0.8-1.2) increase creativity.

Sampling Parameters

integer
default:"50"
For top-k sampling: number of top tokens to sample from.
float
default:"0.9"
For top-p sampling: cumulative probability threshold (0.0-1.0).
float
For min-p sampling: minimum relative probability threshold.
float
For typical sampling: typical probability threshold.
integer
For ads sampling: number of candidate tokens per step (2-10).
float
For ads sampling: quality vs diversity balance (0.1-0.5).

Steering Parameters

string | dict
A steering vector ID string (remote) or a {layer_index: tensor} dict (local).
float
default:"0"
Multiplier for the steering vector magnitude.
string
A named preset (e.g., "brevity", "truthfulness"). Applied as a pre-configured steering configuration.

Constrained Generation

dict
JSON schema to constrain output format. Used with guided-generation sampling.
string
Regex pattern to constrain output. Used with guided-generation sampling.
string
Grammar specification to constrain output. Used with guided-generation sampling.

Advanced Parameters

string
Model name for speculative decoding (ssd sampling method).
list[str]
List of model names for ensemble-sampling.
integer
default:"1"
Number of candidates to generate and score when using the policy-backed path.
boolean
default:"false"
Enables adaptive temperature during retries.
list[float]
Temperature values to use across retry rounds. Setting this also enables adaptive temperature.
boolean
default:"false"
Enables adaptive nucleus sampling during retries.
list[float]
Top-p values to use across retry rounds. Setting this also enables adaptive top-p.
boolean
default:"false"
Regenerates when the selected candidate’s confidence falls below confidence_threshold.
float
default:"0.5"
Minimum candidate confidence when confidence-triggered regeneration is enabled.
list[str]
Python unit-test snippets used by the policy verifier for generated code.
dict
Inline policy configuration. See Policies.
string
ID of a saved policy to apply during generation.
boolean
default:"false"
Requests trace information from policy execution. generation.generate() still returns the output string.
Returns: A plain string containing the generated text.

Sampling Methods

Applying a Steering Vector

Pass a vector ID (returned from steering.generate_vectors() or created in Spectra) or a local {layer: tensor} dict:
For local generation with a custom vector:

Local vs Remote

Remote failures are surfaced when auto mode resolves to remote. Call mx.set_execution_mode("local") to force a loaded local model. Plain local generation supports greedy, top-k, top-p, min-p, and typical. Policy-backed local generation can also run saved policies, constraints, retries, and verifiers. ADS and steering perceptrons are not supported locally.