fix: chat template โ€” null handling, reasoning preservation, turn-tag balance, input validation

#118
by lucianommartins - opened

Summary

Improves Gemma4 chat template:

Bug fixes

  • None values now render as null instead of Python's None
  • String-typed tool_calls[].function.arguments now raises a clear error instead of silently producing malformed DSL
  • Prior-turn reasoning/thinking is preserved across multi-turn tool-call chains (preserve_thinking flag, default=true)
  • Consecutive assistant messages now produce balanced <|turn>model/<turn|> tags via forward-scan continuation detection

Improvements

  • enable_thinking normalized once with | default(false), eliminating repetitive is defined and checks
  • image_url and input_audio content types now map to <|image|> and <|audio|> (OpenAI compatibility)
  • Empty messages=[] handled gracefully instead of crashing
  • Unmatched tool_call_id in tool responses falls back to 'unknown' instead of crashing
  • Consistent .get() access prevents StrictUndefined errors for optional message keys
  • O(1) backward scan for model-turn continuation (was O(n) per message)

Thanks for the commit, I hope this template changes fix premature ending problems. First in the morning to apply these new changes!
I am convincing teams that we can fix the issues, we should not give up and transfer to Qwen models lol. Cannot let gemma4 go.

I've been sharing this publicly which it seems you fix a few things here as well: https://gist.github.com/jscott3201/ad69c4ffbd79f18b11a0f6a94c94fadf

I can move this to a repo with MIT/Apache2 if you want to pull anything off this template for the official template.

I've been sharing this publicly which it seems you fix a few things here as well: https://gist.github.com/jscott3201/ad69c4ffbd79f18b11a0f6a94c94fadf

I can move this to a repo with MIT/Apache2 if you want to pull anything off this template for the official template.

I have been running your template but can you now comment on which is better - Google's new one or yours and will you be coming up with a new one incorporating google's changes?

I would have to diff them but they designed the LLM so I'll lean towards theirs being correct! Their 4 noted issues align with my callouts in the header.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment