New Tool Radar

Local AI

Run vLLM Offline Inference from a Python Script

Use the vLLM LLM class in Python to run a list of prompts through a model in one batch, without starting a server.

Schematic with three steps: Write the batch script; Use the chat interface for instruct models; Run it and read the outputs.

For a fixed list of prompts, vLLM offline inference is the fastest path: the LLM class loads the model once and processes all prompts together, keeping the GPU busy, instead of serving one request at a time through HTTP.

What you need

  • vLLM installed on a Linux machine with an NVIDIA GPU
  • Python 3.10 to 3.13 in the vLLM environment
  • A model name, such as facebook/opt-125m for a first test

Step 1: Write the batch script

Create batch_inference.py with: from vllm import LLM, SamplingParams; a prompts list; sampling_params = SamplingParams(temperature=0.8, top_p=0.95); llm = LLM(model=’facebook/opt-125m’); outputs = llm.generate(prompts, sampling_params). The LLM class is the main class for offline inference, and SamplingParams sets the sampling process.

Step 2: Use the chat interface for instruct models

For a chat model, build a list of message lists and call llm.chat instead of llm.generate. llm.chat applies the model chat template, while llm.generate does not apply it automatically, so raw prompts need the template applied by hand.

Step 3: Run it and read the outputs

Run python batch_inference.py. Each output object holds the prompt and the generated text in outputs[0].text. Print both together so the result stays matched to its prompt.

Worked example: four prompts through OPT-125M

The table below is a synthetic example. The script shape and the example prompts come from the vLLM quickstart; the model is its documented example.

Part Value Note
Model facebook/opt-125m Small test model
Sampling temperature 0.8, top_p 0.95 From the quickstart
Call llm.generate(prompts, sampling_params) Batch, one load

Expected result (UNTESTED). the script prints each prompt with its generated text, and all prompts finish in one model load. This check was not run live; the script shape comes from the cited vLLM docs.

Limits

  • Offline inference loads one model per script run.
  • llm.generate does not apply a chat template; use llm.chat for instruct models.
  • By default vLLM applies the model generation_config.json when one exists.
  • Batch jobs need enough GPU memory for weights plus KV cache for all prompts.

Sources

See all