teich 0.3.0__tar.gz → 0.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. {teich-0.3.0 → teich-0.3.2}/.github/workflows/test.yml +28 -4
  2. {teich-0.3.0 → teich-0.3.2}/PKG-INFO +4 -2
  3. {teich-0.3.0 → teich-0.3.2}/docker/codex-runtime.Dockerfile +2 -3
  4. teich-0.3.2/gemma-4-chat-template.jinja +361 -0
  5. teich-0.3.2/new_gemma_4_template.jinja +472 -0
  6. {teich-0.3.0 → teich-0.3.2}/pyproject.toml +22 -5
  7. teich-0.3.2/qwen3.5-chat-template.jinja +158 -0
  8. teich-0.3.2/qwen3.6-chat-template.jinja +158 -0
  9. teich-0.3.2/qwen3.6_chat_template.jinja +158 -0
  10. {teich-0.3.0 → teich-0.3.2}/src/teich/__init__.py +1 -1
  11. {teich-0.3.0 → teich-0.3.2}/src/teich/anonymize.py +22 -12
  12. {teich-0.3.0 → teich-0.3.2}/src/teich/cli.py +27 -21
  13. {teich-0.3.0 → teich-0.3.2}/src/teich/config.py +9 -13
  14. {teich-0.3.0 → teich-0.3.2}/src/teich/converter.py +58 -50
  15. {teich-0.3.0 → teich-0.3.2}/src/teich/extract.py +213 -86
  16. {teich-0.3.0 → teich-0.3.2}/src/teich/formatter.py +195 -91
  17. {teich-0.3.0 → teich-0.3.2}/src/teich/loader.py +10 -2
  18. {teich-0.3.0 → teich-0.3.2}/src/teich/prepare.py +13 -3
  19. {teich-0.3.0 → teich-0.3.2}/src/teich/runner.py +199 -103
  20. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/dataset_preview.py +43 -15
  21. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/events.py +9 -9
  22. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/interactive.py +24 -18
  23. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/project.py +4 -2
  24. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/server.py +4 -2
  25. {teich-0.3.0 → teich-0.3.2}/src/teich/tool_schema.py +88 -13
  26. {teich-0.3.0 → teich-0.3.2}/tests/test_config.py +15 -3
  27. {teich-0.3.0 → teich-0.3.2}/tests/test_extract_anonymize_cli.py +150 -15
  28. {teich-0.3.0 → teich-0.3.2}/tests/test_formatter.py +98 -0
  29. {teich-0.3.0 → teich-0.3.2}/tests/test_integration.py +43 -32
  30. {teich-0.3.0 → teich-0.3.2}/tests/test_loader.py +59 -3
  31. {teich-0.3.0 → teich-0.3.2}/tests/test_prepare.py +16 -0
  32. {teich-0.3.0 → teich-0.3.2}/tests/test_runner.py +103 -8
  33. {teich-0.3.0 → teich-0.3.2}/tests/test_studio.py +86 -3
  34. {teich-0.3.0 → teich-0.3.2}/tests/test_tool_schema.py +41 -2
  35. {teich-0.3.0 → teich-0.3.2}/uv.lock +2735 -2282
  36. {teich-0.3.0 → teich-0.3.2}/.gitignore +0 -0
  37. {teich-0.3.0 → teich-0.3.2}/DOCS.md +0 -0
  38. {teich-0.3.0 → teich-0.3.2}/LICENSE +0 -0
  39. {teich-0.3.0 → teich-0.3.2}/README.md +0 -0
  40. {teich-0.3.0 → teich-0.3.2}/config.example.yaml +0 -0
  41. {teich-0.3.0 → teich-0.3.2}/docs/cli.md +0 -0
  42. {teich-0.3.0 → teich-0.3.2}/docs/data-format.md +0 -0
  43. {teich-0.3.0 → teich-0.3.2}/docs/generation.md +0 -0
  44. {teich-0.3.0 → teich-0.3.2}/docs/index.md +0 -0
  45. {teich-0.3.0 → teich-0.3.2}/docs/pipeline.md +0 -0
  46. {teich-0.3.0 → teich-0.3.2}/docs/prepare-data.md +0 -0
  47. {teich-0.3.0 → teich-0.3.2}/docs/python-api.md +0 -0
  48. {teich-0.3.0 → teich-0.3.2}/docs/studio.md +0 -0
  49. {teich-0.3.0 → teich-0.3.2}/docs/training.md +0 -0
  50. {teich-0.3.0 → teich-0.3.2}/examples/config.claude-code-thinking.yaml +0 -0
  51. {teich-0.3.0 → teich-0.3.2}/examples/config.codex-reasoning.yaml +0 -0
  52. {teich-0.3.0 → teich-0.3.2}/examples/example_claude_code.jsonl +0 -0
  53. {teich-0.3.0 → teich-0.3.2}/examples/example_codex_session.jsonl +0 -0
  54. {teich-0.3.0 → teich-0.3.2}/examples/example_cursor_session.jsonl +0 -0
  55. {teich-0.3.0 → teich-0.3.2}/examples/example_droid_session.jsonl +0 -0
  56. {teich-0.3.0 → teich-0.3.2}/examples/example_hermes_session.jsonl +0 -0
  57. {teich-0.3.0 → teich-0.3.2}/examples/example_openclaw_session.jsonl +0 -0
  58. {teich-0.3.0 → teich-0.3.2}/examples/example_pi_session.jsonl +0 -0
  59. {teich-0.3.0 → teich-0.3.2}/examples/prompts.jsonl +0 -0
  60. {teich-0.3.0 → teich-0.3.2}/gemma-template.jinja +0 -0
  61. {teich-0.3.0 → teich-0.3.2}/gemma4_example.py +0 -0
  62. {teich-0.3.0 → teich-0.3.2}/src/teich/__main__.py +0 -0
  63. {teich-0.3.0 → teich-0.3.2}/src/teich/audit.py +0 -0
  64. {teich-0.3.0 → teich-0.3.2}/src/teich/codex_auth_broker.py +0 -0
  65. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/__init__.py +0 -0
  66. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/extraction.py +0 -0
  67. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/generation.py +0 -0
  68. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/static/app.js +0 -0
  69. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/static/index.html +0 -0
  70. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/static/style.css +0 -0
  71. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/static/teich.svg +0 -0
  72. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/static/vendor/xterm-addon-fit.js +0 -0
  73. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/static/vendor/xterm.css +0 -0
  74. {teich-0.3.0 → teich-0.3.2}/src/teich/studio/static/vendor/xterm.js +0 -0
  75. {teich-0.3.0 → teich-0.3.2}/src/teich/trace_readme.py +0 -0
  76. {teich-0.3.0 → teich-0.3.2}/teich_example.py +0 -0
  77. {teich-0.3.0 → teich-0.3.2}/tests/conftest.py +0 -0
  78. {teich-0.3.0 → teich-0.3.2}/tests/test_audit.py +0 -0
  79. {teich-0.3.0 → teich-0.3.2}/tests/test_cli.py +0 -0
  80. {teich-0.3.0 → teich-0.3.2}/tests/test_codex_auth_broker.py +0 -0
  81. {teich-0.3.0 → teich-0.3.2}/tests/test_codex_langfuse.py +0 -0
  82. {teich-0.3.0 → teich-0.3.2}/tests/test_converter.py +0 -0
  83. {teich-0.3.0 → teich-0.3.2}/tests/test_langfuse_agents.py +0 -0
  84. {teich-0.3.0 → teich-0.3.2}/tests/test_tokenizer_smoke.py +0 -0
  85. {teich-0.3.0 → teich-0.3.2}/tests/test_trace_readme.py +0 -0
@@ -9,13 +9,17 @@ on:
9
9
  jobs:
10
10
  unit-tests:
11
11
  runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ python-version: ['3.10', '3.11', '3.12', '3.13']
12
16
  steps:
13
17
  - uses: actions/checkout@v4
14
18
 
15
19
  - name: Set up Python
16
20
  uses: actions/setup-python@v5
17
21
  with:
18
- python-version: '3.12'
22
+ python-version: ${{ matrix.python-version }}
19
23
 
20
24
  - name: Install uv
21
25
  run: |
@@ -24,12 +28,32 @@ jobs:
24
28
 
25
29
  - name: Install dependencies
26
30
  run: |
31
+ uv lock --check
27
32
  uv sync --extra dev
28
33
 
29
- - name: Run unit tests
34
+ - name: Run static checks and unit tests
30
35
  run: |
36
+ uv run ruff check .
37
+ uv run mypy
31
38
  uv run pytest tests/ -v -m "not integration" --tb=short
32
39
 
40
+ package-build:
41
+ runs-on: ubuntu-latest
42
+ steps:
43
+ - uses: actions/checkout@v4
44
+
45
+ - name: Install uv
46
+ uses: astral-sh/setup-uv@v6
47
+
48
+ - name: Build wheel and source distribution
49
+ run: uv build
50
+
51
+ - name: Smoke-test built wheel in an isolated environment
52
+ run: |
53
+ uv venv --python 3.12 .package-smoke
54
+ uv pip install --python .package-smoke/bin/python dist/*.whl
55
+ .package-smoke/bin/teich --help
56
+
33
57
  docker-build:
34
58
  runs-on: ubuntu-latest
35
59
  steps:
@@ -50,7 +74,7 @@ jobs:
50
74
  runs-on: ubuntu-latest
51
75
  needs: [unit-tests, docker-build]
52
76
  if: github.event_name == 'push' && github.ref == 'refs/heads/main'
53
- # Only run on main to avoid burning API credits on every PR
77
+ # Only run the slower Docker/runtime smokes on main.
54
78
  environment: integration-tests
55
79
  steps:
56
80
  - uses: actions/checkout@v4
@@ -71,6 +95,6 @@ jobs:
71
95
 
72
96
  - name: Run integration tests
73
97
  env:
74
- OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
98
+ TEICH_RUN_DOCKER_RUNTIME_SMOKE: "1"
75
99
  run: |
76
100
  uv run pytest tests/test_integration.py -v --tb=short
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: teich
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Turn coding agent traces into auditable supervised fine-tuning data
5
5
  License: Apache-2.0
6
6
  License-File: LICENSE
@@ -22,10 +22,12 @@ Requires-Dist: typer>=0.12
22
22
  Requires-Dist: uvicorn>=0.29
23
23
  Requires-Dist: websockets>=12
24
24
  Provides-Extra: dev
25
- Requires-Dist: httpx>=0.27; extra == 'dev'
25
+ Requires-Dist: httpx2>=2.9; extra == 'dev'
26
26
  Requires-Dist: jinja2>=3.1; extra == 'dev'
27
+ Requires-Dist: mypy>=1.10; extra == 'dev'
27
28
  Requires-Dist: pytest>=8.0; extra == 'dev'
28
29
  Requires-Dist: ruff>=0.4; extra == 'dev'
30
+ Requires-Dist: transformers>=5.0; extra == 'dev'
29
31
  Provides-Extra: studio
30
32
  Requires-Dist: fastapi>=0.110; extra == 'studio'
31
33
  Requires-Dist: uvicorn>=0.29; extra == 'studio'
@@ -1,9 +1,8 @@
1
1
  # syntax=docker/dockerfile:1
2
2
  FROM node:26.4.0-slim
3
3
 
4
- # Install system dependencies with cache mount and minimal packages
5
- # Removed: build-essential (only needed for compiling, not runtime)
6
- # Added: --no-install-recommends to skip extra packages
4
+ # Install runtime dependencies plus the compiler headers needed when Hermes or
5
+ # a user-installed Python package has no prebuilt wheel.
7
6
  RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
8
7
  --mount=type=cache,target=/var/lib/apt,sharing=locked \
9
8
  apt-get update && apt-get install -y --no-install-recommends \
@@ -0,0 +1,361 @@
1
+ {%- macro format_parameters(properties, required, filter_keys=false) -%}
2
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
3
+ {%- set ns = namespace(found_first=false) -%}
4
+ {%- for key, value in properties | dictsort -%}
5
+ {%- set add_comma = false -%}
6
+ {%- if not filter_keys or key not in standard_keys -%}
7
+ {%- if ns.found_first %},{% endif -%}
8
+ {%- set ns.found_first = true -%}
9
+ {{ key }}:{
10
+ {%- if value['description'] -%}
11
+ description:<|"|>{{ value['description'] }}<|"|>
12
+ {%- set add_comma = true -%}
13
+ {%- endif -%}
14
+ {%- if value['type'] | upper == 'STRING' -%}
15
+ {%- if value['enum'] -%}
16
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
17
+ enum:{{ format_argument(value['enum']) }}
18
+ {%- endif -%}
19
+ {%- elif value['type'] | upper == 'ARRAY' -%}
20
+ {%- if value['items'] is mapping and value['items'] -%}
21
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
22
+ items:{
23
+ {%- set ns_items = namespace(found_first=false) -%}
24
+ {%- for item_key, item_value in value['items'] | dictsort -%}
25
+ {%- if item_value is not none -%}
26
+ {%- if ns_items.found_first %},{% endif -%}
27
+ {%- set ns_items.found_first = true -%}
28
+ {%- if item_key == 'properties' -%}
29
+ properties:{
30
+ {%- if item_value is mapping -%}
31
+ {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
32
+ {%- endif -%}
33
+ }
34
+ {%- elif item_key == 'required' -%}
35
+ required:[
36
+ {%- for req_item in item_value -%}
37
+ <|"|>{{- req_item -}}<|"|>
38
+ {%- if not loop.last %},{% endif -%}
39
+ {%- endfor -%}
40
+ ]
41
+ {%- elif item_key == 'type' -%}
42
+ {%- if item_value is string -%}
43
+ type:{{ format_argument(item_value | upper) }}
44
+ {%- else -%}
45
+ type:{{ format_argument(item_value | map('upper') | list) }}
46
+ {%- endif -%}
47
+ {%- else -%}
48
+ {{ item_key }}:{{ format_argument(item_value) }}
49
+ {%- endif -%}
50
+ {%- endif -%}
51
+ {%- endfor -%}
52
+ }
53
+ {%- endif -%}
54
+ {%- endif -%}
55
+ {%- if value['nullable'] %}
56
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
57
+ nullable:true
58
+ {%- endif -%}
59
+ {%- if value['type'] | upper == 'OBJECT' -%}
60
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
61
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
62
+ properties:{
63
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
64
+ }
65
+ {%- elif value is mapping -%}
66
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
67
+ properties:{
68
+ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
69
+ }
70
+ {%- endif -%}
71
+ {%- if value['required'] -%}
72
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
73
+ required:[
74
+ {%- for item in value['required'] | default([]) -%}
75
+ <|"|>{{- item -}}<|"|>
76
+ {%- if not loop.last %},{% endif -%}
77
+ {%- endfor -%}
78
+ ]
79
+ {%- endif -%}
80
+ {%- endif -%}
81
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
82
+ type:<|"|>{{ value['type'] | upper }}<|"|>}
83
+ {%- endif -%}
84
+ {%- endfor -%}
85
+ {%- endmacro -%}
86
+ {%- macro format_function_declaration(tool_data) -%}
87
+ declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
88
+ {%- set params = tool_data['function']['parameters'] -%}
89
+ {%- if params -%}
90
+ ,parameters:{
91
+ {%- if params['properties'] -%}
92
+ properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
93
+ {%- endif -%}
94
+ {%- if params['required'] -%}
95
+ required:[
96
+ {%- for item in params['required'] -%}
97
+ <|"|>{{- item -}}<|"|>
98
+ {{- ',' if not loop.last -}}
99
+ {%- endfor -%}
100
+ ],
101
+ {%- endif -%}
102
+ {%- if params['type'] -%}
103
+ type:<|"|>{{- params['type'] | upper -}}<|"|>}
104
+ {%- endif -%}
105
+ {%- endif -%}
106
+ {%- if 'response' in tool_data['function'] -%}
107
+ {%- set response_declaration = tool_data['function']['response'] -%}
108
+ ,response:{
109
+ {%- if response_declaration['description'] -%}
110
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
111
+ {%- endif -%}
112
+ {%- if response_declaration['type'] | upper == 'OBJECT' -%}
113
+ type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
114
+ {%- endif -%}
115
+ {%- endif -%}
116
+ }
117
+ {%- endmacro -%}
118
+ {%- macro format_argument(argument, escape_keys=True) -%}
119
+ {%- if argument is string -%}
120
+ {{- '<|"|>' + argument + '<|"|>' -}}
121
+ {%- elif argument is boolean -%}
122
+ {{- 'true' if argument else 'false' -}}
123
+ {%- elif argument is mapping -%}
124
+ {{- '{' -}}
125
+ {%- set ns = namespace(found_first=false) -%}
126
+ {%- for key, value in argument | dictsort -%}
127
+ {%- if ns.found_first %},{% endif -%}
128
+ {%- set ns.found_first = true -%}
129
+ {%- if escape_keys -%}
130
+ {{- '<|"|>' + key + '<|"|>' -}}
131
+ {%- else -%}
132
+ {{- key -}}
133
+ {%- endif -%}
134
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
135
+ {%- endfor -%}
136
+ {{- '}' -}}
137
+ {%- elif argument is sequence -%}
138
+ {{- '[' -}}
139
+ {%- for item in argument -%}
140
+ {{- format_argument(item, escape_keys=escape_keys) -}}
141
+ {%- if not loop.last %},{% endif -%}
142
+ {%- endfor -%}
143
+ {{- ']' -}}
144
+ {%- else -%}
145
+ {{- argument -}}
146
+ {%- endif -%}
147
+ {%- endmacro -%}
148
+ {%- macro format_json_argument(argument) -%}
149
+ {%- if argument is string -%}
150
+ {{- '"' + argument | replace('\\', '\\\\') | replace('"', '\\"') | replace('\n', '\\n') + '"' -}}
151
+ {%- elif argument is boolean -%}
152
+ {{- 'true' if argument else 'false' -}}
153
+ {%- elif argument is mapping -%}
154
+ {{- '{' -}}
155
+ {%- set ns = namespace(found_first=false) -%}
156
+ {%- for key, value in argument | dictsort -%}
157
+ {%- if ns.found_first %},{% endif -%}
158
+ {%- set ns.found_first = true -%}
159
+ {{- '"' + key | replace('\\', '\\\\') | replace('"', '\\"') + '": ' -}}
160
+ {{- format_json_argument(value) -}}
161
+ {%- endfor -%}
162
+ {{- '}' -}}
163
+ {%- elif argument is sequence -%}
164
+ {{- '[' -}}
165
+ {%- for item in argument -%}
166
+ {{- format_json_argument(item) -}}
167
+ {%- if not loop.last %},{% endif -%}
168
+ {%- endfor -%}
169
+ {{- ']' -}}
170
+ {%- elif argument is none -%}
171
+ {{- 'null' -}}
172
+ {%- else -%}
173
+ {{- argument -}}
174
+ {%- endif -%}
175
+ {%- endmacro -%}
176
+ {%- macro strip_thinking(text) -%}
177
+ {%- set ns = namespace(result='') -%}
178
+ {%- for part in text.split('<channel|>') -%}
179
+ {%- if '<|channel>' in part -%}
180
+ {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
181
+ {%- else -%}
182
+ {%- set ns.result = ns.result + part -%}
183
+ {%- endif -%}
184
+ {%- endfor -%}
185
+ {{- ns.result | trim -}}
186
+ {%- endmacro -%}
187
+
188
+ {%- macro format_tool_response_block(tool_name, response) -%}
189
+ {{- '<|tool_response>' -}}
190
+ {%- if response is mapping -%}
191
+ {{- 'response:' + tool_name + '{' -}}
192
+ {%- for key, value in response | dictsort -%}
193
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
194
+ {%- if not loop.last %},{% endif -%}
195
+ {%- endfor -%}
196
+ {{- '}' -}}
197
+ {%- else -%}
198
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
199
+ {%- endif -%}
200
+ {{- '<tool_response|>' -}}
201
+ {%- endmacro -%}
202
+
203
+ {%- set ns = namespace(prev_message_type=None) -%}
204
+ {%- set loop_messages = messages -%}
205
+ {{- bos_token -}}
206
+ {#- Handle System/Tool Definitions Block -#}
207
+ {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
208
+ {{- '<|turn>system\n' -}}
209
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
210
+ {%- if enable_thinking is defined and enable_thinking -%}
211
+ {{- '<|think|>\n' -}}
212
+ {%- set ns.prev_message_type = 'think' -%}
213
+ {%- endif -%}
214
+ {%- if messages[0]['role'] in ['system', 'developer'] -%}
215
+ {%- if messages[0]['content'] is string -%}
216
+ {{- messages[0]['content'] | trim -}}
217
+ {%- elif messages[0]['content'] is sequence -%}
218
+ {%- for item in messages[0]['content'] -%}
219
+ {{- item['text'] | trim + ' '-}}
220
+ {%- endfor -%}
221
+ {%- endif -%}
222
+ {%- set loop_messages = messages[1:] -%}
223
+ {%- endif -%}
224
+ {%- if tools -%}
225
+ {%- for tool in tools %}
226
+ {{- '<|tool>' -}}
227
+ {{- format_function_declaration(tool) | trim -}}
228
+ {{- '<tool|>' -}}
229
+ {%- endfor %}
230
+ {%- set ns.prev_message_type = 'tool' -%}
231
+ {%- endif -%}
232
+ {{- '<turn|>\n' -}}
233
+ {%- endif %}
234
+
235
+ {#- Pre-scan: find last user message index for reasoning guard -#}
236
+ {%- set ns_turn = namespace(last_user_idx=-1) -%}
237
+ {%- for i in range(loop_messages | length) -%}
238
+ {%- if loop_messages[i]['role'] == 'user' -%}
239
+ {%- set ns_turn.last_user_idx = i -%}
240
+ {%- endif -%}
241
+ {%- endfor -%}
242
+
243
+ {#- Loop through messages -#}
244
+ {%- for message in loop_messages -%}
245
+ {%- if message['role'] == 'tool' -%}
246
+ {{- '<|turn>user\n' -}}
247
+ {%- set tool_body = message.get('content') -%}
248
+ {%- if tool_body is string -%}
249
+ {{- format_tool_response_block(message.get('name') | default('unknown'), tool_body) -}}
250
+ {%- elif tool_body is sequence and tool_body is not string -%}
251
+ {%- set ns_txt = namespace(s='') -%}
252
+ {%- for part in tool_body -%}
253
+ {%- if part.get('type') == 'text' -%}
254
+ {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
255
+ {%- endif -%}
256
+ {%- endfor -%}
257
+ {{- format_tool_response_block(message.get('name') | default('unknown'), ns_txt.s) -}}
258
+ {%- else -%}
259
+ {{- format_tool_response_block(message.get('name') | default('unknown'), tool_body) -}}
260
+ {%- endif -%}
261
+ {{- '<turn|>\n' -}}
262
+ {%- set ns.prev_message_type = 'tool_response' -%}
263
+ {%- else -%}
264
+ {%- set ns.prev_message_type = None -%}
265
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
266
+ {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}
267
+ {%- set prev_nt = namespace(role=None, found=false) -%}
268
+ {%- if loop.index0 > 0 -%}
269
+ {%- for j in range(loop.index0 - 1, -1, -1) -%}
270
+ {%- if not prev_nt.found -%}
271
+ {%- if loop_messages[j]['role'] != 'tool' -%}
272
+ {%- set prev_nt.role = loop_messages[j]['role'] -%}
273
+ {%- set prev_nt.found = true -%}
274
+ {%- endif -%}
275
+ {%- endif -%}
276
+ {%- endfor -%}
277
+ {%- endif -%}
278
+ {%- set prev_role = loop_messages[loop.index0 - 1]['role'] if loop.index0 > 0 else none -%}
279
+ {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant' and prev_role != 'tool') -%}
280
+ {%- if not continue_same_model_turn -%}
281
+ {{- '<|turn>' + role + '\n' }}
282
+ {%- endif -%}
283
+
284
+ {#- Render reasoning/reasoning_content as thinking channel -#}
285
+ {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
286
+ {%- if thinking_text and role == 'model' and (enable_thinking | default(false)) -%}
287
+ {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
288
+ {%- endif -%}
289
+
290
+ {%- if message['tool_calls'] -%}
291
+ {%- for tool_call in message['tool_calls'] -%}
292
+ {%- set function = tool_call['function'] -%}
293
+ {{- '<|tool_call>call:' + function['name'] -}}
294
+ {%- if function['arguments'] is mapping -%}
295
+ {{- format_json_argument(function['arguments']) -}}
296
+ {%- elif function['arguments'] is string -%}
297
+ {{- function['arguments'] -}}
298
+ {%- else -%}
299
+ {{- '{}' -}}
300
+ {%- endif -%}
301
+ {{- '<tool_call|>' -}}
302
+ {%- endfor -%}
303
+ {%- set ns.prev_message_type = 'tool_call' -%}
304
+ {%- endif -%}
305
+
306
+ {%- set ns_tr_out = namespace(flag=false) -%}
307
+ {%- if message.get('tool_responses') -%}
308
+ {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
309
+ {%- for tool_response in message['tool_responses'] -%}
310
+ {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
311
+ {%- set ns_tr_out.flag = true -%}
312
+ {%- set ns.prev_message_type = 'tool_response' -%}
313
+ {%- endfor -%}
314
+ {%- endif -%}
315
+
316
+ {%- set captured_content -%}
317
+ {%- if message['content'] is string -%}
318
+ {%- if role == 'model' -%}
319
+ {{- strip_thinking(message['content']) -}}
320
+ {%- else -%}
321
+ {{- message['content'] | trim -}}
322
+ {%- endif -%}
323
+ {%- elif message['content'] is sequence -%}
324
+ {%- for item in message['content'] -%}
325
+ {%- if item['type'] == 'text' -%}
326
+ {%- if role == 'model' -%}
327
+ {{- strip_thinking(item['text']) -}}
328
+ {%- else -%}
329
+ {{- item['text'] | trim -}}
330
+ {%- endif -%}
331
+ {%- elif item['type'] == 'image' -%}
332
+ {{- '<|image|>' -}}
333
+ {%- set ns.prev_message_type = 'image' -%}
334
+ {%- elif item['type'] == 'audio' -%}
335
+ {{- '<|audio|>' -}}
336
+ {%- set ns.prev_message_type = 'audio' -%}
337
+ {%- elif item['type'] == 'video' -%}
338
+ {{- '<|video|>' -}}
339
+ {%- set ns.prev_message_type = 'video' -%}
340
+ {%- endif -%}
341
+ {%- endfor -%}
342
+ {%- endif -%}
343
+ {%- endset -%}
344
+
345
+ {{- captured_content -}}
346
+ {%- set has_content = captured_content | trim | length > 0 -%}
347
+
348
+ {%- if not (ns_tr_out.flag and not has_content) -%}
349
+ {{- '<turn|>\n' -}}
350
+ {%- endif -%}
351
+ {%- endif -%}
352
+ {%- endfor -%}
353
+
354
+ {%- if add_generation_prompt -%}
355
+ {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
356
+ {{- '<|turn>model\n' -}}
357
+ {%- if not enable_thinking | default(false) -%}
358
+ {{- '<|channel>thought\n<channel|>' -}}
359
+ {%- endif -%}
360
+ {%- endif -%}
361
+ {%- endif -%}