Missing Language.PERL Separators And InMemoryCache Maxsize Update Bug
Two bugs are reported: (1) The Language.PERL enum is defined but not handled in get_separators_for_language, causing a ValueError when using RecursiveCharacterTextSplitter.from_language(Language.PERL). (2) InMemoryCache.update() evicts the oldest entry even when updating an existing key while the cache is at maxsize, leading to unnecessary cache misses.
The Language enum includes PERL, but the separator mapping function lacks a corresponding branch, causing a fall-through error. In InMemoryCache, the update method performs an unconditional eviction when the cache size equals maxsize, without checking whether the key already exists; thus updating an existing key incorrectly removes another entry.
1. Call RecursiveCharacterTextSplitter.from_language(Language.PERL, chunk_size=10, chunk_overlap=0) and observe ValueError. 2. Create InMemoryCache(maxsize=2), update with two distinct keys, then update one of those keys again. Check that the other key has been evicted unexpectedly.
Fixing Code Block
```python
# Fix 1: Add PERL separator support in langchain_text_splitters/character.py
# Inside get_separators_for_language, add the following branch:
elif language == Language.PERL:
return [
"\nsub ",
"\npackage ",
"\ndo ",
"\neval ",
"\n#",
"\n//",
"\n=cut",
"\n__END__",
"\n__DATA__",
"\n=pod",
]
# Fix 2: Modify InMemoryCache.update in langchain_core/caches.py
# Replace the existing update method with:
def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None:
key = (prompt, llm_string)
if key in self._cache:
# Update existing entry without evicting others
del self._cache[key]
elif self._maxsize is not None and len(self._cache) >= self._maxsize:
# Evict only when adding a new key and cache is full
del self._cache[next(iter(self._cache))]
self._cache[key] = return_val
```
For PERL, the added branch returns common Perl code separators (sub, package, do, eval, comments, POD markers) mirroring the approach used for other languages. For InMemoryCache, the updated method first checks if the key exists; if so, it removes and reinserts it to maintain LRU order without evicting other entries. Eviction only occurs when a new key is added and the cache is at capacity.
Edge Case Audit
The PERL separator list may not cover all Perl code styles (e.g., anonymous subs, one-liners) and could affect chunking granularity. The InMemoryCache change preserves data but alters eviction timing; code relying on the old buggy behavior may behave differently. Rollback is straightforward by reverting to the previous implementation or using a version prior to the fix.