Repository navigation
docs(langchain): document that bare-LLM cache hits are not scanned - #10
Merged
Merged
Conversation
LangChain (langchain-core 1.6.12) looks up the LLM cache before firing on_llm_start, and a full cache hit returns the cached generations without start or end events. A callback handler therefore cannot scan the prompt or the cached output of such a call, and no BLOCK or fail_closed error can stop it. Narrow the documented contract in the README, CHANGELOG (Known limitation) and the integration's module docstring: set cache=False on LLMs whose calls must be scanned, or scan outside the model call. Chat models fire on_chat_model_start before their cache lookup, so cached chat calls still have their input scanned. Add regression tests with real FakeListLLM / FakeListChatModel and InMemoryCache, sync and async: - bare LLM cache hit: first call scans llm_input and llm_output (ALLOW); the identical second call with BLOCK mocked returns the cached reply with zero scan requests and no raise - bare LLM with cache=False: the second call raises ContentBlockedError on llm_input - chat model with the same cache: the second call raises ContentBlockedError on chat_input No runtime behaviour changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Independent review (GPT-6 Astra, round 1) at head The cache-hit exclusion accurately narrows the documented contract; the six new sync/async regressions exercise real LangChain cache paths with blocking controls, and executable SDK code is unchanged (AST-equal). Full suite 297 passed. One non-blocking nit: add a cross-reference near the historical coverage summaries (CHANGELOG.md:60-67, langchain.py module docstring). CI 3.9-3.13 green on this head. Merging via the improvement loop. |
Merged
Closed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Limitation
A bare (non-chat) LangChain LLM call answered entirely from an LLM cache is not scanned. The handlers scan prompts in
on_llm_startand outputs inon_llm_end, but langchain-core (1.6.12,language_models/llms.py) looks up the cache before firingon_llm_start. A full cache hit returns the cached generations without any start or end event. So for a repeated prompt on a cachedLLM, neither the prompt nor the cached output reaches ShieldCortex, and no BLOCK orfail_closederror can stop the call.Why document instead of fix
A callback handler can only act on events LangChain fires. On a full cache hit, no event fires, so the handlers cannot fix this without changing how they are used. This PR narrows the documented contract instead:
cache=Falseon LLMs whose calls must be scanned, or to scan outside the model call. It notes that the defaultcache=Nonefalls back to any cache set withset_llm_cache.[Unreleased]→### Known limitation: a new entry with the same guidance.shieldcortex.integrations.langchain: the same paragraph.The docs also say that chat models fire
on_chat_model_startbefore their cache lookup, so a cached chat call still has its input scanned. The control test below shows this.Tests added (
tests/integrations/test_langchain.py)These use a real
FakeListLLM/FakeListChatModelwithInMemoryCacheand respx-mocked/v1/scan. Each test runs in sync (ShieldCortexSyncCallbackHandler) and async (ShieldCortexCallbackHandler) mode. Each makes two calls with the same handler andraise_on_block=True: ALLOW for the first call, then BLOCK for the second.test_bare_llm_cache_hit_is_not_scanned: the first call scansllm_inputandllm_output. The identical second call returns"cached reply"with zero scan requests and no raise. This pins the limitation, so a LangChain change that starts firing callbacks on cache hits will make the test fail and prompt a docs update.test_bare_llm_without_cache_rescans_and_blocks: same setup withcache=False. The second call raisesContentBlockedErroronlangchain:llm_input.test_chat_model_cache_hit_still_scans_input: a chat model with the same cache. The second call raisesContentBlockedErroronlangchain:chat_input.No runtime code changed and no existing assertions changed.
Gates (langchain-core 1.6.12)
ruff check src/: exit 0, all checks passedmypy src/shieldcortex/ --ignore-missing-imports: exit 0, no issues in 12 source filespython -m pytest -q: exit 0, 297 passed🤖 Generated with Claude Code