Blob leaks a UTF-8 BOM into as_string()
Submission checklist
- This is a bug, not a usage question.
- I added a clear and descriptive title that summarizes this issue.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangChain rather than my code.
- The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
- This is not related to the langchain-community package.
- I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.
Package (Required)
- langchain
- langchain-openai
- langchain-anthropic
- langchain-classic
- langchain-core
- langchain-model-profiles
- langchain-tests
- langchain-text-splitters
- langchain-chroma
- langchain-deepseek
- langchain-exa
- langchain-fireworks
- langchain-groq
- langchain-huggingface
- langchain-mistralai
- langchain-nomic
- langchain-ollama
- langchain-openrouter
- langchain-perplexity
- langchain-qdrant
- langchain-xai
- Other / not sure / general
Related Issues / PRs
No response
Reproduction Steps / Example Code (Python)
import tempfile
from pathlib import Path
from langchain_core.documents.base import Blob
p = Path(tempfile.mkdtemp()) / "doc.md"
p.write_text("# Heading\n\nbody\n", encoding="utf-8-sig") # a BOM, as Notepad writes it
print(repr(Blob.from_path(p).as_string()))
# actual: '\ufeff# Heading\n\nbody\n'
# expected: '# Heading\n\nbody\n'Error Message and Stack Trace (if applicable)
Description
- I'm loading local files through
Blob— a Markdown file that was saved on Windows, so it carries a UTF-8 byte order mark. - I expect
as_string()to return the file's text. - Instead it returns the text with a zero-width
U+FEFFprepended, becauseBlobdecodes withencoding="utf-8", which preserves the BOM.
Notepad, Excel's "CSV UTF-8" export and PowerShell > / Out-File all write
a BOM by default, so any file opened and re-saved on Windows arrives with one.
The BOM then flows into splitters, embeddings and prompts. For Markdown it
lands before the leading #, exactly where a heading parser expects
line-start. Blob.from_data() behaves the same way for BOM-prefixed bytes.
This isn't platform-specific. The BOM is in the file's bytes, so it reproduces identically on Linux and macOS. Windows is just where BOM files come from.
Suggested fix: default to utf-8-sig. It decodes plain UTF-8 byte-for-byte
identically and strips a BOM when one is present, so files without a BOM are
unaffected and passing encoding explicitly keeps its exact meaning.
System Info
System Information
OS: Windows OS Version: 10.0.26200 Python Version: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:17:27) [MSC v.1929 64 bit (AMD64)]
Package Information
langchain_core: 1.6.3 langchain_community: 0.4.2 langsmith: 0.12.4 langchain_classic: 1.0.8 langchain_protocol: 0.0.19 langchain_text_splitters: 1.1.2
Social handles (optional)
Source: langchain-ai/langchain