Skip to content

Commit

Permalink
fix: propagate botocore exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelm10 committed Sep 11, 2024
1 parent dd9f74a commit cbfee25
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion libs/aws/langchain_aws/embeddings/bedrock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import logging
import os
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -153,7 +154,8 @@ def _embedding_func(self, text: str) -> List[float]:
return response_body.get("embedding")

except Exception as e:
raise ValueError(f"Error raised by inference endpoint: {e}")
logging.error(f"Error raised by inference endpoint: {e}")
raise e

def _normalize_vector(self, embeddings: List[float]) -> List[float]:
"""Normalize the embedding to a unit vector."""
Expand Down
11 changes: 9 additions & 2 deletions libs/aws/langchain_aws/llms/bedrock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import logging
import os
import warnings
from abc import ABC
Expand Down Expand Up @@ -734,7 +735,10 @@ def _prepare_input_and_invoke(
) = LLMInputOutputAdapter.prepare_output(provider, response).values()

except Exception as e:
raise ValueError(f"Error raised by bedrock service: {e}")
logging.error(f"Error raised by bedrock service: {e}")
if run_manager is not None:
run_manager.on_llm_error(e)
raise e

if stop is not None:
text = enforce_stop_tokens(text, stop)
Expand Down Expand Up @@ -854,7 +858,10 @@ def _prepare_input_and_invoke_stream(
response = self.client.invoke_model_with_response_stream(**request_options)

except Exception as e:
raise ValueError(f"Error raised by bedrock service: {e}")
logging.error(f"Error raised by bedrock service: {e}")
if run_manager is not None:
run_manager.on_llm_error(e)
raise e

for chunk in LLMInputOutputAdapter.prepare_output_stream(
provider,
Expand Down
11 changes: 9 additions & 2 deletions libs/aws/langchain_aws/llms/sagemaker_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sagemaker InvokeEndpoint API."""

import io
import logging
import re
from abc import abstractmethod
from typing import Any, Dict, Generic, Iterator, List, Mapping, Optional, TypeVar, Union
Expand Down Expand Up @@ -338,7 +339,10 @@ def _stream(
run_manager.on_llm_new_token(chunk.text)

except Exception as e:
raise ValueError(f"Error raised by streaming inference endpoint: {e}")
logging.error(f"Error raised by streaming inference endpoint: {e}")
if run_manager is not None:
run_manager.on_llm_error(e)
raise e

def _call(
self,
Expand Down Expand Up @@ -384,7 +388,10 @@ def _call(
**_endpoint_kwargs,
)
except Exception as e:
raise ValueError(f"Error raised by inference endpoint: {e}")
logging.error(f"Error raised by inference endpoint: {e}")
if run_manager is not None:
run_manager.on_llm_error(e)
raise e

text = self.content_handler.transform_output(response["Body"])
if stop is not None:
Expand Down

0 comments on commit cbfee25

Please sign in to comment.