langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Issue: If DynamoDB table does not exist conversation message history fails with "local variable 'response' referenced before assignment"

Open RyanFrench opened this issue 1 year ago • 1 comments

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

  1. Create a dynamodb chat message history, initialised with a DynamoDB table name that doesn't exist
  2. Interact with the message history

Expected behavior

An error should be logged stating that the dynamodb table was not found

RyanFrench avatar Jun 13 '23 09:06 RyanFrench

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

devstein avatar Jun 13 '23 18:06 devstein