Skip to content

fix(embeddings): match a third OpenAI token-limit error phrasing - #404

Merged
ofriw merged 1 commit into
chunkhound:mainfrom
AmirF194:fix/210-token-limit-classifier-phrase-miss
Sep 8, 2026
Merged

ofriw merged 1 commit into
chunkhound:mainfrom
AmirF194:fix/210-token-limit-classifier-phrase-miss

Conversation

@AmirF194

@AmirF194 AmirF194 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

_embed_batch_internal's token-limit classifier only recognizes two phrasings from the OpenAI API: "maximum context length" + "tokens", or "tokens" + "max" + "per request". The reported error, the input length exceeds the context length, matches neither, so it falls through to raise instead of routing through the existing split-and-retry logic in handle_token_limit_error, and the whole batch fails outright.

This adds a third condition matching "input length exceeds" + "context length", following the same style as the two existing ones.

Verification:

  • New test (tests/unit/test_openai_provider_token_limit.py) reproduces the reported message against a mocked BadRequestError: fails on main (error propagates), passes on this branch (batch splits and completes). A second test pins that an unrelated BadRequestError still raises.
  • Full tests/unit/ suite (1799 tests) passes unchanged.
  • ruff check / ruff format --check clean on the changed lines; mypy clean on the changed file.
  • I did not build the Rust extension or run the Windows/macOS legs; this change is pure Python and doesn't touch that surface.

_embed_batch_internal's classifier only recognized "maximum context
length"+"tokens" or "tokens"+"max"+"per request". The reported message
"the input length exceeds the context length" matches neither, so the
error propagated instead of triggering the existing split-and-retry
path in handle_token_limit_error.

Fixes chunkhound#210

@ofriw ofriw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you so much for this valuable contribution! LGTM

@ofriw
ofriw added this pull request to the merge queue Sep 8, 2026
Merged via the queue into chunkhound:main with commit ad4c95f Sep 8, 2026
17 checks passed
@AmirF194

AmirF194 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Appreciate it. This one closes the last of the three OpenAI token-limit phrasings the classifier was missing, so the split-retry path should catch all of them now.

@AmirF194
AmirF194 deleted the fix/210-token-limit-classifier-phrase-miss branch September 8, 2026 09:14
@ofriw ofriw mentioned this pull request Sep 8, 2026
7 tasks done
HananKavitzAmat added a commit to HananKavitzAmat/chunkhound that referenced this pull request Sep 15, 2026
…e matching

The 400 -> context-length classifier matched a bare "context", or any body
containing both "token" and "limit". Python's live path uses three
conjunctive predicates: ("maximum context length" AND "tokens"), ("tokens"
AND "max" AND "per request"), or ("input length exceeds" AND "context
length"). The Rust set was a strict superset, so nothing was missed -- the
problem was the other direction.

`ContextLengthExceeded` is the only error `embed_with_split` recovers from,
and it is excluded from retry, so a misclassified 400 recurses through
batch halving and issues up to 2N-1 requests against an endpoint that
already refused the batch (N is `embed_batch_size`, default 200). It then
records "input exceeds the provider context limit" for every chunk, burying
the real cause -- a quota error or a rejected parameter reads to the user as
"your chunks are too long".

Extract the predicate into `common.rs::is_context_length_error` and call it
from both providers. Sharing it is the point: the copies were
character-for-character identical, this classifier has needed three upstream
revisions (most recently chunkhound#404), and a per-provider copy is the likeliest way
a future fix lands in one file only.

Case-insensitivity is kept and documented as a deliberate widening over
Python's case-sensitive checks -- it only adds bodies differing by
capitalisation, and errs toward the recoverable path.

Tests: unit tests for all three phrasings plus the non-length bodies that
the loose version wrongly accepted, and a contract test asserting a
non-length 400 issues exactly one request. Verified against a build with the
loose predicate restored: the contract test fails with "3 requests issued
where 1 was expected" while the three positive tests still pass, so the new
test is the discriminating one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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.

2 participants