-
Notifications
You must be signed in to change notification settings - Fork 563
fix: Propagate model and base URL in LLMCallException; improve error handling #1502
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?
Changes from all commits
b48d7bb
3ada74a
bdd0e80
ace439c
df829e4
815fc34
9c120c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from typing import Any, Optional | ||
|
|
||
| __all__ = [ | ||
| "ConfigurationError", | ||
| "InvalidModelConfigurationError", | ||
| "InvalidRailsConfigurationError", | ||
| "LLMCallException", | ||
| ] | ||
|
|
||
|
|
||
| class ConfigurationError(ValueError): | ||
| """ | ||
| Base class for Guardrails Configuration validation errors. | ||
| """ | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class InvalidModelConfigurationError(ConfigurationError): | ||
| """Raised when a guardrail configuration's model is invalid.""" | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class InvalidRailsConfigurationError(ConfigurationError): | ||
| """Raised when rails configuration is invalid. | ||
|
|
||
| Examples: | ||
| - Input/output rail references a model that doesn't exist in config | ||
| - Rail references a flow that doesn't exist | ||
| - Missing required prompt template | ||
| - Invalid rail parameters | ||
| """ | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class LLMCallException(Exception): | ||
| """A wrapper around the LLM call invocation exception. | ||
|
|
||
| This is used to propagate the exception out of the `generate_async` call. The default behavior is to | ||
| catch it and return an "Internal server error." message. | ||
| """ | ||
|
|
||
| def __init__(self, inner_exception: Any, context_message: Optional[str] = None): | ||
| """Initialize LLMCallException. | ||
|
|
||
| Args: | ||
| inner_exception: The original exception that occurred | ||
| context_message: Optional context to prepend (for example, the model name or endpoint) | ||
| """ | ||
| message = f"{context_message or 'LLM Call Exception'}: {str(inner_exception)}" | ||
| super().__init__(message) | ||
|
|
||
| self.inner_exception = inner_exception | ||
| self.context_message = context_message |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,13 +142,13 @@ def init_langchain_model( | |
| initializers: list[ModelInitializer] = [ | ||
| # Try special case handlers first (handles both chat and text) | ||
| ModelInitializer(_handle_model_special_cases, ["chat", "text"]), | ||
| # FIXME: is text and chat a good idea? | ||
| # For text mode, use text completion, we are using both text and chat as the last resort | ||
| ModelInitializer(_init_text_completion_model, ["text", "chat"]), | ||
| # For chat mode, first try the standard chat completion API | ||
| ModelInitializer(_init_chat_completion_model, ["chat"]), | ||
| # For chat mode, fall back to community chat models | ||
| ModelInitializer(_init_community_chat_models, ["chat"]), | ||
|
Comment on lines
+145
to
151
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: reordering changes fallback behavior - text completion now runs before chat/community models. FIXME comment suggests uncertainty. verify this doesn't break existing configurations expecting chat completion first |
||
| # FIXME: is text and chat a good idea? | ||
| # For text mode, use text completion, we are using both text and chat as the last resort | ||
| ModelInitializer(_init_text_completion_model, ["text", "chat"]), | ||
|
Comment on lines
-149
to
-151
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NeMo Guardrails MS uses a custom chat client for However, the relevant exception was thrown by our custom class, but gets swallowed here by the exception thrown by I repositioned |
||
| ] | ||
|
|
||
| # Track the last exception for better error reporting | ||
|
|
||
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.
style: text completion now runs before chat models - verify this doesn't break existing chat configurations that expect chat completion to be tried first. the FIXME suggests uncertainty about this ordering.