Skip to content

fix(studio): improve tool-calling re-prompt for small models - #4783

Merged
danielhanchen merged 1 commit into
mainfrom
fix/studio-tool-reprompt-tuning
Apr 2, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix/studio-tool-reprompt-tuning

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

  • Raise _REPROMPT_MAX_CHARS from 500 to 2000 so longer responses (full code blocks, multi-paragraph plans) still trigger the plan-without-action re-prompt
  • Raise _MAX_REPROMPTS from 1 to 3 to give small models more chances to switch from narrating to actually calling tools
  • Use direct, imperative re-prompt language that small models follow more reliably
  • Strengthen the system prompt tool nudge to explicitly redirect code generation to the python tool
  • Fix tool-calling detection for chat templates that use Jinja whitespace-trimming syntax ({%- if tools -%})

Context

PR #4769 added the plan-without-action re-prompt to prevent small models from stalling on tool-calling tasks. However, two gaps remained:

  1. Char limit too low: Models that generate full HTML/code responses (1000+ chars) never triggered the re-prompt because _REPROMPT_MAX_CHARS=500 filtered them out. The model would write an entire weather dashboard in HTML instead of calling python to execute code.

  2. One retry not enough: With _MAX_REPROMPTS=1, small models got a single nudge. They would acknowledge the nudge but still respond with text instead of a tool call on the second attempt.

  3. Template detection gap: Some chat templates use {%- if tools -%} (with trailing dash for whitespace trimming) which wasn't matched by the existing tool_markers list, causing supports_tools to be incorrectly set to False.

Test plan

  • Load a small GGUF model (e.g. Qwen3.5-4B) with tool calling enabled
  • Send "Create a live weather dashboard in HTML" -- verify the model calls web_search or python instead of writing code directly
  • Send "What are current exchange rates for USD to EUR?" -- verify web_search is called
  • Load a model whose chat template uses {%- if tools -%} syntax -- verify supports_tools is detected as True
  • Verify large models (>9B) are unaffected by the changes

Small GGUF models (<9B) frequently generate full code or lengthy
explanations instead of calling tools, bypassing the existing
plan-without-action re-prompt mechanism. Three issues:

1. _REPROMPT_MAX_CHARS=500 was too low -- models that output full
   HTML/code responses (often 1000+ chars) never triggered the
   re-prompt at all, since it only fires on short responses.

2. _MAX_REPROMPTS=1 gave the model only one chance to comply.
   Small models often need 2-3 nudges before switching from
   text generation to tool calling.

3. The re-prompt text ("Please use the available tools...") was
   too polite for small models to follow reliably.

4. Tool-calling detection missed chat templates using Jinja
   whitespace-trimming syntax ({%- if tools -%}) since only
   ({%- if tools %}) and ({% if tools %}) were checked.

Changes:
- Raise _REPROMPT_MAX_CHARS from 500 to 2000 so longer responses
  (code blocks, multi-paragraph plans) still trigger re-prompts
- Raise _MAX_REPROMPTS from 1 to 3 for more retry budget
- Use direct, imperative re-prompt language that small models
  follow more reliably ("STOP. You MUST call a tool NOW.")
- Strengthen the system prompt tool nudge to explicitly forbid
  outputting code blocks (redirect to the python tool instead)
- Add Jinja whitespace-trimmed variants to the tool_markers
  list so all template styles are detected correctly

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 148a0653b6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

" Always call tools directly."
" IMPORTANT: Always call tools directly -- never write code yourself."
" Never describe what you plan to do -- just call the tool immediately."
" For any code request, call the python tool. For any factual question, call web_search."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid hardcoding unavailable tools in tool-use nudge

This nudge now instructs the model to call python for code and web_search for facts even when those tools were explicitly filtered out via enabled_tools (for example, ['terminal']). In that configuration the prompt contradicts the provided tool schema, which can steer small models into invalid tool names and waste tool iterations instead of using the only allowed tool.

Useful? React with 👍 / 👎.

"the task instead of describing what to do."
"STOP. Do NOT write code or explain. "
"You MUST call a tool NOW. "
"Call web_search or python immediately."

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Re-prompt with only currently enabled tool names

The recovery re-prompt always says to call web_search or python, but generate_chat_completion_with_tools can be invoked with a restricted tool list that excludes one or both names. In those cases the fallback path pushes the model toward unsupported calls, which can consume retries and still fail to reach a valid tool invocation.

Useful? React with 👍 / 👎.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request increases the reprompt limits and expands tool detection markers in the llama_cpp backend. It also updates tool-use nudges to be more forceful in directing the model to use specific tools (python and web_search) instead of generating code or explanations. Feedback suggests simplifying the reprompt text and consolidating instructions in the tool action nudge to reduce redundancy and improve clarity.

Comment on lines +2662 to +2664
"STOP. Do NOT write code or explain. "
"You MUST call a tool NOW. "
"Call web_search or python immediately."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The instruction 'Call web_search or python immediately' is redundant given the previous sentence 'You MUST call a tool NOW'. It is better to keep the prompt concise to avoid confusing the model.

Suggested change
"STOP. Do NOT write code or explain. "
"You MUST call a tool NOW. "
"Call web_search or python immediately."
"STOP. Do NOT write code or explain. "
"You MUST call a tool NOW."

Comment on lines +101 to +102
" For any code request, call the python tool. For any factual question, call web_search."
" Do NOT output code blocks -- use the python tool instead."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The instructions 'For any code request, call the python tool. For any factual question, call web_search.' and 'Do NOT output code blocks -- use the python tool instead.' are repetitive and can be consolidated for better clarity.

    " IMPORTANT: Always call tools directly -- never write code yourself."
    " Never describe what you plan to do -- just call the tool immediately."
    " Use the python tool for code requests and web_search for factual questions."

@danielhanchen
danielhanchen merged commit 4f9986e into main Apr 2, 2026
5 checks passed
@danielhanchen
danielhanchen deleted the fix/studio-tool-reprompt-tuning branch April 2, 2026 15:59
shibizhao pushed a commit to shibizhao/unsloth-npu that referenced this pull request Apr 7, 2026
…ai#4783)

Small GGUF models (<9B) frequently generate full code or lengthy
explanations instead of calling tools, bypassing the existing
plan-without-action re-prompt mechanism. Three issues:

1. _REPROMPT_MAX_CHARS=500 was too low -- models that output full
   HTML/code responses (often 1000+ chars) never triggered the
   re-prompt at all, since it only fires on short responses.

2. _MAX_REPROMPTS=1 gave the model only one chance to comply.
   Small models often need 2-3 nudges before switching from
   text generation to tool calling.

3. The re-prompt text ("Please use the available tools...") was
   too polite for small models to follow reliably.

4. Tool-calling detection missed chat templates using Jinja
   whitespace-trimming syntax ({%- if tools -%}) since only
   ({%- if tools %}) and ({% if tools %}) were checked.

Changes:
- Raise _REPROMPT_MAX_CHARS from 500 to 2000 so longer responses
  (code blocks, multi-paragraph plans) still trigger re-prompts
- Raise _MAX_REPROMPTS from 1 to 3 for more retry budget
- Use direct, imperative re-prompt language that small models
  follow more reliably ("STOP. You MUST call a tool NOW.")
- Strengthen the system prompt tool nudge to explicitly forbid
  outputting code blocks (redirect to the python tool instead)
- Add Jinja whitespace-trimmed variants to the tool_markers
  list so all template styles are detected correctly
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