Skip to content

Conversation

rhatdan
Copy link
Member

@rhatdan rhatdan commented Jul 21, 2025

Partial fix: #1568

Summary by Sourcery

Bug Fixes:

  • Move Engine initialization in generate() to after checking args.container and args.engine to prevent premature creation with invalid inputs

Partial fix: containers#1568

Signed-off-by: Daniel J Walsh <[email protected]>
Copy link
Contributor

sourcery-ai bot commented Jul 21, 2025

Reviewer's Guide

Engine instantiation in generate() is deferred until after argument validation to avoid creating the Engine prematurely and ensure required options are present.

Sequence diagram for deferred Engine instantiation in generate()

sequenceDiagram
    participant User as actor User
    participant Rag as Rag.generate()
    participant Engine as Engine

    User->>Rag: Call generate(args)
    Rag->>Rag: Set args.nocapdrop = True
    Rag->>Rag: Check if args.container is present
    alt args.container missing
        Rag-->>User: Raise KeyError
    else args.container present
        Rag->>Rag: Check if args.engine is present
        alt args.engine missing
            Rag-->>User: Raise KeyError
        else args.engine present
            Rag->>Engine: Instantiate Engine(args)
        end
    end
Loading

Class diagram for updated Engine instantiation in Rag

classDiagram
    class Rag {
        +generate(args)
        -engine: Engine
    }
    class Engine {
        +Engine(args)
    }
    Rag --> Engine : uses
Loading

File-Level Changes

Change Details Files
Defer Engine initialization until after argument validation
  • Removed early self.engine = Engine(args) call
  • Added self.engine = Engine(args) after container and engine option checks
ramalama/rag.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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.

Summary of Changes

Hello @rhatdan, 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 addresses a logical flow issue in the ramalama/rag.py file by ensuring that the Engine object is initialized only after critical input validation checks have been performed. This change prevents the unnecessary creation of an engine instance when essential arguments are missing, thereby improving the robustness and error handling of the generate method.

Highlights

  • Engine Initialization Order: The instantiation of the Engine object within the generate method has been reordered. It now occurs after the validation checks for args.container and args.engine, ensuring that the engine is only created if these required arguments are present.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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 @rhatdan - I've reviewed your changes - here's some feedback:

  • Consider extracting the container and engine argument checks into a separate validation method to keep generate() focused on orchestration.
  • KeyError is usually for missing dict keys—using a ValueError or a dedicated exception type for invalid CLI arguments would be more semantically clear.
  • Since you moved Engine instantiation, double‐check and document any side effects in Engine.init to prevent unexpected behavior changes.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the container and engine argument checks into a separate validation method to keep generate() focused on orchestration.
- KeyError is usually for missing dict keys—using a ValueError or a dedicated exception type for invalid CLI arguments would be more semantically clear.
- Since you moved Engine instantiation, double‐check and document any side effects in Engine.__init__ to prevent unexpected behavior changes.

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.

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 correctly moves the Engine initialization to after the validation checks, which is a good practice to prevent unnecessary object creation with invalid arguments. The change is sound.

I've added one suggestion to improve the clarity and conciseness of the validation logic that is part of the changed code block.

Comment on lines 80 to 81
if not args.engine or args.engine == "":
raise KeyError("rag command requires a container. Can not be run without a container engine.")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The check for args.engine can be made more idiomatic and the error message can be clarified.

  1. The condition if not args.engine or args.engine == "" can be simplified to if not args.engine:, as empty strings are falsy in Python and this is more concise.
  2. The error message "rag command requires a container. Can not be run without a container engine." appears to be partially copied from the check above it. A more precise message would improve clarity for the user.

Here is a suggested improvement:

Suggested change
if not args.engine or args.engine == "":
raise KeyError("rag command requires a container. Can not be run without a container engine.")
if not args.engine:
raise KeyError("rag command requires a container engine to be specified.")

Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion seems valid (for a follow-up)

Copy link
Contributor

@olliewalsh olliewalsh left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 80 to 81
if not args.engine or args.engine == "":
raise KeyError("rag command requires a container. Can not be run without a container engine.")
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion seems valid (for a follow-up)

@rhatdan rhatdan merged commit 368e702 into containers:main Jul 21, 2025
24 of 57 checks passed
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.

ramalama rag and ramalama run not working
2 participants