Skip to content

[Feature] Support fromjson in jinja2 chat template rendering #40366

@byjiang1996

Description

@byjiang1996

Feature request

GLM45 requires fromjson in jinja2 to deserialize str typed tool_calls.function.arguments to dict within chat template so it can iterate over arguments's k-v within jinja2 chat template.

{% for tc in m.tool_calls %}
{%- if tc.function %}
{%- set tc = tc.function %}
{%- endif %}
{{ '\n<tool_call>' + tc.name }}
{% set _args = tc.arguments | fromjson %}
{% for k, v in _args.items() %}
<arg_key>{{ k }}</arg_key>
<arg_value>{{ v \| tojson(ensure_ascii=False) if v is not string else v }}</arg_value>
{% endfor %}
</tool_call>{% endfor %}
{% endif %}

https://huggingface.co/zai-org/GLM-4.5/blob/main/chat_template.jinja#L75

Motivation

GLM45 requires fromjson in jinja2 to deserialize str typed tool_calls.function.arguments to dict within chat template so it can iterate over arguments's k-v within jinja2 chat template.

{% for tc in m.tool_calls %}
{%- if tc.function %}
{%- set tc = tc.function %}
{%- endif %}
{{ '\n<tool_call>' + tc.name }}
{% set _args = tc.arguments | fromjson %}
{% for k, v in _args.items() %}
<arg_key>{{ k }}</arg_key>
<arg_value>{{ v \| tojson(ensure_ascii=False) if v is not string else v }}</arg_value>
{% endfor %}
</tool_call>{% endfor %}
{% endif %}

https://huggingface.co/zai-org/GLM-4.5/blob/main/chat_template.jinja#L75

Your contribution

I will submit a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions