[PR #14695] template: add JSON String() methods for tool struct types #61488

Open
opened 2026-04-29 16:35:08 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14695
Author: @tysoncung
Created: 3/7/2026
Status: 🔄 Open

Base: mainHead: fix/template-function-json-serialization


📝 Commits (1)

  • c470357 template: add JSON String() methods for tool struct types

📊 Changes

2 files changed (+153 additions, -0 deletions)

View changed files

📝 template/template.go (+15 -0)
📝 template/template_test.go (+138 -0)

📄 Description

Summary

Adds String() methods to templateTool, templateToolFunction, and templateToolFunctionParameters so they serialize as valid JSON when referenced directly in Go templates.

Problem

When a model template uses {{ .Function }} to render a tool's function object (as Qwen3's template does), the output is Go's default struct format:

{get_weather Get the current weather for a city {object [city] {"city":{"type":"string",...}}}}

Instead of valid JSON:

{"name":"get_weather","description":"Get the current weather for a city","parameters":{"type":"object","required":["city"],"properties":{"city":{"type":"string",...}}}}

This breaks tool calling for any model whose template renders tool structs directly rather than accessing individual fields.

Fix

The existing map types (templateArgs, templateProperties) and slice type (templateTools) already implement String() with JSON marshaling. This PR extends the same pattern to the three struct types that were missing it:

  • templateTool — the full tool object ({{ . }} in a tools range)
  • templateToolFunction — the function sub-object ({{ .Function }})
  • templateToolFunctionParameters — the parameters sub-object ({{ .Function.Parameters }})

Tests

Added three new test cases:

  • TestTemplateToolFunctionJSON — verifies {{ .Function }} outputs valid JSON
  • TestTemplateToolJSON — verifies {{ . }} on a tool outputs valid JSON
  • TestTemplateToolParametersJSON — verifies {{ .Function.Parameters }} outputs valid JSON

All existing tests continue to pass.

Fixes #14601


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/14695 **Author:** [@tysoncung](https://github.com/tysoncung) **Created:** 3/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/template-function-json-serialization` --- ### 📝 Commits (1) - [`c470357`](https://github.com/ollama/ollama/commit/c470357bcb149cb274c8d3ee7b95a65df4b943fa) template: add JSON String() methods for tool struct types ### 📊 Changes **2 files changed** (+153 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `template/template.go` (+15 -0) 📝 `template/template_test.go` (+138 -0) </details> ### 📄 Description ## Summary Adds `String()` methods to `templateTool`, `templateToolFunction`, and `templateToolFunctionParameters` so they serialize as valid JSON when referenced directly in Go templates. ## Problem When a model template uses `{{ .Function }}` to render a tool's function object (as Qwen3's template does), the output is Go's default struct format: ``` {get_weather Get the current weather for a city {object [city] {"city":{"type":"string",...}}}} ``` Instead of valid JSON: ```json {"name":"get_weather","description":"Get the current weather for a city","parameters":{"type":"object","required":["city"],"properties":{"city":{"type":"string",...}}}} ``` This breaks tool calling for any model whose template renders tool structs directly rather than accessing individual fields. ## Fix The existing map types (`templateArgs`, `templateProperties`) and slice type (`templateTools`) already implement `String()` with JSON marshaling. This PR extends the same pattern to the three struct types that were missing it: - `templateTool` — the full tool object (`{{ . }}` in a tools range) - `templateToolFunction` — the function sub-object (`{{ .Function }}`) - `templateToolFunctionParameters` — the parameters sub-object (`{{ .Function.Parameters }}`) ## Tests Added three new test cases: - `TestTemplateToolFunctionJSON` — verifies `{{ .Function }}` outputs valid JSON - `TestTemplateToolJSON` — verifies `{{ . }}` on a tool outputs valid JSON - `TestTemplateToolParametersJSON` — verifies `{{ .Function.Parameters }}` outputs valid JSON All existing tests continue to pass. Fixes #14601 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-29 16:35:08 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#61488