Skip to content

feat(storage): add content_filters to DynamoDbChatStorage#537

Open
nuthalapativarun wants to merge 1 commit into
2FastLabs:mainfrom
nuthalapativarun:fix/131-dynamodb-content-filters
Open

feat(storage): add content_filters to DynamoDbChatStorage#537
nuthalapativarun wants to merge 1 commit into
2FastLabs:mainfrom
nuthalapativarun:fix/131-dynamodb-content-filters

Conversation

@nuthalapativarun

Copy link
Copy Markdown

Issue Link (REQUIRED)

Fixes #131

Summary

Changes

Adds an optional content_filters parameter to DynamoDbChatStorage — a list of (original, placeholder) string pairs. When provided:

  • On save (save_chat_message, save_chat_messages): each original value is replaced by its placeholder before the message is written to DynamoDB.
  • On fetch (fetch_chat, fetch_chat_with_timestamp, fetch_all_chats): placeholders are restored to their original values before returning to the caller.

This eliminates the need for a custom storage implementation when sensitive content (e.g. company names, tokens) must not be persisted in plain text.

User experience

Before: users had to subclass DynamoDbChatStorage or write a custom storage class to mask sensitive values.

After:

storage = DynamoDbChatStorage(
    table_name="my-table",
    region="us-east-1",
    content_filters=[
        ("acme-corp", "#COMPANY#"),
        ("secret-token-xyz", "#TOKEN#"),
    ]
)

DynamoDB stores #COMPANY#; the agent always sees acme-corp.

Checklist

  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented
  • I have linked this PR to an existing issue (required)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Adds an optional content_filters parameter — a list of (original, placeholder)
pairs — to DynamoDbChatStorage. Sensitive values are replaced with their
placeholders before writing to DynamoDB and restored on every read path
(fetch_chat, fetch_chat_with_timestamp, fetch_all_chats).

Closes 2FastLabs#131
@cornelcroi

Copy link
Copy Markdown
Collaborator

Hey @nuthalapativarun, a few things need to be fixed before this can merge.

There is a bug in save_chat_message — when a consecutive-role duplicate is detected, the method returns the raw result from _fetch_raw which still has placeholder strings in it instead of the original values. The caller gets masked data back. Also, fetch_all_chats applies _reverse_content_filters differently from the other fetch methods — it has an isinstance guard that silently skips the reversal when content is not a list, so placeholders can leak through. Both of these need tests too. And the docs page for DynamoDB storage needs to be updated to document the new content_filters parameter. CI also hasn't run on the branch.

One thing worth calling out in the PR description: the filter replacement works by doing a json.dumps then naive string replace, so if a filter string contains JSON special characters like quotes or backslashes it can produce invalid JSON. Worth documenting as a known limitation. Also interesting to note — the Swift equivalent (TransformingChatStorage) deliberately does not restore the original values on read, while this PR does. Both approaches are valid but they are different behaviors and worth mentioning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: DynamoDbChatStorage option to replace sensitive content by placeholder

2 participants