Skip to main content

Runtime

The Runtime class is a wrapper for launching the SGLang HTTP server programmatically from Python. It’s primarily used with the SGLang frontend language.
For offline processing without the frontend language, use the Engine class instead.

RuntimeEndpoint

The RuntimeEndpoint class provides a client interface to communicate with a running SGLang server.

Initialization

str
required
Base URL of the SGLang server.
Optional[str]
default:"None"
API key for authentication.
Optional[str]
default:"None"
SSL certificate verification path.
Optional[str]
default:"None"
Name of the chat template to use. Auto-detected from model if not specified.

Methods

get_model_name

Get the model path/name from the server.

get_server_info

Get server configuration and status information.

flush_cache

Flush the KV cache on the server.

cache_prefix

Pre-cache a prefix string in the KV cache.

start_profile / stop_profile

Start and stop server profiling.

Runtime

The Runtime class launches an HTTP server in a separate process and provides an endpoint to interact with it.

Initialization

str
required
Path to the model on Hugging Face or local filesystem. See ServerArgs for more details.
str
default:"error"
Log level for the server. Options: “debug”, “info”, “warning”, “error”.
float
default:"300.0"
Timeout in seconds for waiting for the server to start.
Additional keyword arguments are passed to ServerArgs.

Attributes

str
Base URL of the launched server (e.g., “http://127.0.0.1:30000”).
str
Full URL for the generate endpoint.
RuntimeEndpoint
RuntimeEndpoint instance for interacting with the server.

Methods

generate

Synchronous text generation.
Union[str, List[str]]
required
Input prompt(s).
Optional[Dict]
Sampling parameters dictionary.
Optional[Union[List[bool], bool]]
default:"False"
Whether to return log probabilities.
Optional[List[Optional[str]]]
LoRA adapter path(s) for each request.
Returns: str - JSON string containing the response

async_generate

Asynchronous streaming text generation.
Alias: add_request can also be used for async_generate.

encode

Generate embeddings.
Union[str, List[str], List[Dict], List[List[Dict]]]
required
Text to encode.
Returns: str - JSON string containing embeddings

get_server_info

Get server information asynchronously.
Returns: Dict - Server information dictionary

get_tokenizer

Get the tokenizer used by the server.
Returns: Hugging Face tokenizer instance

start_profile / stop_profile

Start and stop server profiling.

cache_prefix

Pre-cache a prefix string.

shutdown

Shutdown the server and clean up resources.
The shutdown method is automatically called when the Runtime object is deleted or when the Python program terminates.

Usage Examples

Basic Server Launch

Streaming with Async

Using RuntimeEndpoint with Existing Server

Batch Requests

Using with Frontend Language (SGLang)

Differences from Engine

Use Engine for offline batch processing and Runtime when you need:
  • An HTTP server for multiple clients
  • Integration with the SGLang frontend language
  • Remote access to the model

See Also