Skip to content

server_keys

Utilities for managing ephemeral server keys in a FastAPI application.

Warning

Under almost no circumstances should you need to import this module directly. If stainedglass_output_protection is installed, and you launch vLLM via the alternative entrypoint, this module will be automatically applied.

Functions:

Name Description
ephemeral_key_lifespan_factory

Create a FastAPI lifespan async context manager that wraps an existing lifespan but also adds functionality to

inject_key_lifespan

Add the ephemeral_key_lifespan to a function that builds a vLLM OpenAI-compatible FastAPI application.

periodic_refresh

Periodically refresh the ephemeral key.

ephemeral_key_lifespan_factory

ephemeral_key_lifespan_factory(
    refresh_time: float,
) -> Callable[
    [
        Callable[
            [fastapi.FastAPI],
            contextlib.AbstractAsyncContextManager,
        ]
    ],
    Callable[
        [fastapi.FastAPI],
        contextlib.AbstractAsyncContextManager,
    ],
]

Create a FastAPI lifespan async context manager that wraps an existing lifespan but also adds functionality to periodically refresh the ephemeral keys.

Parameters:

Name Type Description Default

refresh_time

float

Time in seconds to wait between refreshes.

required

Returns:

Type Description
Callable[[Callable[[fastapi.FastAPI], contextlib.AbstractAsyncContextManager]], Callable[[fastapi.FastAPI], contextlib.AbstractAsyncContextManager]]

Wrapped lifespan async context manager.

inject_key_lifespan

inject_key_lifespan(
    build_app_func: BuildAppFunc, key_refresh_time_s: float
) -> middleware.BuildAppFunc

Add the ephemeral_key_lifespan to a function that builds a vLLM OpenAI-compatible FastAPI application.

Parameters:

Name Type Description Default

build_app_func

BuildAppFunc

Function that builds the vLLM OpenAI-compatible FastAPI application

required

key_refresh_time_s

float

Time in seconds to wait between ephemeral key refreshes.

required

Returns:

Type Description
middleware.BuildAppFunc

A new function compatible with the same signature as build_app_func that also adds the ephemeral_key_lifespan after using

middleware.BuildAppFunc

build_app_func to build the app.

periodic_refresh async

periodic_refresh(app: FastAPI, period: float) -> None

Periodically refresh the ephemeral key.

Parameters:

Name Type Description Default

app

FastAPI

FastAPI application whose state will hold the ephemeral key pair.

required

period

float

Time in seconds to wait between refreshes.

required