Skip to content

jinja : correct default size for string slices#19913

Merged
ngxson merged 1 commit intoggml-org:masterfrom
EZForever:jinja-string-slice-size
Feb 26, 2026
Merged

jinja : correct default size for string slices#19913
ngxson merged 1 commit intoggml-org:masterfrom
EZForever:jinja-string-slice-size

Conversation

@EZForever
Copy link
Copy Markdown
Contributor

Make sure to read the contributing guidelines before submitting a PR

As of b8157, when trying to use string slices in a chat template, and the slice does not specify end index (e.g. content[1 : ]), no output will be emitted since the default end index is calculated only for arrays, and remains 0 for strings. This PR adds handling for strings, and should be complete for currently supported data types.

test-chat-template details

test.jinja:

{{- messages[0].content[:] }}

test.json:

{
	"messages": [
		{ "role": "user", "content": "123456" }
	]
}

Before (b8157):

C:\llama.cpp-b8157\build\bin\Debug>test-chat-template.exe --json "C:\test.json" "C:\test.jinja"
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:767 : Member expression on object type Array, property type Integer
jinja-runtime:787 : Accessing Array index 0
jinja-runtime:753 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:767 : Member expression on object type Object, property type String
jinja-runtime:782 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:736 : Member expression is a slice: start 0, stop 0, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value messages[0].content, type: String (used), ops: slice
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:767 : Member expression on object type Array, property type Integer
jinja-runtime:787 : Accessing Array index 0
jinja-runtime:753 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:767 : Member expression on object type Object, property type String
jinja-runtime:782 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:736 : Member expression is a slice: start 0, stop 0, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value messages[0].content, type: String (used), ops: slice
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:767 : Member expression on object type Array, property type Integer
jinja-runtime:787 : Accessing Array index 0
jinja-runtime:753 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:767 : Member expression on object type Object, property type String
jinja-runtime:782 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:736 : Member expression is a slice: start 0, stop 0, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value tools[0].function.name, type: String , ops:
jinja-caps:61  : Value messages[1].tool_calls, type: Array , ops:
jinja-caps:61  : Value messages[1].tool_calls[1].function, type: Object , ops:
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:767 : Member expression on object type Array, property type Integer
jinja-runtime:787 : Accessing Array index 0
jinja-runtime:753 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:767 : Member expression on object type Object, property type String
jinja-runtime:782 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:736 : Member expression is a slice: start 0, stop 0, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value messages[1].reasoning_content, type: String , ops:
jinja-caps:280 : Caps(
  supports_parallel_tool_calls=false
  supports_preserve_reasoning=false
  supports_string_content=true
  supports_system_role=true
  supports_tool_calls=false
  supports_tools=false
  supports_typed_content=false
)

jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:767 : Member expression on object type Array, property type Integer
jinja-runtime:787 : Accessing Array index 0
jinja-runtime:753 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:767 : Member expression on object type Object, property type String
jinja-runtime:782 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:736 : Member expression is a slice: start 0, stop 0, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'

=== OUTPUT ===


C:\llama.cpp-b8157\build\bin\Debug>

After (this PR):

C:\llama.cpp-b8157\build\bin\Debug>test-chat-template.exe --json "C:\test.json" "C:\test.jinja"
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:769 : Member expression on object type Array, property type Integer
jinja-runtime:789 : Accessing Array index 0
jinja-runtime:755 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:769 : Member expression on object type Object, property type String
jinja-runtime:784 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:738 : Member expression is a slice: start 0, stop 7, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value messages[0].content, type: String (used), ops: slice
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:769 : Member expression on object type Array, property type Integer
jinja-runtime:789 : Accessing Array index 0
jinja-runtime:755 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:769 : Member expression on object type Object, property type String
jinja-runtime:784 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:738 : Member expression is a slice: start 0, stop 14, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value messages[0].content, type: String (used), ops: slice
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:769 : Member expression on object type Array, property type Integer
jinja-runtime:789 : Accessing Array index 0
jinja-runtime:755 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:769 : Member expression on object type Object, property type String
jinja-runtime:784 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:738 : Member expression is a slice: start 0, stop 12, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value tools[0].function.name, type: String , ops:
jinja-caps:61  : Value messages[1].tool_calls, type: Array , ops:
jinja-caps:61  : Value messages[1].tool_calls[1].function, type: Object , ops:
jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'tools'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:769 : Member expression on object type Array, property type Integer
jinja-runtime:789 : Accessing Array index 0
jinja-runtime:755 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:769 : Member expression on object type Object, property type String
jinja-runtime:784 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:738 : Member expression is a slice: start 0, stop 12, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'
jinja-caps:61  : Value messages[1].reasoning_content, type: String , ops:
jinja-caps:280 : Caps(
  supports_parallel_tool_calls=false
  supports_preserve_reasoning=false
  supports_string_content=true
  supports_system_role=true
  supports_tool_calls=false
  supports_tools=false
  supports_typed_content=false
)

jinja-value:1268 : global_from_json: setting key 'messages'
jinja-value:1268 : global_from_json: setting key 'bos_token'
jinja-value:1268 : global_from_json: setting key 'eos_token'
jinja-value:1268 : global_from_json: setting key 'add_generation_prompt'
jinja-runtime:90  : Identifier 'messages' found, type = Array
jinja-runtime:719 : Member expression, computing property type IntegerLiteral
jinja-runtime:769 : Member expression on object type Array, property type Integer
jinja-runtime:789 : Accessing Array index 0
jinja-runtime:755 : Member expression, object type Object, static property 'content'
jinja-runtime:278 : Trying built-in function 'content' for type Object
jinja-runtime:769 : Member expression on object type Object, property type String
jinja-runtime:784 : Accessed property 'content' value, got type: String
jinja-runtime:719 : Member expression, computing property type SliceExpression
jinja-runtime:738 : Member expression is a slice: start 0, stop 6, step 1
jinja-runtime:278 : Trying built-in function 'slice' for type String
jinja-runtime:286 : Binding built-in 'slice'

=== OUTPUT ===
123456

C:\llama.cpp-b8157\build\bin\Debug>

@github-actions github-actions Bot added the jinja parser Issues related to the jinja parser label Feb 26, 2026
Copy link
Copy Markdown
Member

@CISC CISC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks!

@ngxson ngxson merged commit 9b62913 into ggml-org:master Feb 26, 2026
78 checks passed
bartowski1182 pushed a commit to bartowski1182/llama.cpp that referenced this pull request Mar 2, 2026
ArberSephirotheca pushed a commit to ArberSephirotheca/llama.cpp that referenced this pull request Mar 3, 2026
Ethan-a2 pushed a commit to Ethan-a2/llama.cpp that referenced this pull request Mar 20, 2026
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
… and new jinja template engine (ggml-org#1369)

---------

Co-authored-by: Piotr Wilkin <piotr.wilkin@syndatis.com>

common : add nemotron 3 parsing (ggml-org#18077)

common : add parser for ministral/mistral large 3/devstral 2 (ggml-org#17713)

common : default content to an empty string (ggml-org#18485)

chat: make tool description and parameters optional per OpenAI spec (ggml-org#18478)

Per the OpenAI API specification, both 'description' and 'parameters'
fields in tool function definitions are optional. Previously, the parser
would throw an exception if these fields were missing.

Attempts to fix ggml-org#17667

common : implement new jinja template engine (ggml-org#18462)
---------

Co-authored-by: Alde Rojas <hello@alde.dev>
Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>

jinja: correct member access rule (ggml-org#18905)

jinja : fix lexing of float literals with sign (ggml-org#18901)

jinja : add missing tojson filter for bool (ggml-org#18900)

jinja : attribute support for join, map and sort (ggml-org#18883)

jinja : fix object item order (and properly implement dictsort) (ggml-org#18904)

tests : add test-jinja -py option for cross-checking (ggml-org#18906)

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

---------

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

ci : run test-jinja -py on high perf [no ci] (ggml-org#18916)

jinja : fix undefined keys and attributes and int/float as bool (ggml-org#18924)

jinja: support none|string (ggml-org#18995)

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

---------

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

jinja : implement mixed type object keys (ggml-org#18955)

---------

Co-authored-by: Xuan Son Nguyen <son@huggingface.co>

jinja : undefined should be treated as sequence/iterable (return string/array) by filters/tests (ggml-org#19147)

`tojson` is not a supported `undefined` filter

keep it DRY and fix some types

jinja : do not pass empty tools and add some none filters (ggml-org#19176)

jinja : add unordered_map include to value.h [no ci] (ggml-org#19205)

jinja : add missing 'in' test to template engine (ggml-org#19004) (ggml-org#19239)

The jinja template parser was missing the 'in' test from
global_builtins(), causing templates using reject("in", ...),
select("in", ...), or 'x is in(y)' to fail with
"selectattr: unknown test 'in'".

This broke tool-calling for Qwen3-Coder and any other model
whose chat template uses the 'in' test.

Added test_is_in supporting array, string, and object containment
checks, mirroring the existing 'in' operator logic in runtime.cpp.

Includes test cases for all three containment types plus
reject/select filter usage.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Sid Mohan <sidmohan0@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Xuan Son Nguyen <son@huggingface.co>

Add Jinja support for "indent" string filter (ggml-org#19529)

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

---------

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>

add vendor

refactor chat

server : support preserving reasoning_content in assistant message (ggml-org#18994)

chat : fix translategemma crash on common_chat_format_example (ggml-org#19019)

chat: fix language input for translategemma (ggml-org#19052)

Co-authored-by: Aldehir Rojas <hello@alde.dev>

---------

Co-authored-by: Aldehir Rojas <hello@alde.dev>

chat: fix case where template accepts type content only (ggml-org#19419)

mtmd : chat : Fix extra \n between text and media marker (ggml-org#19595)

Thanks to @tugot17 for detecting and reporting the issue.

For vision models (e.g. LFM2.5-VL-1.6B and Qwen/Qwen3-VL-4B-Instruct) `llama-mtmd-cli` produces identical output to HF implementation.

However `llama-server` doesn't. I traced it down to extra newline
inserted after `<__media__>`.

This happens in `to_json_oaicompat`, that treats media markers as text
and joins all parts with `\n` separator.

PR introduces new type `media_marker` and uses it for media markers.
Extra logic is added to prevent insertion of newlines before and after
media markers.

With this change number of input tokens is identical to HF
implementation and as a result the output is also identical.

I explored other ways to address the issue
* remove completely `\n` between text parts in `to_json_oaicompat`
* merge text messages in server-common.cpp before sending them to `to_json_oaicompat`

Please propose alternative ways of fixing this issue.

Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com>

---------

Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com>

common : merge qwen3-coder and nemotron nano 3 parsers (ggml-org#19765)

common : fix improper trimming in XML parser on complete message (ggml-org#19805)

Co-authored-by: Jules LEIDELINGER <11395311+julio75012@users.noreply.github.com>

jinja: correct stats for tojson and string filters (ggml-org#19785)

jinja : correct default size for string slices (ggml-org#19913)

common : handle unicode during partial json parsing (ggml-org#16526)

common : fix json schema with '\' in literals (ggml-org#17307)

add back qwen_coder_xml and mirothinker

Co-authored-by: Aldehir Rojas <hello@alde.dev>
rsenthilkumar6 pushed a commit to rsenthilkumar6/llama.cpp that referenced this pull request May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jinja parser Issues related to the jinja parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants