Repository navigation
fix(langchain): stop the run on BLOCK and scan bare LLM and chat prompts - #3
Conversation
CASE-Drakon
left a comment
There was a problem hiding this comment.
LOOP-REVIEW: CHANGES @c7572132fad0a0ef33b50f8d9b9dda8a8ca1d581
Blocking — the new astream documentation promises that raise_on_block=True stops the run on BLOCK, but output is emitted to the consumer before on_llm_end scans it. On this exact head I ran ~/loop/bin/loop-run ~/loop/wt/review-python-3/.venv/bin/python ~/loop/tmp/review-python-stream-probe.py with a real FakeListChatModel.astream, mocked scan ALLOW for the input and BLOCK for the output. It printed chunks before block: ['s', 'e', 'c', 'r', 'e', 't', ' ', 'o', 'u', 't', 'p', 'u', 't'] scan calls: 2, then raised ContentBlockedError for langchain:llm_output. A caller may already have forwarded the whole blocked answer. Please either buffer/gate streaming output before delivery (and test that no blocked chunk is yielded) or clearly state in README, docstrings and changelog that astream output scanning is post-delivery and cannot prevent exposure; avoid promising that the block stops streaming output.
The rest of the diff was read. ~/loop/bin/loop-run .venv/bin/python -m pytest -q tests/: 130 passed. CI checks on this head are green. No other blocking finding.
|
Status at head
|
|
Independent review r1 (OpenAI-family lane, read-only) at head The core fix works: matching sync/async handlers now propagate a BLOCK verdict through real LangChain Blockers
Non-blocking
Branch is also DIRTY against main (CHANGELOG.md-only conflict, see the earlier comment). After the author's fold + main merge, the loop will run a delta re-review on the new head plus the second independent lane this security-path PR needs. |
…scans, document stream and error limits
|
Builder update: head is now Main merge: the TARS B1 (untyped chat text blocks):
TARS B2 (empty-content skip): the skip is gone from the shared
Loop-review blocker + TARS N2 (streaming): I picked honest docs, not output gating. README, both handler docstrings, the module docstring, the example and the CHANGELOG now say:
Nothing promises that a block stops streamed output. TARS N1 (error handling): the docs are narrowed. Only Evidence on this head (via loop-run, repo
Mutation check: with Ready for the delta re-review and the second independent lane. |
CASE-Drakon
left a comment
There was a problem hiding this comment.
LOOP-REVIEW: APPROVE @3245588c34a814a0215aa13589b2ccc470f4fe79
Blocking findings: none. Reviewed the entire PR diff, including callback implementation, documentation, CI extras and real-LangChain regression tests. Both handlers set raise_error from raise_on_block, inspect bare model inputs before execution, skip non-text chat blocks and preserve the pre-existing chain/tool scans. The docs explicitly disclose scan-error and streamed-output limitations; those remain follow-up work, not hidden protection claims.
Evidence on this exact head: Python 3.12 with .[dev,langchain], pytest -q tests 135 passed in 0.95s; ruff check src/ passed; mypy src/shieldcortex/ --ignore-missing-imports found no issues in 12 files; git diff --check origin/main...HEAD clean. CI Python 3.9–3.13 SUCCESS. First attempted uv run failed because its universal resolver evaluated the unrelated CrewAI extra on Python 3.9; a direct venv install of the CI extras worked and the complete suite passed.
Fixes two fail-open gaps in the LangChain integration (part 1 of Drakon-Systems-Ltd/ShieldCortex-internal#130, items 1 and 2; items 3 and 4 will follow in a separate PR).
Problem
raise_on_block=Truedid not stop the run.ShieldCortexCallbackHandleris anAsyncCallbackHandlerand never setraise_error, which langchain_core defaults toFalse. The callback manager logged theValueErrorand the chain carried on.ChatOpenAI(callbacks=[handler]), but the handler only scanned inputs inon_chain_start/on_tool_start. A bare chat model or LLM fireson_chat_model_start/on_llm_start, which were not implemented.Change
raise_error = raise_on_block, so a BLOCK now propagates fromainvoke/astream.ContentBlockedError(ValueError)carries.resultand.source_identifier. Existingexcept ValueErrorcode keeps working.on_llm_start(langchain:llm_input) andon_chat_model_start(langchain:chat_input). Text is taken from string content and from{"type": "text"}blocks; non-text blocks are ignored. Duplicates within one call and empty strings are skipped. Both respectscan_inputs.ShieldCortexSyncCallbackHandlerforinvoke(). LangChain cannot propagate an async handler's exception out of a sync call, so the README now says to use the handler that matches the call mode. Scan and verdict logic is shared by both handlers.examples/langchain_callback.pynow say exactly what is scanned. They also state that onlyBLOCKacts today and that scan API errors are logged and the run continues (unchanged here; that is items 3 and 4)..[dev,langchain], so the LangChain tests actually run.Test plan
tests/integrations/test_langchain.py: 8 tests that use real langchain_core runnables (FakeListChatModel,FakeListLLM,RunnableLambda) and respx-mocked/v1/scan. They cover:ContentBlockedError(aValueError)scan_inputs=Falsesends no input scansinvoke()main'slangchain.pygives 7 failed, 1 passed.c7572132fad0a0ef33b50f8d9b9dda8a8ca1d581:ruff check src/: All checks passedmypy src/shieldcortex/ --ignore-missing-imports: no issues in 12 source filespytest tests/: 130 passed