-
Notifications
You must be signed in to change notification settings - Fork 8.5k
fix(tools): handle UTF-8 BOM in CSVLoader #7467
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
Jim-jimu
wants to merge
2
commits into
crewAIInc:main
Choose a base branch
from
Jim-jimu:fix/csv-loader-utf8-bom
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.
+133
−1
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from crewai_tools.rag.loaders.json_loader import JSONLoader | ||
| from crewai_tools.rag.loaders.xml_loader import XMLLoader | ||
| from crewai_tools.rag.source_content import SourceContent | ||
| import pytest | ||
| from requests import Response | ||
| from requests.utils import get_encoding_from_headers | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("charset", ["", "; charset=utf-8-sig"]) | ||
| @pytest.mark.parametrize( | ||
| "loader, content_type, content, metadata", | ||
| [ | ||
| ( | ||
| JSONLoader, | ||
| "application/octet-stream", | ||
| '{"message": "hello"}', | ||
| {"format": "json", "type": "dict", "size": 1}, | ||
| ), | ||
| ( | ||
| XMLLoader, | ||
| "application/xml", | ||
| "<root>hello</root>", | ||
| {"format": "xml", "root_tag": "root"}, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_url_loaders_preserve_bom_decoding( | ||
| loader: type[JSONLoader] | type[XMLLoader], | ||
| content_type: str, | ||
| content: str, | ||
| metadata: dict[str, str | int], | ||
| charset: str, | ||
| ) -> None: | ||
| """Keep JSON and XML parsing compatible with automatic or explicit BOM decoding.""" | ||
| response = Response() | ||
| response.status_code = 200 | ||
| response.headers["Content-Type"] = content_type + charset | ||
| response.encoding = get_encoding_from_headers(response.headers) | ||
| response._content = content.encode("utf-8-sig") | ||
| with patch("crewai_tools.security.safe_requests._raw_get", return_value=response): | ||
| result = loader().load(SourceContent("https://example.com/data")) | ||
|
|
||
| assert result.metadata == metadata | ||
| assert "hello" in result.content |
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.
When a URL serves BOM-prefixed UTF-8 bytes as
Content-Type: text/csvwithout a charset, Requests assigns ISO-8859-1 beforeload_from_url()readsresponse.text, so this method receivesname,...rather than a leading U+FEFF. The newremoveprefix()is therefore a no-op and the first header remains corrupted; the URL test masks this by mocking.textas already-correct Unicode. Detect the raw BOM and selectutf-8-sigbefore reading the response text, and exercise that behavior with a byte-backed response.AGENTS.md reference: AGENTS.md:L10-L11
Useful? React with 👍 / 👎.
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.
Reproduced with a real
requests.Responsecontaining UTF-8 BOM bytes andContent-Type: text/csvwithout a charset. Requests selected Latin-1, and four byte-backed CSV regression cases failed on the previous implementation.Fixed in 0e5075d. The shared
load_from_url()helper now detectsBOM_UTF8inresponse.contentand selectsutf-8-sigbefore readingresponse.text.The URL tests now exercise real response decoding and cover quoted/unquoted headers, non-ASCII values, BOM-free UTF-8 and Latin-1 responses, and embedded U+FEFF. Additional JSON/XML tests preserve compatibility with automatic and explicit BOM decoding in other users of the shared helper.
Validation: 68 tests passed, including 30 CSV tests; Ruff lint/format and mypy passed. The only warning is the existing
crewai.utilities.lock_storedeprecation warning.