Skip to content
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

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

Closed
14 tasks
RyanFrench opened this issue Jun 13, 2023 · 1 comment · Fixed by #6141

Comments

@RyanFrench
Copy link
Contributor

RyanFrench commented Jun 13, 2023

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

@devstein
Copy link

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

hwchase17 pushed a commit that referenced this issue Jun 19, 2023
…ny chat history. (#6141)

<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.



After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.

Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->

The current version of chat history with DynamoDB doesn't handle the
case correctly when a table has no chat history. This change solves this
error handling.

<!-- Remove if not applicable -->

Fixes #6088

#### Who can review?

Tag maintainers/contributors who might be interested:

@hwchase17

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @hwchase17

  VectorStores / Retrievers / Memory
  - @dev2049

 -->
kacperlukawski pushed a commit to kacperlukawski/langchain that referenced this issue Jun 29, 2023
…ny chat history. (langchain-ai#6141)

<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.



After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.

Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->

The current version of chat history with DynamoDB doesn't handle the
case correctly when a table has no chat history. This change solves this
error handling.

<!-- Remove if not applicable -->

Fixes langchain-ai#6088

#### Who can review?

Tag maintainers/contributors who might be interested:

@hwchase17

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @hwchase17

  VectorStores / Retrievers / Memory
  - @dev2049

 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants