Repository navigation
fix(studio): improve tool-calling re-prompt for small models - #4783
Conversation
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
There was a problem hiding this comment.
💡 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." |
There was a problem hiding this comment.
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." |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| "STOP. Do NOT write code or explain. " | ||
| "You MUST call a tool NOW. " | ||
| "Call web_search or python immediately." |
There was a problem hiding this comment.
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.
| "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." |
| " 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." |
There was a problem hiding this comment.
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."…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
Summary
_REPROMPT_MAX_CHARSfrom 500 to 2000 so longer responses (full code blocks, multi-paragraph plans) still trigger the plan-without-action re-prompt_MAX_REPROMPTSfrom 1 to 3 to give small models more chances to switch from narrating to actually calling tools{%- 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:
Char limit too low: Models that generate full HTML/code responses (1000+ chars) never triggered the re-prompt because
_REPROMPT_MAX_CHARS=500filtered them out. The model would write an entire weather dashboard in HTML instead of callingpythonto execute code.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.Template detection gap: Some chat templates use
{%- if tools -%}(with trailing dash for whitespace trimming) which wasn't matched by the existingtool_markerslist, causingsupports_toolsto be incorrectly set toFalse.Test plan
web_searchorpythoninstead of writing code directlyweb_searchis called{%- if tools -%}syntax -- verifysupports_toolsis detected asTrue