Skip to content

fix(gliner2): count compiled inference prompts - #794

Merged
svonava merged 1 commit into
mainfrom
codex/issue-382-exact-prompt
Oct 18, 2026
Merged

svonava merged 1 commit into
mainfrom
codex/issue-382-exact-prompt

Conversation

@svonava

@svonava svonava commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

GLiNER2 prompt admission used fixed per-label estimates, which can undercount repeated JSON choice prefixes and reject or admit a different schema than the loaded compiler would encode. Count the loaded inference compiler’s actual tokens for entities, relations, classification and structured extraction, including markers and document-side prefixes.

The counter uses a private processor/tokenizer once per load, leaves serving state and global RNG unchanged, retains the bounded count cache and character guards, and propagates compiler errors. Classification uses the same pure rendering stages to avoid the upstream singleton random draw. Existing document metering, task/error precedence and whole-document refusal are unchanged.

Addresses #382 item 2 only. The remaining reported/billed window-metering policy item stays open.

Validation:

  • 685 related adapter, LoRA, API and queue tests passed; scoped Ruff and typecheck passed.
  • All 32 new compiler controls passed with GLiNER2 1.3.1, 1.3.2 and 2.0.0 (the latter with Transformers 5.20.1).
  • Independent real-tokenizer checks passed 99 comparisons across all three versions and four task routes, including state/RNG isolation. This is tokenizer/compiler validation, with no model inference or GPU claim.
  • Five negative controls using the former estimator failed at the intended boundary/choice-prefix assertions.

Summary by CodeRabbit

  • Improvements
    • Prompt limits now reflect tokens in the compiled inference prompt across supported extraction tasks, improving the accuracy of prompt admission and document token usage.
    • Structured extraction also checks property names and enum values against character limits before building the prompt.
    • Prompt counting no longer changes the serving processor’s mode, caches, or random state.

@svonava
svonava requested a review from a team as a code owner October 18, 2026 05:16
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 42d76956-50b8-4fc3-acbf-e60f8893aaf0
📥 Commits

Reviewing files that changed from the base of the PR and between 2281ba8 and 1edd819.

📒 Files selected for processing: 10
  • packages/sie_server/src/sie_server/adapters/gliner2/_prompt.py
  • packages/sie_server/src/sie_server/adapters/gliner2/adapter.py
  • packages/sie_server/tests/adapters/test_gliner2.py
  • packages/sie_server/tests/adapters/test_gliner2_complete_text.py
  • packages/sie_server/tests/adapters/test_gliner2_input_errors.py
  • packages/sie_server/tests/adapters/test_gliner2_long_text.py
  • packages/sie_server/tests/adapters/test_gliner2_lora.py
  • packages/sie_server/tests/adapters/test_gliner2_prompt.py
  • packages/sie_server/tests/adapters/test_gliner2_validation_order.py
  • packages/sie_server/tests/test_queue_executor.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

GLiNER2 prompt admission now counts tokens from compiled inference schemas for entity, relation, classification, and structured extraction. A private inference processor performs counting. The adapter checks the measured count before document processing. Tests cover counting, limits, cache handling, and processor lifecycle.

Changes: GLiNER2 prompt counting

Layer / File(s) Summary
Private prompt counter and schema construction
packages/sie_server/src/sie_server/adapters/gliner2/_prompt.py, packages/sie_server/tests/adapters/test_gliner2_prompt.py
A private inference processor counts compiled schemas. Classification prompts use a dedicated renderer. Structured fields are added from parsed field specifications. Tests check count accuracy, processor state, and schema immutability.
Adapter schema counting and prompt admission
packages/sie_server/src/sie_server/adapters/gliner2/adapter.py, packages/sie_server/tests/adapters/test_gliner2_prompt.py
Entity, relation, classification, and structured extraction paths count their compiled schemas. The adapter checks prompt limits using the counted tokens and cache key. Tests cover admission boundaries, validation order, caching, and counter lifecycle.
Adapter tests and test fixtures
packages/sie_server/tests/adapters/test_gliner2.py, packages/sie_server/tests/adapters/test_gliner2_complete_text.py, packages/sie_server/tests/adapters/test_gliner2_input_errors.py, packages/sie_server/tests/adapters/test_gliner2_long_text.py, packages/sie_server/tests/adapters/test_gliner2_lora.py, packages/sie_server/tests/adapters/test_gliner2_validation_order.py, packages/sie_server/tests/test_queue_executor.py
Existing tests and fixtures now provide prompt-count stubs and parsed field specifications. Relation prompt tests build a relations schema instead of using the removed fixed estimate.

Sequence Diagram(s)

sequenceDiagram
  participant GLiNER2Adapter
  participant create_schema
  participant prompt_counter
  participant PrivateProcessor
  participant PromptLimit
  GLiNER2Adapter->>create_schema: build inference schema
  create_schema-->>GLiNER2Adapter: return schema
  GLiNER2Adapter->>prompt_counter: count schema tokens
  prompt_counter->>PrivateProcessor: format empty-text record
  PrivateProcessor-->>prompt_counter: return input IDs
  prompt_counter-->>GLiNER2Adapter: return token count
  GLiNER2Adapter->>PromptLimit: check count with cache key
Loading

Suggested reviewers: krisztian-gajdar

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 1edd8

GLiNER2 prompt limits now count compiled prompts. No unresolved merge-blocking risk remains in the supplied context.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 21.43% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 56 functions across 10 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: counting compiled GLiNER2 inference prompts.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@svonava
svonava merged commit f3b34e2 into main Oct 18, 2026
21 checks passed
@svonava
svonava deleted the codex/issue-382-exact-prompt branch October 18, 2026 05:46
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.

1 participant