Skip to content

Conversation

@amanjaiswal73892
Copy link
Collaborator

@amanjaiswal73892 amanjaiswal73892 commented Aug 25, 2025

  • Make langChain dependency optional.

This pull request improves the handling of optional dependencies on langchain_community throughout the codebase. The main changes ensure that the code can run even if langchain_community is not installed, by using dynamic imports and adding graceful fallbacks.

Dependency management and import handling:

  • Removed langchain_community from the required dependencies in pyproject.toml, making it optional.
  • Updated both src/agentlab/llm/llm_utils.py and src/agentlab/llm/tracking.py to use importlib.util.find_spec for checking the presence of langchain_community, and conditionally import related modules or set them to None if unavailable. [1] [2] [3] [4]

Graceful degradation and error handling:

  • Modified functions that use langchain_community objects (such as messages_to_dict in llm_utils.py and pricing functions in tracking.py) to check for the presence of these dependencies and handle their absence gracefully, including logging warnings and returning empty results as needed. [1] [2] [3]

Description by Korbit AI

What change is being made?

Remove the langchain dependency and make the code robust to operate without it by implementing conditional imports and handling import exceptions.

Why are these changes being made?

This change improves the modularity and flexibility of the code by avoiding a hard dependency on langchain, allowing the application to function without it if necessary. This approach anticipates scenarios where users might not have langchain installed and provides fallbacks for critical functionalities, ensuring a resilient system.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

Copy link

@korbit-ai korbit-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.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Error Handling Over-broad exception handling ▹ view
Files scanned
File Path Reviewed
src/agentlab/llm/tracking.py
src/agentlab/llm/llm_utils.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment on lines +143 to +146
try:
input_cost_dict = bedrock_anthropic_callback.MODEL_COST_PER_1K_INPUT_TOKENS
output_cost_dict = bedrock_anthropic_callback.MODEL_COST_PER_1K_OUTPUT_TOKENS
except Exception as e:
Copy link

Choose a reason for hiding this comment

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

Over-broad exception handling category Error Handling

Tell me more
What is the issue?

Using a broad Exception catch that could mask specific errors like AttributeError when bedrock_anthropic_callback is None.

Why this matters

Different error types require different handling strategies. Catching specific exceptions helps identify and handle issues appropriately.

Suggested change ∙ Feature Preview
try:
    if bedrock_anthropic_callback is None:
        raise ImportError("langchain_community not installed")
    input_cost_dict = bedrock_anthropic_callback.MODEL_COST_PER_1K_INPUT_TOKENS
    output_cost_dict = bedrock_anthropic_callback.MODEL_COST_PER_1K_OUTPUT_TOKENS
except (ImportError, AttributeError) as e:
    logging.warning(
        f"Failed to get Anthropic pricing: {e}. "
        "Please install langchain-community or use LiteLLM API for pricing information."
    )
    return {}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

@recursix recursix merged commit f000d62 into main Aug 25, 2025
7 checks passed
@recursix recursix deleted the remove-langchain-deps branch August 25, 2025 20:11
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