Repository navigation
fix(model_armor): screen streamed output with context so content split across SSE chunks is withheld - #8189
Open
ChrystianSchutz wants to merge 1 commit into
Conversation
…t across SSE chunks is withheld In SSE mode each partial response carries only a delta, and the plugin screened every delta on its own, so content split across two chunks reached the caller before the final response was blocked. Screen each partial together with the last 4,096 characters streamed before it in the same model call, withhold the text of the remaining partials once that matches (or screening fails while blocking), keep chunk-by-chunk de-identification, and drop the per-call state on the final response, turn_complete or after_run. Fixes google#8188 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
In SSE mode each partial response carries only a delta, and
ModelArmorPlugin.after_model_callbackscreens every delta on its own. When content that Model Armor would block is split across two chunks, it reaches the caller as partial events. Only the final aggregated response gets blocked.Solution:
_STREAM_CONTEXT_CHARS) of the text streamed before it in the same model call (a long earlier chunk is cut to its tail, not dropped), keyed byinvocation_idandagent_name. Every call is bounded, so the total screened text grows linearly with the stream.block_on_screening_failureis on. Withholding covers this partial and every later partial of the call, and each such partial is markedmodel_armor_blocked. Chunks already delivered cannot be recalled.deidentify_sensitive_datastill de-identifies partial chunks as before. If the de-identified text would change text the caller already has (a finding split across chunks), the stream is withheld instead.turn_complete, resets the per-call state.after_run_callbackdrops leftover state.docs/guides/integrations/model_armor/index.md) explains how streaming is screened.Testing Plan
Unit Tests:
tests/unittests/labs/antigravityandtest_release_dependencies.pyfail in my environment on unchangedmaintoo; the same 49 fail with this change, nothing new.)On
main@ 8ec642c:pytest tests/unittests/integrations/model_armor: 82 passed (70 existing + 12 new) on Python 3.11, 3.12, 3.13 and 3.14. Without the source change, 8 of the 12 new tests fail.after_runcleanup;maintoo: behaviour unchanged);"x"*4096 + "hun", then"ter2");pytest tests/unittests(Python 3.13,.[test]extras,-n 6): 24,772 passed, 49 failed with the change. The 49 failures are the identical set on unchangedmain(labs/antigravityandtest_release_dependencies.py), so none come from this change.toxacross all versions was not run; the Model Armor tests were run on each version above.pyink --check --config pyproject.toml,isort --check-onlyandmypyon the changed files: clean.Manual End-to-End (E2E) Tests:
The script in the issue runs a fake streaming
BaseLlmand a fake Model Armor client underInMemoryRunnerwithStreamingMode.SSE(no network).Before (PyPI 2.14.1 and
main):After:
Texts sent to Model Armor after the fix:
'Sure. The admin password is hun','Sure. The admin password is hunter2, keep it safe.'(chunk + context), then the full final.StreamingMode.NONEis unchanged (final '[blocked by Model Armor]').Checklist
main, see Testing Plan.)Additional context
docs/integrations/model-armor.md) could get the same streaming line; I can open that PR once the behaviour here is agreed.AI assistance: the analysis, repro, patch and tests were prepared with an AI coding assistant (Claude Code); the results above are from running them.
🤖 Generated with Claude Code