summarylogtreecommitdiffstats
path: root/0003-templates-fix-Gemma-4-template-crash-on-array-typed-.patch
blob: 6756e7d6a30fd29a2b6ee3cf01bcc1b2d9813de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
From a93c0ff1ba6732de81b3158bc85641451f8f8646 Mon Sep 17 00:00:00 2001
From: Ewout van Mansom <ewout@vanmansom.name>
Date: Wed, 8 Apr 2026 14:33:54 +0200
Subject: [PATCH 3/6] templates : fix Gemma 4 template crash on array-typed
 schema fields

The Gemma 4 Jinja template calls value['type'] | upper which crashes
when type is an array like ["string", "null"] instead of a plain
string. Add guards to check for type existence, distinguish arrays
from strings, filter out null, and fall back to "string" for
degenerate cases.
---
 .../google-gemma-4-31B-it-interleaved.jinja   | 33 +++++++++++++++----
 1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/models/templates/google-gemma-4-31B-it-interleaved.jinja b/models/templates/google-gemma-4-31B-it-interleaved.jinja
index 85791c4fe..9df8d8dd2 100644
--- a/models/templates/google-gemma-4-31B-it-interleaved.jinja
+++ b/models/templates/google-gemma-4-31B-it-interleaved.jinja
@@ -6,6 +6,16 @@
         {%- if key not in standard_keys -%}
             {%- if ns.found_first %},{% endif -%}
             {%- set ns.found_first = true -%}
+            {#- Safe type extraction: handle missing type, array type ["string", "null"], or plain string -#}
+            {%- if 'type' in value -%}
+                {%- if value['type'] is iterable and value['type'] is not string -%}
+                    {%- set value_type = value['type'] | reject('equalto', 'null') | first | default('string') -%}
+                {%- else -%}
+                    {%- set value_type = value['type'] -%}
+                {%- endif -%}
+            {%- else -%}
+                {%- set value_type = 'string' -%}
+            {%- endif -%}
             {{ key }}:{
             {%- if value['description'] -%}
                 description:<|"|>{{ value['description'] }}<|"|>
@@ -15,12 +25,12 @@
                 {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                 nullable:true
             {%- endif -%}
-            {%- if value['type'] | upper == 'STRING' -%}
+            {%- if value_type | upper == 'STRING' -%}
                 {%- if value['enum'] -%}
                     {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                     enum:{{ format_argument(value['enum']) }}
                 {%- endif -%}
-            {%- elif value['type'] | upper == 'OBJECT' -%}
+            {%- elif value_type | upper == 'OBJECT' -%}
                 ,properties:{
                 {%- if value['properties'] is defined and value['properties'] is mapping -%}
                     {{- format_parameters(value['properties'], value['required'] | default([])) -}}
@@ -36,7 +46,7 @@
                     {%- endfor -%}
                     ]
                 {%- endif -%}
-            {%- elif value['type'] | upper == 'ARRAY' -%}
+            {%- elif value_type | upper == 'ARRAY' -%}
                 {%- if value['items'] is mapping and value['items'] -%}
                     ,items:{
                     {%- set ns_items = namespace(found_first=false) -%}
@@ -72,7 +82,7 @@
                 {%- endif -%}
             {%- endif -%}
             {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
-            type:<|"|>{{ value['type'] | upper }}<|"|>}
+            type:<|"|>{{ value_type | upper }}<|"|>}
         {%- endif -%}
     {%- endfor -%}
 {%- endmacro -%}
@@ -93,7 +103,11 @@
             ],
         {%- endif -%}
         {%- if params['type'] -%}
-            type:<|"|>{{- params['type'] | upper -}}<|"|>}
+            {%- if params['type'] is iterable and params['type'] is not string -%}
+                type:<|"|>{{- params['type'] | reject('equalto', 'null') | first | default('object') | upper -}}<|"|>}
+            {%- else -%}
+                type:<|"|>{{- params['type'] | upper -}}<|"|>}
+            {%- endif -%}
         {%- endif -%}
     {%- endif -%}
     {%- if 'response' in tool_data['function'] -%}
@@ -102,8 +116,13 @@
         {%- if response_declaration['description'] -%}
             description:<|"|>{{- response_declaration['description'] -}}<|"|>,
         {%- endif -%}
-        {%- if response_declaration['type'] | upper == 'OBJECT' -%}
-            type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
+        {%- if response_declaration['type'] is iterable and response_declaration['type'] is not string -%}
+            {%- set resp_type = response_declaration['type'] | reject('equalto', 'null') | first | default('object') -%}
+        {%- else -%}
+            {%- set resp_type = response_declaration['type'] | default('object') -%}
+        {%- endif -%}
+        {%- if resp_type | upper == 'OBJECT' -%}
+            type:<|"|>{{- resp_type | upper -}}<|"|>}
         {%- endif -%}
     {%- endif -%}
     }
-- 
2.53.0