AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Blob.As_bytes_io() Raises NotImplementedError For String Data

Blob.as_bytes_io() currently only yields a byte stream for bytes data and raises NotImplementedError for string data, contrary to Blob.as_bytes() which works for both str and bytes. This creates an API inconsistency and breaks code that expects to stream string blobs as bytes.

highConfidence 95%LangChainEmbeddingsChat ModelsRetrieversAffected V<=1.2.19

Origin Analysis

The as_bytes_io() context manager in langchain_core.documents.base.Blob is implemented with only a branch for bytes data. When the blob's data is a string (str), it falls through to the else clause and raises NotImplementedError, even though as_bytes() properly encodes the string to bytes using the blob's encoding.
```python from langchain_core.documents.base import Blob blob = Blob.from_data("hello") with blob.as_bytes_io() as f: print(f.read()) ``` This raises `NotImplementedError: Unable to convert blob Blob 0x... metadata={} data='hello'` instead of returning a byte stream.

Fixing Code Block

Edge Case Audit

The fix loads the entire encoded string into memory when creating the BytesIO object. For very large strings, this may increase memory usage; however, the same trade-off already exists in as_bytes(). The fix does not address cases where the blob's data is None or a path-based blob (path attribute set but data is None). Such cases will still raise NotImplementedError. If a future change to encoding behavior is needed, ensure that the same encoding is used consistently across all conversion methods. Rollback recommendation: if this patch causes unexpected issues, revert to the previous version and use `blob.as_bytes()` as a workaround, reading the bytes directly instead of using the stream.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state