GGUF
conversational

lmstudio temp fix for tool calling

#1
by rkuovc - opened

UPDATE: LM Studio 0.4.5 (Build 1) fixed the issue, and stock prompt template works fine now!


I was getting "Failed to parse tool call" for js-sandbox and "Error rendering prompt with jinja template" for multiple files in chat (single file worked) and when in opencode some tool calling didn't work either. So I pasted the prompt template to fix these two issues and now tool calling works in both lmstudio and opencode.

Hope this helps someone, but the generated template fix looks pretty bruteforced, maybe theres a better way?

Here's the template:

{%- set image_count = namespace(value=0) %}
{%- set video_count = namespace(value=0) %}

{%- macro render_content(content, do_vision_count) %}
    {%- if content is string %}
        {{- content }}
    {%- elif content is iterable and content is not mapping %}
        {%- for item in content %}
            {%- if item.type == 'image' or 'image' in item %}
                {%- if do_vision_count %} {%- set image_count.value = image_count.value + 1 %} {%- endif %}
                {{- ('Picture ' ~ image_count.value ~ ': ' if add_vision_id else '') ~ '<|vision_start|><|image_pad|><|vision_end|>' }}
            {%- elif item.type == 'video' or 'video' in item %}
                {%- if do_vision_count %} {%- set video_count.value = video_count.value + 1 %} {%- endif %}
                {{- ('Video ' ~ video_count.value ~ ': ' if add_vision_id else '') ~ '<|vision_start|><|video_pad|><|vision_end|>' }}
            {%- elif 'text' in item %}
                {{- item.text }}
            {%- endif %}
        {%- endfor %}
    {%- endif %}
{%- endmacro %}

{%- if tools %}
    {{- '<|im_start|>system\n# Tools\n\nYou have access to the following functions:\n\n<tools>\n' }}
    {%- for tool in tools %}
        {{- tool | tojson ~ '\n' }}
    {%- endfor %}
    {{- '</tools>\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n{"name": "function_name", "arguments": {"param_1": "value"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: a valid JSON object must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
    {%- if messages and messages[0].role == 'system' %}
        {{- '\n\n' ~ render_content(messages[0].content, false)|trim }}
    {%- endif %}
    {{- '<|im_end|>\n' }}
{%- elif messages and messages[0].role == 'system' %}
    {{- '<|im_start|>system\n' ~ render_content(messages[0].content, false)|trim ~ '<|im_end|>\n' }}
{%- endif %}

{%- set ns = namespace(searching=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
    {%- if ns.searching and message.role == "user" %}
        {%- set content = render_content(message.content, false)|trim %}
        {%- if not (content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
            {%- set ns.searching = false %}
            {%- set ns.last_query_index = (messages|length - 1) - loop.index0 %}
        {%- endif %}
    {%- endif %}
{%- endfor %}

{%- for message in messages %}
    {%- set content = render_content(message.content, true)|trim %}
    {%- if message.role == "system" and not loop.first %}
        {{- '<|im_start|>system\n' ~ content ~ '<|im_end|>\n' }}
    {%- elif message.role == "user" %}
        {{- '<|im_start|>user\n' ~ content ~ '<|im_end|>\n' }}
    {%- elif message.role == "assistant" %}
        {{- '<|im_start|>assistant\n' }}
        
        {%- set reasoning = message.reasoning_content | default('', true) %}
        {%- if not reasoning and '</think>' in content %}
            {%- set reasoning = content.split('</think>')[0].split('<think>')[-1]|trim %}
            {%- set content = content.split('</think>')[-1]|trim %}
        {%- endif %}
        
        {%- if loop.index0 > ns.last_query_index and reasoning %}
            {{- '<think>\n' ~ reasoning ~ '\n</think>\n\n' }}
        {%- endif %}
        
        {{- content }}
        
        {%- if message.tool_calls %}
            {%- for tool_call in message.tool_calls %}
                {%- set tc = tool_call.function | default(tool_call) %}
                {%- set args = tc.arguments | default('{}') %}
                {%- set args_str = args if args is string else args | tojson %}
                {{- '\n<tool_call>\n{"name": "' ~ tc.name ~ '", "arguments": ' ~ args_str ~ '}\n</tool_call>' }}
            {%- endfor %}
        {%- endif %}
        {{- '<|im_end|>\n' }}
    {%- elif message.role == "tool" %}
        {%- if loop.previtem and loop.previtem.role != "tool" %} {{- '<|im_start|>user' }} {%- endif %}
        {{- '\n<tool_response>\n' ~ content ~ '\n</tool_response>' }}
        {%- if loop.last or (loop.nextitem and loop.nextitem.role != "tool") %} {{- '<|im_end|>\n' }} {%- endif %}
    {%- endif %}
{%- endfor %}

{%- if add_generation_prompt %}
    {{- '<|im_start|>assistant\n' ~ ('<think>\n' if enable_thinking|default(true) else '<think>\n\n</think>\n\n') }}
{%- endif %}

LM Studio build 0.4.5+2 has just fixed it, I reverted back to the default Prompt Template from the one posted above and it works fine.

LM Studio build 0.4.5+2 has just fixed it, I reverted back to the default Prompt Template from the one posted above and it works fine.

The OpenClaw call still throws an error, but the one provided by the original poster works fine.

Sign up or log in to comment