langchain
langchain copied to clipboard
Issue: If DynamoDB table does not exist conversation message history fails with "local variable 'response' referenced before assignment"
System Info
If the dyanamodb table does not exist when retrieving conversation history then a generic "local variable 'response' referenced before assignment" error is returned. This is due to the exception handling at https://github.com/hwchase17/langchain/blob/master/langchain/memory/chat_message_histories/dynamodb.py#L50 does not determine if the table does not exist
Who can help?
No response
Information
- [ ] The official example notebooks/scripts
- [ ] My own modified scripts
Related Components
- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async
Reproduction
- Create a dynamodb chat message history, initialised with a DynamoDB table name that doesn't exist
- Interact with the message history
Expected behavior
An error should be logged stating that the dynamodb table was not found
Generated by a 🤖
Answer
To fix the issue, modify the exception handling in the DynamoDBChatMessageHistory
class to check if the table exists and log an appropriate error message. Update the dynamodb.py
file with the following code:
from botocore.exceptions import ClientError
# ...
@property
def messages(self) -> List[BaseMessage]: # type: ignore
"""Retrieve the messages from DynamoDB"""
items = []
try:
response = self.table.get_item(Key={"SessionId": self.session_id})
if "Item" in response:
items = response["Item"]["History"]
except ClientError as error:
if error.response["Error"]["Code"] == "ResourceNotFoundException":
logger.error("DynamoDB table not found: %s", self.table.table_name)
else:
logger.error(error)
messages = messages_from_dict(items)
return messages
This change will log an error stating that the DynamoDB table was not found and return an empty list of messages if the table does not exist.
Sources
This response is meant to be helpful and save users time. It is not meant to be a precise solution.
Please leave a 👍 if this is helpful and 👎 if it is irrelevant