refactor(clients): extract shared think-tag parsing into forge.prompts.think_tags#112
Merged
Conversation
…s.think_tags
The <think>/[THINK] delimiter regex and its extraction helper were
duplicated: a full copy (capture groups + _extract_think_tags) in
clients/llamafile.py and a second strip-only copy of the regex in
prompts/templates.py. Adding reasoning parity to other clients would have
meant a third copy.
Collapse them into one source of truth, forge/prompts/think_tags.py
(THINK_TAG_RE + extract_think_tags):
- llamafile.py re-imports it under the historical private name
(extract_think_tags as _extract_think_tags) so existing call sites and
the test import keep working unchanged.
- templates.py imports the shared regex; its only use is .sub("", ...),
so the added capture groups are behavior-neutral.
Placed under forge.prompts (not forge.clients) because clients/__init__.py
eagerly imports every client, which would create an import cycle if
templates.py imported a clients-based helper.
Pure refactor — no behavior change. 163 unit tests pass
(llamafile + templates + vllm).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EpuVYCYeb1DhWfynCVyA6a
This was referenced Jun 20, 2026
Merged
antoinezambelli
added a commit
that referenced
this pull request
Jun 20, 2026
Bump version to 0.7.6 and add the CHANGELOG entry for the batch since 0.7.5: two Ollama backend fixes (#111, #115), inline <think> reasoning capture on vLLM + Ollama (#110), the shared think-tag parsing refactor (#112), the consolidated test-double fixture (#76), and the 16GB-tier eval models (#107). Co-authored-by: Claude Opus 4.8 (1M context) <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.
What
Extract the duplicated
<think>/[THINK]think-tag parsing into one shared module,forge/prompts/think_tags.py(THINK_TAG_RE+extract_think_tags).Previously the regex + helper lived in
clients/llamafile.py, with a second strip-only copy of the regex inprompts/templates.py. This collapses both into a single source of truth.Why
Foundation for upcoming cross-client reasoning-parity work (see #110). Adding reasoning extraction to other clients would otherwise have meant a third copy of the same regex.
Details
llamafile.pyre-imports the helper under its historical private name (extract_think_tags as _extract_think_tags), so existing call sites and the test import keep working unchanged.templates.pyimports the shared regex; its only use is.sub("", ...), so the shared version's capture groups are behavior-neutral there.forge.prompts(notforge.clients) becauseclients/__init__.pyeagerly imports every client — a clients-based helper imported bytemplates.pywould create an import cycle.Scope
Pure refactor — no behavior change. No client behavior is altered; the actual #110 fix +
openai_compat/ollamareasoning parity will follow in a separate PR stacked on this.Testing
163 unit tests pass (
test_llamafile_client.py,test_templates.py,test_vllm_client.py). No import cycle (verified from multiple entry points).🤖 Generated with Claude Code
https://claude.ai/code/session_01EpuVYCYeb1DhWfynCVyA6a