-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: prevent crash when reloading conversations with Base64-encoded files #2799
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
Open
hztBUAA
wants to merge
3
commits into
Chainlit:main
Choose a base branch
from
hztBUAA:fix/base64-file-reload-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−17
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import json | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| from chainlit.data.chainlit_data_layer import ChainlitDataLayer | ||
| from chainlit.data.dynamodb import DynamoDBDataLayer | ||
|
|
||
|
|
||
| class TestConvertElementRowToDict: | ||
| """Test suite for ChainlitDataLayer._convert_element_row_to_dict.""" | ||
|
|
||
| def _make_layer(self): | ||
| return ChainlitDataLayer(database_url="postgresql://fake", storage_client=None) | ||
|
|
||
| def _make_row(self, **overrides): | ||
| row = { | ||
| "id": "elem-1", | ||
| "threadId": "thread-1", | ||
| "stepId": "step-1", | ||
| "metadata": json.dumps({"type": "file"}), | ||
| "url": None, | ||
| "name": "test_file.txt", | ||
| "mime": "text/plain", | ||
| "objectKey": None, | ||
| "chainlitKey": None, | ||
| "display": "inline", | ||
| "size": None, | ||
| "language": None, | ||
| "page": None, | ||
| "autoPlay": None, | ||
| "playerConfig": None, | ||
| "props": "{}", | ||
| } | ||
| row.update(overrides) | ||
| return row | ||
|
|
||
| def test_convert_element_row_with_none_url(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict(self._make_row(url=None)) | ||
| assert result["url"] is None | ||
|
|
||
| def test_convert_element_row_with_none_object_key(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict(self._make_row(objectKey=None)) | ||
| assert result["objectKey"] is None | ||
|
|
||
| def test_convert_element_row_with_valid_url(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(url="https://storage.example.com/file.txt") | ||
| ) | ||
| assert result["url"] == "https://storage.example.com/file.txt" | ||
|
|
||
| def test_convert_element_row_preserves_chainlit_key(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(chainlitKey="file-abc-123") | ||
| ) | ||
| assert result["chainlitKey"] == "file-abc-123" | ||
|
|
||
| def test_convert_element_row_type_from_metadata(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(metadata=json.dumps({"type": "image"})) | ||
| ) | ||
| assert result["type"] == "image" | ||
|
|
||
| def test_convert_element_row_default_type(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(metadata=json.dumps({})) | ||
| ) | ||
| assert result["type"] == "file" | ||
|
|
||
| def test_convert_element_row_full_data(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row( | ||
| url="https://storage.example.com/file.txt", | ||
| objectKey="threads/thread-1/files/elem-1", | ||
| chainlitKey="file-abc-123", | ||
| display="side", | ||
| size="large", | ||
| language="python", | ||
| page=3, | ||
| mime="application/pdf", | ||
| props=json.dumps({"custom": "value"}), | ||
| ) | ||
| ) | ||
| assert result["id"] == "elem-1" | ||
| assert result["url"] == "https://storage.example.com/file.txt" | ||
| assert result["objectKey"] == "threads/thread-1/files/elem-1" | ||
| assert result["chainlitKey"] == "file-abc-123" | ||
| assert result["props"] == {"custom": "value"} | ||
|
|
||
|
|
||
| class TestGetElementNoneHandling: | ||
| """Test that get_element does not convert None values to 'None' strings.""" | ||
|
|
||
| def _make_layer(self): | ||
| return ChainlitDataLayer(database_url="postgresql://fake", storage_client=None) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_element_returns_none_url_not_string(self): | ||
| layer = self._make_layer() | ||
| layer.execute_query = AsyncMock( | ||
| return_value=[ | ||
| { | ||
| "id": "elem-1", | ||
| "threadId": "thread-1", | ||
| "stepId": "step-1", | ||
| "metadata": json.dumps({"type": "file"}), | ||
| "url": None, | ||
| "name": "test.txt", | ||
| "mime": None, | ||
| "objectKey": None, | ||
| "chainlitKey": "ck-1", | ||
| "display": "inline", | ||
| "size": None, | ||
| "language": None, | ||
| "page": None, | ||
| "autoPlay": None, | ||
| "playerConfig": None, | ||
| "props": "{}", | ||
| } | ||
| ] | ||
| ) | ||
| result = await layer.get_element("thread-1", "elem-1") | ||
| assert result is not None | ||
| assert result["url"] is None | ||
| assert result["objectKey"] is None | ||
| assert result["mime"] is None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_element_not_found(self): | ||
| layer = self._make_layer() | ||
| layer.execute_query = AsyncMock(return_value=[]) | ||
| result = await layer.get_element("thread-1", "nonexistent") | ||
| assert result is None | ||
|
|
||
|
|
||
| class _FakeDynamoClient: | ||
| def __init__(self, items): | ||
| self.items = items | ||
|
|
||
| def query(self, **_kwargs): | ||
| return {"Items": self.items} | ||
|
|
||
|
|
||
| class _FailingStorageProvider: | ||
| async def get_read_url(self, object_key: str): | ||
| raise RuntimeError(f"failed for {object_key}") | ||
|
|
||
|
|
||
| class TestDynamoThreadElementUrlFailure: | ||
| @pytest.mark.asyncio | ||
| async def test_get_thread_tolerates_storage_read_url_error(self): | ||
| bootstrap = DynamoDBDataLayer( | ||
| table_name="test-table", client=_FakeDynamoClient([]) | ||
| ) | ||
|
|
||
| thread_item = bootstrap._serialize_item( | ||
| { | ||
| "PK": "THREAD#thread-1", | ||
| "SK": "THREAD", | ||
| "id": "thread-1", | ||
| "createdAt": "2026-01-01T00:00:00Z", | ||
| "name": "name", | ||
| } | ||
| ) | ||
| element_item = bootstrap._serialize_item( | ||
| { | ||
| "PK": "THREAD#thread-1", | ||
| "SK": "ELEMENT#elem-1", | ||
| "id": "elem-1", | ||
| "objectKey": "threads/thread-1/files/elem-1", | ||
| "type": "file", | ||
| "name": "file.txt", | ||
| } | ||
| ) | ||
|
|
||
| layer = DynamoDBDataLayer( | ||
| table_name="test-table", | ||
| client=_FakeDynamoClient([thread_item, element_item]), | ||
| storage_provider=_FailingStorageProvider(), | ||
| ) | ||
|
|
||
| thread = await layer.get_thread("thread-1") | ||
|
|
||
| assert thread is not None | ||
| assert thread["id"] == "thread-1" | ||
| assert len(thread["elements"]) == 1 | ||
| assert thread["elements"][0]["id"] == "elem-1" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice changes in two data layers but unit tests only for one, what's the reason?