Skip to content

Conversation

@JashG
Copy link
Contributor

@JashG JashG commented Nov 26, 2025

Description

init_langchain_model() attempts to initialize a model in a specific order. If no methods succeed, it returns the last exception thrown. This can mask the actual exception that is most relevant to the consumer.

For example, the NeMo Guardrails MS internally uses a custom chat client for main models. If there's an initialization error thrown by _init_community_chat_models, it always gets masked by _init_text_completion_model, which throws an exception when a provider can't be found.

This PR updates the init_* functions to instead return None if a provider can't be found. This way, if a previous init_* function threw an exception as a result of actually invoking a provider class, it gets preserved.

Related Issue(s)

Checklist

  • I've read the CONTRIBUTING guidelines.
  • I've updated the documentation if applicable.
  • I've added tests if applicable.
  • @mentions of the person or team responsible for reviewing proposed changes. @Pouyanpi

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 26, 2025

Greptile Overview

Greptile Summary

Modified initialization functions to return None when providers aren't found instead of raising exceptions, ensuring that real initialization errors (like invalid API keys or configuration issues) are surfaced instead of being masked by "provider not found" errors from later fallback attempts.

  • Updated _init_text_completion_model and _init_community_chat_models to catch RuntimeError from provider lookups and return None
  • Updated _init_gpt35_turbo_instruct return type to include None
  • Modified unit tests to verify None returns instead of expecting ValueError
  • Added comprehensive integration tests to verify exception preservation through the fallback chain and ImportError prioritization

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The change is well-designed and addresses a real problem where relevant exceptions were being masked. The implementation correctly distinguishes between "provider not found" (return None) vs "provider found but initialization failed" (raise exception). Comprehensive test coverage validates both the individual function behavior and the end-to-end exception handling flow, including proper ImportError prioritization.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
nemoguardrails/llm/models/langchain_initializer.py 5/5 Changed return types of _init_text_completion_model, _init_community_chat_models, and _init_gpt35_turbo_instruct to return None instead of raising exceptions when providers aren't found, preventing relevant exceptions from being masked
tests/llm_providers/test_langchain_initialization_methods.py 5/5 Updated tests to expect None return instead of ValueError when providers are not found
tests/llm_providers/test_langchain_initializer.py 5/5 Added comprehensive tests for exception preservation and ImportError prioritization in the fallback chain

Sequence Diagram

sequenceDiagram
    participant Client
    participant init_langchain_model
    participant special_cases as _handle_model_special_cases
    participant chat_completion as _init_chat_completion_model
    participant community_chat as _init_community_chat_models
    participant text_completion as _init_text_completion_model
    participant get_provider as _get_*_provider

    Client->>init_langchain_model: init_langchain_model(model, provider, mode, kwargs)
    
    Note over init_langchain_model: Try initializers in order
    
    init_langchain_model->>special_cases: Try special cases
    alt Special case found
        special_cases-->>init_langchain_model: Return model
        init_langchain_model-->>Client: Return model
    else No special case
        special_cases-->>init_langchain_model: None
    end
    
    init_langchain_model->>chat_completion: Try chat completion (if mode=chat)
    chat_completion->>get_provider: _get_chat_completion_provider(provider)
    alt Provider found
        get_provider-->>chat_completion: provider_cls
        chat_completion-->>init_langchain_model: Return model
        init_langchain_model-->>Client: Return model
    else Provider error
        get_provider-->>chat_completion: ValueError/ImportError
        chat_completion-->>init_langchain_model: Exception
        Note over init_langchain_model: Store exception, continue
    end
    
    init_langchain_model->>community_chat: Try community chat (if mode=chat)
    community_chat->>get_provider: _get_chat_completion_provider(provider)
    alt Provider not found (NEW)
        get_provider-->>community_chat: RuntimeError
        community_chat-->>init_langchain_model: None (not exception!)
    else Provider found but init fails
        get_provider-->>community_chat: provider_cls
        Note over community_chat: provider_cls(**kwargs) fails
        community_chat-->>init_langchain_model: Exception (API key error, etc)
        Note over init_langchain_model: Store exception, continue
    end
    
    init_langchain_model->>text_completion: Try text completion
    text_completion->>get_provider: _get_text_completion_provider(provider)
    alt Provider not found (NEW)
        get_provider-->>text_completion: RuntimeError
        text_completion-->>init_langchain_model: None (not exception!)
    else Provider found
        get_provider-->>text_completion: provider_cls
        text_completion-->>init_langchain_model: Return model
        init_langchain_model-->>Client: Return model
    end
    
    Note over init_langchain_model: All initializers tried
    alt ImportError encountered
        init_langchain_model-->>Client: Raise ModelInitializationError with ImportError
    else Other exception encountered
        init_langchain_model-->>Client: Raise ModelInitializationError with last exception
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@codecov
Copy link

codecov bot commented Nov 26, 2025

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
nemoguardrails/llm/models/langchain_initializer.py 84.61% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

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