Skip to content

Conversation

@doringeman
Copy link
Contributor

Add preload support to reduce first-request latency for slow-starting backends like vLLM.

It works by having configure, which is also used by Compose, call handleOpenAIInference with a preloadOnly context key in order to stop right after loading the model. The CLI side calls handleOpenAIInference directly with X-Preload-Only set.

Fixes #623.


To test:

MODEL_RUNNER_PORT=8080 make run
$ cat compose.yaml
services:
  alpine:
    image: alpine
    models:
      - llm

models:
  llm:
    model: ai/gpt-oss
MODEL_RUNNER_HOST=http://localhost:8080 docker compose up && MODEL_RUNNER_HOST=http://localhost:8080 docker model run gpt-oss hi

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • The preloadOnlyKey used with context.WithValue is a plain string, which risks collisions with other context values; consider defining a dedicated unexported type (e.g. type preloadOnlyKeyType struct{}) and using a value of that type as the key.
  • In the Configure handler’s preload goroutine, errors from json.Marshal and http.NewRequestWithContext are ignored; even if the preload is best-effort, it would be helpful to at least log these failures so issues with model preloading are diagnosable.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `preloadOnlyKey` used with `context.WithValue` is a plain string, which risks collisions with other context values; consider defining a dedicated unexported type (e.g. `type preloadOnlyKeyType struct{}`) and using a value of that type as the key.
- In the `Configure` handler’s preload goroutine, errors from `json.Marshal` and `http.NewRequestWithContext` are ignored; even if the preload is best-effort, it would be helpful to at least log these failures so issues with model preloading are diagnosable.

## Individual Comments

### Comment 1
<location> `pkg/inference/scheduling/http_handler.go:24-28` </location>
<code_context>
 	"github.com/docker/model-runner/pkg/middleware"
 )

+const preloadOnlyKey string = "preloadOnly"
+
 // HTTPHandler handles HTTP requests for the scheduler.
</code_context>

<issue_to_address>
**issue (bug_risk):** Use a dedicated context key type instead of a plain string for `preloadOnlyKey`.

A plain string context key can collide with keys from other packages (including stdlib/middleware). Define a private key type instead (e.g. `type preloadOnlyKeyType struct{}` with `var preloadOnlyKey preloadOnlyKeyType`) to make this key uniquely identifiable while still supporting the `X-Preload-Only` header path.
</issue_to_address>

### Comment 2
<location> `pkg/inference/scheduling/http_handler.go:382-391` </location>
<code_context>

+	// Preload the model in the background by calling handleOpenAIInference with preload-only context.
+	// This makes Compose preload the model as well as it calls `configure` by default.
+	go func() {
+		preloadBody, _ := json.Marshal(OpenAIInferenceRequest{Model: configureRequest.Model})
+		ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
+		defer cancel()
+		preloadReq, _ := http.NewRequestWithContext(
+			context.WithValue(ctx, preloadOnlyKey, true),
+			http.MethodPost,
+			inference.InferencePrefix+"/v1/chat/completions",
+			bytes.NewReader(preloadBody),
+		)
+		preloadReq.Header.Set("User-Agent", r.UserAgent())
+		if backend != nil {
+			preloadReq.SetPathValue("backend", backend.Name())
+		}
+		h.handleOpenAIInference(httptest.NewRecorder(), preloadReq)
+	}()
+
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Avoid silently discarding errors when building the preload request in the goroutine.

`json.Marshal` and `http.NewRequestWithContext` both ignore their errors, so any failure just skips the preload with no signal. Even for best-effort behavior, we should at least log these errors (or return early) so malformed config or context issues are visible during startup.
</issue_to_address>

### Comment 3
<location> `cmd/cli/commands/run.go:763-765` </location>
<code_context>
 				return nil
 			}

+			// For interactive mode, eagerly load the model in the background
+			// while the user types their first query
+			go func() {
+				_ = desktopClient.Preload(context.Background(), model)
+			}()
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Consider using `cmd.Context()` instead of `context.Background()` for the interactive preload goroutine.

Using `context.Background()` lets the preload continue even after the CLI command exits (for example, if the user cancels quickly). Passing `cmd.Context()` (or a derived context) into `Preload` would ensure the preload is cancelled with the command lifecycle instead.

Suggested implementation:

```golang
			// For interactive mode, eagerly load the model in the background
			// while the user types their first query
			go func() {
				_ = desktopClient.Preload(cmd.Context(), model)
			}()

```

If `context` is only imported for this call site elsewhere in the file, you can remove the unused `context` import after this change. Otherwise, no additional code changes are required.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @doringeman, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a mechanism to preload models into memory, addressing the issue of high first-request latency for backends like vLLM. It achieves this by adding a Preload function and integrating it into the CLI, interactive mode, and the configure command. The changes ensure that models are loaded in advance, improving the responsiveness of the system.

Highlights

  • Preload Functionality: Introduces a Preload function to load models into memory without running inference, reducing first-request latency, especially for slow-starting backends like vLLM.
  • CLI Changes: Modifies the CLI to use the new Preload function when the --detach flag is used, ensuring the model is loaded in the background.
  • Interactive Mode Enhancement: Improves interactive mode by eagerly loading the model in the background while the user types their first query, enhancing the user experience.
  • Configure Integration: Integrates preload functionality into the configure command, which is also used by Compose, to preload models during configuration.
  • HTTP Handler Modification: Updates the HTTP handler to support a preloadOnly context key and X-Preload-Only header to stop request processing immediately after loading the model.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • cmd/cli/commands/run.go
    • Modified to use the Preload function when the --detach flag is used.
    • Added background model loading for interactive mode.
  • cmd/cli/desktop/desktop.go
    • Introduced the Preload function to load models without running inference.
  • pkg/inference/scheduling/http_handler.go
    • Added support for preloadOnly context key and X-Preload-Only header to handle preload-only requests.
    • Integrated preload functionality into the Configure function.
Activity
  • Introduced Preload function in cmd/cli/desktop/desktop.go.
  • Modified cmd/cli/commands/run.go to use Preload for --detach and interactive mode.
  • Updated pkg/inference/scheduling/http_handler.go to support preload-only requests and integrate with Configure.
  • Added test cases to verify the new functionality.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a model preloading feature to improve first-request latency, which is a valuable enhancement for user experience. The implementation is well-structured, adding a Preload method to the client and using a special header/context key on the backend to short-circuit inference after model loading. My review focuses on improving error handling in a few areas, particularly for background tasks where failures could otherwise go unnoticed. Overall, the changes are solid and well-thought-out.

Add preload support to reduce first-request latency for slow-starting backends like vLLM.

It works by having `configure`, which is also used by Compose, call `handleOpenAIInference` with a `preloadOnly` context key in order to stop right after loading the model.
The CLI side calls `handleOpenAIInference` directly with `X-Preload-Only` set.

Fixes docker#623.

Signed-off-by: Dorin Geman <dorin.geman@docker.com>
@doringeman doringeman force-pushed the preload-optimization branch from bbacaad to bdf158b Compare February 4, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"docker model run" optimization

1 participant