Skip to content

Parse context_budget explicitly with input validation - #265

Merged
Qianhui Wu (QianhuiWu) merged 1 commit into
mainfrom
context-budget-parsing
Nov 24, 2026
Merged

Qianhui Wu (QianhuiWu) merged 1 commit into
mainfrom
context-budget-parsing

Conversation

@QianhuiWu

Copy link
Copy Markdown
Collaborator

What does this PR do?

context_budget (e.g. "+100", "*1.5") in compress_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.

  • All documented formats ("+100", "*1.5", "*2", "-50", "/2") behave the same as before.
  • An empty string still leaves the budget unchanged.
  • Unsupported formats (e.g. "100", "+100*2", non-string values, "/0") now raise a clear ValueError with the expected format, instead of an arbitrary Python error.
  • Bumps the version to 0.2.3.

Testing

  • Ran the existing test suite (tests/test_llmlingua.py, tests/test_longllmlingua.py, tests/test_llmlingua2.py) locally with transformers==4.44.2 on CPU: all tests pass, and compressed outputs are unchanged.

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.
@QianhuiWu

Copy link
Copy Markdown
Collaborator Author

CI failures here are pre-existing and unrelated to this change:

  • ubuntu-latest / 3.9: setup-python fails because Python 3.9 is no longer available on the ubuntu-latest image (now Ubuntu 26.04).
  • ubuntu-latest / 3.10, 3.11: CI installs transformers 5.x (unpinned), and iterative_compress_prompt fails with ValueError: too many values to unpack when iterating past_key_values, since 5.x returns a DynamicCache object instead of tuples. This code path is not touched by this PR.
  • windows-2019: jobs stay queued because the windows-2019 runner image has been retired.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 jue-zhang self-assigned this Nov 24, 2026
@QianhuiWu
Qianhui Wu (QianhuiWu) merged commit 2c6af3f into main Nov 24, 2026
5 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants