Skip to content

refactor(clients): extract shared think-tag parsing into forge.prompts.think_tags#112

Merged
antoinezambelli merged 1 commit into
mainfrom
az/think-tags-helper
Jun 20, 2026
Merged

refactor(clients): extract shared think-tag parsing into forge.prompts.think_tags#112
antoinezambelli merged 1 commit into
mainfrom
az/think-tags-helper

Conversation

@antoinezambelli

Copy link
Copy Markdown
Owner

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 in prompts/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.py re-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.py imports the shared regex; its only use is .sub("", ...), so the shared version's capture groups are behavior-neutral there.
  • Placed under forge.prompts (not forge.clients) because clients/__init__.py eagerly imports every client — a clients-based helper imported by templates.py would create an import cycle.

Scope

Pure refactor — no behavior change. No client behavior is altered; the actual #110 fix + openai_compat/ollama reasoning 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

…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
@antoinezambelli antoinezambelli merged commit 1830706 into main Jun 20, 2026
2 checks passed
@antoinezambelli antoinezambelli deleted the az/think-tags-helper branch June 20, 2026 05:36
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>
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