crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] RAG URL loaders decode UTF-8 text without a declared charset as Latin-1

Open RaycarlLei opened this issue 1 month ago • 2 comments

Description

load_from_url in crewai_tools/rag/loaders/utils.py returns response.text (utils.py#L37-L39). When a server sends a text/* Content-Type with no charset parameter, Requests falls back to ISO-8859-1, so a UTF-8 body comes back as mojibake: café 上海 becomes café ä¸\x8aæµ·. That string is what gets chunked and embedded.

Every loader that fetches through load_from_url is affected when the response is text/*: TextFileLoader (URL support from #7506), CSVLoader, JSONLoader, MDXLoader and XMLLoader. A text/plain or text/csv response without a charset is common. Python's own python -m http.server sends exactly that for .txt and .csv files.

Steps to Reproduce

  1. Serve two UTF-8 files with Python's built-in server. It sends Content-Type: text/plain and text/csv with no charset:
mkdir site
printf 'café 上海\n' > site/notes.txt
printf 'city,country\n上海,中国\nMontréal,Canada\n' > site/cities.csv
python -m http.server 8000 --bind 127.0.0.1 --directory site
  1. In another terminal, run:
import os

# Only needed because the test server is on localhost.
os.environ["CREWAI_TOOLS_ALLOW_UNSAFE_PATHS"] = "true"

from crewai_tools.rag.loaders.csv_loader import CSVLoader
from crewai_tools.rag.loaders.text_loader import TextFileLoader
from crewai_tools.rag.source_content import SourceContent

base = "http://127.0.0.1:8000"
print(repr(TextFileLoader().load(SourceContent(f"{base}/notes.txt")).content))
print(repr(CSVLoader().load(SourceContent(f"{base}/cities.csv")).content))

No API key or outside network access is needed.

Expected behavior

A text/* body that declares no charset and is valid UTF-8 is read as UTF-8. URLReadTool already uses UTF-8 when no charset is declared (url_read_tool.py#L334). A declared charset is still honored, and a body that is not valid UTF-8 can keep today's Latin-1 fallback.

Screenshots/Code snippets

load_from_url on main:

try:
    response = safe_get(url, headers=headers, timeout=30)
    response.raise_for_status()
    return response.text

requests.utils.get_encoding_from_headers returns "ISO-8859-1" for any text/* Content-Type without a charset, and response.text decodes with that.

Operating System

Other (specify in additional context)

Python Version

3.10

crewAI Version

1.15.28 (main at 5d58569ae; the RAG loaders are unchanged on 95de6ea1f)

crewAI Tools Version

1.15.28

Virtual Environment

Venv

Evidence

Output of step 2 on main (5d58569ae), the same on Python 3.10.19 and 3.13.16:

'café ä¸\x8aæµ·\n'
'Headers: city | country\n--------------------------------------------------\nRow 1: city: ä¸\x8aæµ· | country: ä¸\xadå\x9b½\nRow 2: city: Montréal | country: Canada'

The same bytes served as text/plain; charset=utf-8 load correctly as 'café 上海\n'.

Possible Solution

In load_from_url, when Requests applied its ISO-8859-1 default (no charset in the header) and the body decodes as UTF-8, set response.encoding = "utf-8-sig" before reading .text. Leave declared charsets alone, and keep the Latin-1 fallback for bodies that are not valid UTF-8. utf-8-sig also drops a leading BOM, which is how #7467 decodes BOM-prefixed bodies, so the two changes give the same result.

A PR with the fix and tests follows and links this issue.

Additional context

  • OS: macOS 27.0.1 (not in the dropdown).
  • Follow-up to #7506, which routed text-file URLs through load_from_url. Its URL test sets response.encoding = "utf-8" on the mocked response, so the header default was never exercised.
  • #7467 (open) changes the same function to honor a UTF-8 BOM. It leaves BOM-free bodies alone, and its tests expect a BOM-free Latin-1 text/csv body without a charset to keep decoding as Latin-1. The fix above keeps that case. #7892 and #7602 only touch csv_loader.py.
  • AI disclosure: Claude Code (Claude Opus 5.5) found this bug, ran the reproduction above and wrote this report. It filed the issue on behalf of @RaycarlLei, who approved posting it. Please add the llm-generated label; this account doesn't have permission to add labels.

RaycarlLei avatar Oct 22 '26 19:10 RaycarlLei

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 22 '26 12:11 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Nov 28 '26 12:11 github-actions[bot]