From 540ebf35a9b8fb4f9fc1fa8145da4cac271e51a7 Mon Sep 17 00:00:00 2001 From: T Cramer Date: Fri, 29 Mar 2024 03:07:33 +0000 Subject: [PATCH] community[patch]: Add explicit error message to Bedrock error output. (#17328) - **Description:** Propagate Bedrock errors into Langchain explicitly. Use-case: unset region error is hidden behind 'Could not load credentials...' message - **Issue:** [17654](https://github.com/langchain-ai/langchain/issues/17654) - **Dependencies:** None --------- Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> --- libs/community/langchain_community/document_loaders/pdf.py | 2 +- libs/community/langchain_community/embeddings/bedrock.py | 2 +- .../langchain_community/embeddings/sagemaker_endpoint.py | 2 +- libs/community/langchain_community/llms/bedrock.py | 4 +++- .../comprehend_moderation/amazon_comprehend_moderation.py | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/libs/community/langchain_community/document_loaders/pdf.py b/libs/community/langchain_community/document_loaders/pdf.py index d4a636c69be..fc49be4e25c 100644 --- a/libs/community/langchain_community/document_loaders/pdf.py +++ b/libs/community/langchain_community/document_loaders/pdf.py @@ -670,7 +670,7 @@ class AmazonTextractPDFLoader(BasePDFLoader): raise ValueError( "Could not load credentials to authenticate with AWS client. " "Please check that credentials in the specified " - "profile name are valid." + f"profile name are valid. {e}" ) from e self.parser = AmazonTextractPDFParser( textract_features=features, diff --git a/libs/community/langchain_community/embeddings/bedrock.py b/libs/community/langchain_community/embeddings/bedrock.py index 7ab94df4dcb..3cfa977fd91 100644 --- a/libs/community/langchain_community/embeddings/bedrock.py +++ b/libs/community/langchain_community/embeddings/bedrock.py @@ -107,7 +107,7 @@ class BedrockEmbeddings(BaseModel, Embeddings): raise ValueError( "Could not load credentials to authenticate with AWS client. " "Please check that credentials in the specified " - "profile name are valid." + f"profile name are valid. Bedrock error: {e}" ) from e return values diff --git a/libs/community/langchain_community/embeddings/sagemaker_endpoint.py b/libs/community/langchain_community/embeddings/sagemaker_endpoint.py index 4e906133f76..1f01e1ecf4d 100644 --- a/libs/community/langchain_community/embeddings/sagemaker_endpoint.py +++ b/libs/community/langchain_community/embeddings/sagemaker_endpoint.py @@ -142,7 +142,7 @@ class SagemakerEndpointEmbeddings(BaseModel, Embeddings): raise ValueError( "Could not load credentials to authenticate with AWS client. " "Please check that credentials in the specified " - "profile name are valid." + f"profile name are valid. {e}" ) from e except ImportError: diff --git a/libs/community/langchain_community/llms/bedrock.py b/libs/community/langchain_community/llms/bedrock.py index 8ab5ad276ea..203110f87bc 100644 --- a/libs/community/langchain_community/llms/bedrock.py +++ b/libs/community/langchain_community/llms/bedrock.py @@ -427,11 +427,13 @@ class BedrockBase(BaseModel, ABC): "Could not import boto3 python package. " "Please install it with `pip install boto3`." ) + except ValueError as e: + raise ValueError(f"Error raised by bedrock service: {e}") except Exception as e: raise ValueError( "Could not load credentials to authenticate with AWS client. " "Please check that credentials in the specified " - "profile name are valid." + f"profile name are valid. Bedrock error: {e}" ) from e return values diff --git a/libs/experimental/langchain_experimental/comprehend_moderation/amazon_comprehend_moderation.py b/libs/experimental/langchain_experimental/comprehend_moderation/amazon_comprehend_moderation.py index 3298f2843f3..197d789e32e 100644 --- a/libs/experimental/langchain_experimental/comprehend_moderation/amazon_comprehend_moderation.py +++ b/libs/experimental/langchain_experimental/comprehend_moderation/amazon_comprehend_moderation.py @@ -109,7 +109,7 @@ class AmazonComprehendModerationChain(Chain): raise ValueError( "Could not load credentials to authenticate with AWS client. " "Please check that credentials in the specified " - "profile name are valid." + f"profile name are valid. {e}" ) from e @property