Skip to content

fix(cli): ensure create_crew respects --provider flag #2499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

theCyberTech
Copy link
Collaborator

Title: fix(cli): Respect --provider flag in create_crew command

Description:

This PR addresses an issue where the crewai create crew command incorrectly prompted the user for provider selection, even when a valid provider was specified using the --provider flag.

Changes:

Modified the create_crew function in src/crewai/cli/create_crew.py to check for the --provider argument before initiating interactive selection.
If a valid provider is passed via the flag, it is used directly, skipping the prompt.
If no provider is passed, or an invalid one is specified, the existing logic (checking environment variables or interactive selection) proceeds as normal.
Added new tests in tests/cli/test_create_crew.py to cover scenarios involving the --provider flag, ensuring the fix works as expected and preventing regressions.

@joaomdmoura
Copy link
Collaborator

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comment for PR #2499

Summary

This PR enhances the crew creation CLI functionality to properly manage the --provider flag and enriches test coverage, impacting src/crewai/cli/create_crew.py and introducing tests/cli/test_create_crew.py.

Code Quality Improvements

1. Function Complexity

The create_crew function is lengthy and manages multiple tasks. This can lead to difficulties in maintenance and understanding. Consider breaking it down into smaller functions, such as:

def handle_provider_selection(provider, provider_models, env_vars):
    # logic for selecting the provider

This makes the code easier to test and reduces cognitive load on future maintainers.

2. Error Handling

The current implementation could benefit from improved error handling for API key configurations. Instead of using generic exceptions, define specific error handling paths that cover various failure scenarios:

try:
    # Logic for configuring provider keys
except KeyError as ke:
    click.secho(f"Missing key: {str(ke)}", fg="red")

3. Constants and Configuration

Hardcoded strings should be moved to a dedicated constants file to enhance clarity and maintainability:

ERROR_MESSAGES = {
    "PROVIDER_NOT_FOUND": "Could not retrieve provider data.",
    "PROVIDER_SELECTION_FAILED": "Provider selection failed.",
}

Tests Review

Positive Aspects

  • The tests demonstrate thorough coverage of major scenarios.
  • They utilize fixtures and mocking effectively, improving test isolation.
  • Names and docstrings of the tests are clear, providing instant context about their purpose.

Suggested Additional Test Cases

Consider adding tests for invalid inputs and error scenarios to further validate reliability:

@pytest.mark.parametrize("invalid_input", ["", " ", "test@crew"])
def test_create_crew_invalid_name(invalid_input, runner):
    """Test crew creation with invalid name inputs"""

General Recommendations

  1. Documentation:
    Ensure the top-level functions like create_crew contain docstrings to clarify their functionalities and parameters:
def create_crew(name: str, provider: Optional[str] = None):
    """
    Create a new crew with specified configuration.
    ...
    """
  1. Type Hints:
    Add type hints throughout the codebase to enhance readability and maintainability.

  2. Structured Logging:
    Introduce logging to replace some click outputs. For instance:

import logging
logger = logging.getLogger(__name__)
logger.info("Creating crew: %s", name)
  1. Separate Constants File:
    Create a separate file for constants management, improving organization and reducing redundancy.

Overall, the adjustments proposed in this review aim to bolster the maintainability and reliability of the code and ensure enhanced test coverage to handle edge cases and error scenarios effectively. Improvements in documentation and logging will streamline future enhancements and debugging efforts.

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.

3 participants