Skip to content

Comments

De-deduplicate code#704

Closed
ericcurtin wants to merge 1 commit intomainfrom
deduplicate
Closed

De-deduplicate code#704
ericcurtin wants to merge 1 commit intomainfrom
deduplicate

Conversation

@ericcurtin
Copy link
Contributor

Particularly python stuff

Particularly python stuff

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ericcurtin, 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 significantly refactors the handling of Python executable discovery and command execution within the inference backends. By extracting these common functionalities into a new, shared utility file, it eliminates redundant code present in multiple backend implementations. This change streamlines the process of interacting with Python environments, ensuring consistency and simplifying future maintenance for Python-dependent components.

Highlights

  • Centralized Python Utilities: Introduced a new python.go file in the backends package to centralize common Python-related utility functions, including FindPythonPath for locating Python executables and NewPythonCmd for creating Python command execution objects.
  • Code Deduplication: Removed redundant Python path discovery logic and local pythonCmd helper functions from diffusers.go, mlx.go, and sglang.go by leveraging the newly introduced centralized utilities.
  • Improved Maintainability: Refactored the codebase to reduce duplication, making it easier to manage and update Python-related operations across different inference backends.

🧠 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
  • pkg/inference/backends/diffusers/diffusers.go
    • Removed os/exec and path/filepath imports.
    • Removed the ErrPythonNotFound error definition.
    • Updated Python path discovery to use the new backends.FindPythonPath function.
    • Updated Python command execution to use the new backends.NewPythonCmd function.
    • Removed the deprecated pythonCmd helper method.
  • pkg/inference/backends/mlx/mlx.go
    • Updated Python path discovery to use the new backends.FindPythonPath function.
  • pkg/inference/backends/python.go
    • Added a new file containing ErrPythonNotFound, FindPythonPath, and NewPythonCmd functions to centralize Python utility logic.
  • pkg/inference/backends/sglang/sglang.go
    • Removed os/exec and path/filepath imports.
    • Removed the ErrPythonNotFound error definition.
    • Updated Python path discovery to use the new backends.FindPythonPath function.
    • Updated Python command execution to use the new backends.NewPythonCmd function.
    • Removed the deprecated pythonCmd helper method.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
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

@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 1 issue, and left some high level feedback:

  • In mlx.Install, you now always map FindPythonPath failures to ErrStatusNotFound; consider propagating backends.ErrPythonNotFound (and/or using its message for status) so callers can distinguish between a missing Python binary and other MLX-related setup errors.
  • Since FindPythonPath and NewPythonCmd are now shared utilities, it may be worth updating any other backends that manually resolve Python and spawn commands to use these helpers for consistent behavior and error handling across backends.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `mlx.Install`, you now always map `FindPythonPath` failures to `ErrStatusNotFound`; consider propagating `backends.ErrPythonNotFound` (and/or using its message for `status`) so callers can distinguish between a missing Python binary and other MLX-related setup errors.
- Since `FindPythonPath` and `NewPythonCmd` are now shared utilities, it may be worth updating any other backends that manually resolve Python and spawn commands to use these helpers for consistent behavior and error handling across backends.

## Individual Comments

### Comment 1
<location> `pkg/inference/backends/mlx/mlx.go:83-88` </location>
<code_context>
-			m.status = ErrStatusNotFound.Error()
-			return ErrStatusNotFound
-		}
+	pythonPath, err := backends.FindPythonPath(m.customPythonPath, "")
+	if err != nil {
+		m.status = ErrStatusNotFound.Error()
+		return ErrStatusNotFound
</code_context>

<issue_to_address>
**suggestion:** Preserve the underlying Python resolution error instead of always mapping to ErrStatusNotFound

Now that `FindPythonPath` can return a more specific `ErrPythonNotFound`, consider either propagating `err` directly (as in the diffusers/sglang backends) or setting `m.status = err.Error()` instead of `ErrStatusNotFound.Error()`. This would preserve the more detailed failure information (e.g., “MLX not available” vs. “python3 not found”), improving diagnosis of misconfigured environments.

```suggestion
	pythonPath, err := backends.FindPythonPath(m.customPythonPath, "")
	if err != nil {
		m.status = err.Error()
		return err
	}
	m.pythonPath = pythonPath
```
</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.

Comment on lines +83 to 88
pythonPath, err := backends.FindPythonPath(m.customPythonPath, "")
if err != nil {
m.status = ErrStatusNotFound.Error()
return ErrStatusNotFound
}

// Store the python path for later use
m.pythonPath = pythonPath
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Preserve the underlying Python resolution error instead of always mapping to ErrStatusNotFound

Now that FindPythonPath can return a more specific ErrPythonNotFound, consider either propagating err directly (as in the diffusers/sglang backends) or setting m.status = err.Error() instead of ErrStatusNotFound.Error(). This would preserve the more detailed failure information (e.g., “MLX not available” vs. “python3 not found”), improving diagnosis of misconfigured environments.

Suggested change
pythonPath, err := backends.FindPythonPath(m.customPythonPath, "")
if err != nil {
m.status = ErrStatusNotFound.Error()
return ErrStatusNotFound
}
// Store the python path for later use
m.pythonPath = pythonPath
pythonPath, err := backends.FindPythonPath(m.customPythonPath, "")
if err != nil {
m.status = err.Error()
return err
}
m.pythonPath = pythonPath

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

The pull request successfully de-duplicates Python-related logic across the diffusers, mlx, and sglang backends by introducing a centralized FindPythonPath helper and a NewPythonCmd utility in the backends package. This refactoring improves maintainability and ensures consistent Python discovery behavior across different inference engines. The implementation correctly handles custom Python paths, virtual environments, and system-wide fallbacks while preserving existing error contracts.

Comment on lines +3 to +8
import (
"errors"
"os"
"os/exec"
"path/filepath"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Add the context package to the imports to support context-aware command creation helpers.

Suggested change
import (
"errors"
"os"
"os/exec"
"path/filepath"
)
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
)

Comment on lines +36 to +42
func NewPythonCmd(pythonPath string, args ...string) *exec.Cmd {
binary := "python3"
if pythonPath != "" {
binary = pythonPath
}
return exec.Command(binary, args...)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a context-aware version of this helper (e.g., NewPythonCmdContext) that uses exec.CommandContext. Since the Install methods in the backends already receive a context.Context, leveraging it for these check commands would allow for better process management and cancellation support during the installation phase.

@ericcurtin ericcurtin closed this Feb 20, 2026
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.

1 participant