- Notifications
You must be signed in to change notification settings - Fork 564
fix: Surface relevant exception when initializing langchain model #1516
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
base: develop
Are you sure you want to change the base?
fix: Surface relevant exception when initializing langchain model #1516
Conversation
Greptile OverviewGreptile SummaryModified initialization functions to return
Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram 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 |
There was a problem hiding this 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
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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
mainmodels. 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 returnNoneif a provider can't be found. This way, if a previousinit_*function threw an exception as a result of actually invoking a provider class, it gets preserved.Related Issue(s)
Checklist