Repository navigation
Conversation
Assisted-by: Claude Code Signed-off-by: Rafael Vasquez <[email protected]>
ajbozarth
left a comment
There was a problem hiding this comment.
Some feedback from Claude: one regression in the truncation fallback, details inline.
Assisted-by: Claude Code Signed-off-by: Rafael Vasquez <[email protected]>
Dismissing my changes requested as all my items have been addressed, but I would rather defer approval to Jake
markstur
left a comment
There was a problem hiding this comment.
Re-checked at debb394a. One correctness gap, one should-fix, and a few smaller items.
Request change
Truncation fallback doesn't fire unless MAX_NEW_TOKENS is in model_options — mellea/backends/huggingface.py:2661-2675 derives "length" only when mot._call.model_options contains MAX_NEW_TOKENS. When the cap comes from model.generation_config (or transformers' default max_length) instead — i.e. the user set it neither per-call nor at backend construction — generation stops mid-reasoning with no EOS, finish_reasons stays None, truncated is False, and the partial reasoning lands in mot.value. That's the scenario this PR exists to fix. The tests always set the option explicitly (test_huggingface_thinking.py:439, 469, 502), so only the option-present path is covered, and the new paragraph in docs/docs/how-to/configure-model-options.md states the empty-result.value behavior unconditionally. Suggested fix: fall back to self._model.generation_config.max_new_tokens when the per-call option is absent.
Should fix
_prompt_opens_think is evaluated eagerly — huggingface.py:2733 computes _prompt_opens_think(tokenizer, input_ids) as an argument on every eligible non-streaming call, but _split_think_tags only reads it when the close tag is absent from the output (:397). In the common case that's a full-prompt tokenizer.decode wasted per call. Computing it only when native is None and truncated and _THINK_CLOSE_TAG not in raw_value keeps it off the hot path.
Comments
huggingface.py:2749—cache_key = id(mot.value)collides across thunks whenever the split yields an empty string: the truncated branch returns the literal""(:401), which CPython interns as a singleton, so two truncated generations share a cache key. Todaycache_gethas no production callers (onlytest_huggingface_thinking.py:686), so the practical effect is an overwritten LRU entry rather than a wrong lookup — but the keying scheme will bite ifcache_getgains a caller. Worth keying on something per-thunk.huggingface.py:302—input_ids["input_ids"]sits outside thetry:at 303, so a mapping without that key raisesKeyErrorinstead of taking the documentedFalsefallback. Same shape at:332. Hypothetical today (the only caller passes a realBatchEncoding); moving the subscript inside thetrywould match the contract.huggingface.py:380, 2692, 2709— the new#1799/#1800references extend issue-number pointers inmellea/source comments and docstrings. Per this repo's convention that history belongs in commits, PR descriptions, and tests, these would read better as plain statements of the limitation. The pre-existing#1604refs are inherited; the new lines add more.- The tensor-vs-dict normalization idiom (
input_ids if isinstance(input_ids, torch.Tensor) else input_ids["input_ids"]) now appears five times (:302, :332, :2466, :2593, :2634), two of them added here. A module-level helper would keep any future fix (e.g. theKeyErrorguard above) in one place. - For the record, since the docstring marks it deliberate: a stop string firing inside the think block (or EOS mid-think) yields
"stop", not truncation, so partial reasoning stays inmot.value. The two-signal design documents this tradeoff; fine to leave as is.
🤖 Generated with Claude Code
Signed-off-by: Rafael Vasquez <[email protected]> Assisted-by: Claude Code
Pull Request
Issue
Closes #1604, with follow-up issues opened #1799 and #1800.
Description
Improves thinking/answer parsing on the non-streaming
LocalHFBackendchat path:Transformers-native parsing —
_native_parse_thinking()callstokenizer.parse_response()when the tokenizer declares aresponse_template, falling back to string splitting on no template, parse error, tool calls, or no reasoning to handle any future model that declares a template.post_processing()tries the native parse first, then the string splitter. Remaining#1604doc/comment pointers are repointed to:Truncated-reasoning fallback —
_prompt_opens_think()detects a prompt that ends inside an unclosed<think>block (e.g. granite-4.2). When generation stops on length before</think>, the unfinished reasoning goes tomot.thinkingwith an emptymot.valueinstead of leaking into the answer. Both signals are required: the prompt (or output) opened a<think>block, and generation stopped on length. The prompt-open check decodes the whole prompt, so it is evaluated lazily, only when the splitter would use it.Structured output guard — the split gate now includes
_format is None, so structured output is never split.Finish-reason ordering — token count and finish-reason derivation now run before the split gate in
post_processing(), somot.generation.finish_reasonsis available for the truncation decision.Length stop detected from every cap source — the finish-reason derivation previously read only
ModelOption.MAX_NEW_TOKENSfrom the per-call options, so a cap set anywhere else produced no"length"and partial reasoning leaked intomot.value. It now resolves the cap the waygenerate()merges it (_resolve_generation_caps): per-call option ormax_length, then a per-callgeneration_config=, then the model'sgeneration_config.max_new_tokenswins overmax_length, andmax_lengthis compared against total sequence length, since it counts the prompt. When nothing is set anywhere, transformers' built-in default cap (20 new tokens) applies.Small cleanups — a shared
_input_ids_tensor()helper replaces five copies of the tensor-vs-mapping idiom, and the two newtrysites now treat a mapping withoutinput_idsas the documented fallback instead of raisingKeyError.The how-to (
configure-model-options.md) now documents the empty-result.valuebehavior on a length stop and where the limit can be set.Testing
Attribution
Adding a new component, requirement, sampling strategy, or tool?
If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.
NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.