Repository navigation
Parse context_budget explicitly with input validation - #265
Merged
Merged
Conversation
context_budget (e.g. "+100", "*1.5") is now parsed with a strict pattern of a single arithmetic operator followed by a number and applied directly. Unsupported formats raise a clear ValueError instead of an arbitrary Python error. Documented usage is unchanged. Also bump version to 0.2.3.
Collaborator
Author
|
CI failures here are pre-existing and unrelated to this change:
The same Unit Test workflow is also failing on other recent PRs, including docs-only ones. I ran the full test suite locally with transformers==4.44.2, and all tests pass. |
There was a problem hiding this comment.
🟡 Changes recommended
Focused unit tests are missing for the helper’s supported operators, operands, no-op behavior, and error cases, so the passing suite does not verify the new validation.
1 open issue
What changed in this PR
This PR replaces eval-based context budget handling with explicit parsing and validation in the prompt compression pipeline.
Changes:
- Adds
apply_context_budget()for single-operator arithmetic expressions and empty-string no-ops. - Rejects unsupported inputs and division by zero with
ValueError. - Bumps the package version to 0.2.3.
| File | Description |
|---|---|
llmlingua/version.py |
Updates the patch version to 0.2.3. |
llmlingua/prompt_compressor.py |
Adds the validation helper and replaces the existing eval call. |
Review effort: Balanced
| _CONTEXT_BUDGET_PATTERN = re.compile(r"^\s*([+\-*/])\s*(\d+(?:\.\d+)?)\s*$") | ||
|
|
||
|
|
||
| def apply_context_budget(target_token: float, context_budget: str) -> float: |
jue-zhang
approved these changes
Nov 24, 2026
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.

What does this PR do?
context_budget(e.g."+100","*1.5") incompress_prompt()is now parsed with a dedicated helper,apply_context_budget(). The helper accepts exactly one arithmetic operator (+,-,*,/) followed by a non-negative number, then applies it to the context-level token budget."+100","*1.5","*2","-50","/2") behave the same as before."100","+100*2", non-string values,"/0") now raise a clearValueErrorwith the expected format, instead of an arbitrary Python error.Testing
tests/test_llmlingua.py,tests/test_longllmlingua.py,tests/test_llmlingua2.py) locally withtransformers==4.44.2on CPU: all tests pass, and compressed outputs are unchanged.