trl icon indicating copy to clipboard operation
trl copied to clipboard

GLM-4-MoE tool-call responses parse with `<|observation|>` as content

Open albertvillanova opened this issue 1 month ago • 3 comments

GLM-4-MoE ends an assistant turn that makes tool calls with <|observation|>, one of the model's end-of-sequence tokens, so a generated tool-call completion ends with it. parse_response keeps that token as the message content instead of dropping it. The other end-of-turn tokens (<|user|>, <|endoftext|>) are dropped. As a result, every GLM-4-MoE tool-call message parsed by GRPOTrainer carries content: '<|observation|>'. That text is then rendered back into the conversation before the tool result.

Reproduction

from transformers import AutoTokenizer
from trl import add_response_schema
from trl.chat_template_utils import parse_response


def multiply(a: int, b: int) -> int:
    """
    Multiplies two integers.

    Args:
        a: The first integer.
        b: The second integer.
    """
    return a * b


tokenizer = add_response_schema(AutoTokenizer.from_pretrained("zai-org/GLM-4.5-Air"))
prompt = tokenizer.apply_chat_template(
    [{"role": "user", "content": "What is 3*4?"}], tools=[multiply], add_generation_prompt=True
)
completion = tokenizer.encode(
    "<think></think>\n<tool_call>multiply\n<arg_key>a</arg_key>\n<arg_value>3</arg_value>\n"
    "<arg_key>b</arg_key>\n<arg_value>4</arg_value>\n</tool_call><|observation|>",
    add_special_tokens=False,
)
print(parse_response(tokenizer, completion, prefix=prompt, tools=[multiply]))
{'role': 'assistant', 'tool_calls': [{'type': 'function', 'function': {'name': 'multiply', 'arguments': {'a': 3, 'b': 4}}}], 'content': '<|observation|>'}

Expected content: ''. Ending the completion with <|user|> instead gives content: ''. Tested with TRL main and transformers 5.20.2.

Root cause

parse_response removes only tokenizer.eos_token (<|endoftext|>) from the parsed content. glm4moe_template consumes <|user|> as an end of turn but not <|observation|>, so the token falls through into content.

albertvillanova avatar Oct 29 '26 10:10 albertvillanova

Hi! I'd be interested in working on this issue as my first contribution to TRL.

The reproduction is clear, and I'd like to investigate a fix in parse_response along with a regression test ensuring that <|observation|> is not retained in the parsed assistant message content.

Would it be okay for me to work on a PR for this?

MarouaZoubir avatar Oct 29 '26 16:10 MarouaZoubir

Hi! I'd like to work on this issue as my first contribution to TRL. My proposed approach is to update the GLM-4-MoE response template to recognize <|observation|> as an end-of-turn marker, then add a regression test verifying that tool-call responses have an empty content field. I'll run the relevant response-parsing tests and check that the change doesn't affect other templates. Would you be able to assign this issue to me so I can work on the PR?

MarouaZoubir avatar Oct 29 '26 17:10 MarouaZoubir

Hi! I've been investigating this issue and have implemented a potential fix locally.

The root cause is that parse_response removes tokenizer.eos_token (<|endoftext|>) from the parsed content, while glm4moe_template recognizes <|user|> as a content boundary but does not handle <|observation|>. As a result, <|observation|> can remain in the content field of parsed GLM-4-MoE tool-call messages.

To address this, I updated the close_pattern in glm4moe_template from:

r"<\|user\|>\s*|$"

to:

r"<\|user\|>\s*|<\|observation\|>\s*|$"

This allows the parser to stop extracting content when it encounters <|observation|>, rather than retaining the marker as ordinary message text.

I also added a regression test in tests/test_chat_template_utils.py that checks whether a GLM-4-MoE response containing a multiply tool call with arguments a=3 and b=4, ending with <|observation|>, is parsed into the expected tool call with an empty content field.

The targeted parsing tests currently pass: 9 passed and 24 skipped.

Would it be possible to assign this issue to me, or confirm that I can proceed with submitting a PR? I'd appreciate your feedback on whether this approach aligns with the project's expectations. Thank you!

MarouaZoubir avatar Oct 29 '26 22:10 MarouaZoubir