fix(parser): MarkdownParser char limit#826
Merged
qin-ctx merged 1 commit intovolcengine:mainfrom Mar 21, 2026
Merged
Conversation
|
Failed to generate code suggestions for PR |
The token estimator (_estimate_token_count) uses a simplified heuristic
(0.3 tokens/non-CJK-non-space char) that can underestimate actual token
counts for dense code or technical content. This caused large sections to
pass the token check and be written as single L2 files, which then
exceeded embedding API limits (e.g., text-embedding-v4 8192 token cap).
Changes:
- Add `max_section_chars: int = 6000` to ParserConfig as a hard character
limit per section, guarding against token estimation errors
- `_smart_split_content`: enforces char limit per split chunk alongside
the existing token estimate limit; falls back to token-only splitting
when max_section_chars <= 0 to avoid range(n, 0) crash
- `_save_section`: requires both token AND char limits satisfied before
writing a section as a single file
- `_save_merged`: splits joined content via _smart_split_content when the
merged result exceeds max_section_chars, preventing token-small but
char-large sections from accumulating into oversized files
- `_parse_and_create_structure`: small-document fast-path now also checks
char count before writing as a single file
- `ParserConfig.validate()`: rejects max_section_chars <= 0
- `ParserConfig` class docstring: add max_section_chars attribute,
fix max_section_size description ("characters" -> "tokens")
- `ov.conf.example`: add max_section_chars to pdf parser block
Add 21 unit tests covering ParserConfig defaults and validation,
_smart_split_content char-limit enforcement, _save_section and
_save_merged char-limit gates, and _parse_and_create_structure
fast-path with and without headings (VikingFS mocked throughout).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ddd94fa to
3d9360d
Compare
qin-ctx
approved these changes
Mar 21, 2026
zeattacker
pushed a commit
to zeattacker/OpenViking
that referenced
this pull request
Mar 21, 2026
…er (volcengine#826) The token estimator (_estimate_token_count) uses a simplified heuristic (0.3 tokens/non-CJK-non-space char) that can underestimate actual token counts for dense code or technical content. This caused large sections to pass the token check and be written as single L2 files, which then exceeded embedding API limits (e.g., text-embedding-v4 8192 token cap). Changes: - Add `max_section_chars: int = 6000` to ParserConfig as a hard character limit per section, guarding against token estimation errors - `_smart_split_content`: enforces char limit per split chunk alongside the existing token estimate limit; falls back to token-only splitting when max_section_chars <= 0 to avoid range(n, 0) crash - `_save_section`: requires both token AND char limits satisfied before writing a section as a single file - `_save_merged`: splits joined content via _smart_split_content when the merged result exceeds max_section_chars, preventing token-small but char-large sections from accumulating into oversized files - `_parse_and_create_structure`: small-document fast-path now also checks char count before writing as a single file - `ParserConfig.validate()`: rejects max_section_chars <= 0 - `ParserConfig` class docstring: add max_section_chars attribute, fix max_section_size description ("characters" -> "tokens") - `ov.conf.example`: add max_section_chars to pdf parser block Add 21 unit tests covering ParserConfig defaults and validation, _smart_split_content char-limit enforcement, _save_section and _save_merged char-limit gates, and _parse_and_create_structure fast-path with and without headings (VikingFS mocked throughout). Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
The token estimator (
_estimate_token_count) inMarkdownParseruses a simplified heuristic (0.3 tokens/non-CJK-non-space char) that can underestimate actual token counts for dense code or technical content. This caused oversized L2 sections to pass the token check and be written as single files, which then exceeded embedding API limits (e.g. text-embedding-v4's 8192-token cap).Changes:
max_section_chars: int = 6000toParserConfigas a hard character limit per section, guarding against token estimation errors_smart_split_content: enforces char limit per chunk alongside the token estimate; guards againstmax_section_chars=0crash_save_section: requires both token AND char limits satisfied before writing a section as a single file_save_merged: splits joined content when the merged result exceedsmax_section_chars, preventing token-small but char-large sections from accumulating into oversized files_parse_and_create_structure: small-document fast-path now also checks char count before writing as a single fileParserConfig.validate()rejectsmax_section_chars <= 0ov.conf.exampleupdated withmax_section_charsin the pdf parser blockmax_section_charsattribute, fixmax_section_sizedescription ("characters" → "tokens")Test plan
pytest tests/parse/test_markdown_char_limit.py -v— 21 tests passov add-resource <large-markdown-file>completes without embedding token limit errors🤖 Generated with Claude Code