From bac2a2e959d1442fa48c1095055e41466068858d Mon Sep 17 00:00:00 2001 From: Mateusz Charytoniuk Date: Mon, 9 Nov 2026 13:30:57 +0100 Subject: [PATCH] Read finished answers the way llama.cpp parses final messages so an unclosed reasoning tail stays reasoning --- llama-cpp-bindings-sys/wrapper_chat_session.cpp | 3 ++- .../tests/generation_end_classification.rs | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/llama-cpp-bindings-sys/wrapper_chat_session.cpp b/llama-cpp-bindings-sys/wrapper_chat_session.cpp index 4f9e1c47..a82814bf 100644 --- a/llama-cpp-bindings-sys/wrapper_chat_session.cpp +++ b/llama-cpp-bindings-sys/wrapper_chat_session.cpp @@ -417,7 +417,8 @@ extern "C" auto llama_rs_chat_session_read( reading->generation_prompt_length = generation_prompt_length; reading->context.input = std::move(parse_input.text); reading->context.tokens = std::move(parse_input.tokens); - reading->context.flags = is_partial ? COMMON_PEG_PARSE_FLAG_LENIENT : COMMON_PEG_PARSE_FLAG_COMPLETE; + reading->context.flags = is_partial ? COMMON_PEG_PARSE_FLAG_LENIENT + : COMMON_PEG_PARSE_FLAG_LENIENT | COMMON_PEG_PARSE_FLAG_COMPLETE; reading->result = session->session->parser().parse(reading->context); reading->matches_format = is_partial diff --git a/llama-cpp-bindings-tests/tests/generation_end_classification.rs b/llama-cpp-bindings-tests/tests/generation_end_classification.rs index b24290c5..e505db19 100644 --- a/llama-cpp-bindings-tests/tests/generation_end_classification.rs +++ b/llama-cpp-bindings-tests/tests/generation_end_classification.rs @@ -63,7 +63,7 @@ fn visible_text_in(outcomes: &[IngestOutcome], section: SampledTokenSection) -> n_batch = 128, n_ubatch = 64, )] -fn an_answer_stopped_inside_unclosed_reasoning_releases_its_unsettled_tail_as_content( +fn an_answer_stopped_inside_unclosed_reasoning_releases_its_unsettled_tail_as_reasoning( fixture: &LlamaFixture<'_>, ) -> Result<()> { const SETTLED_REASONING: &str = "Let me think"; @@ -90,8 +90,8 @@ fn an_answer_stopped_inside_unclosed_reasoning_releases_its_unsettled_tail_as_co visible_text_in(&outcomes, SampledTokenSection::Pending), ], [ - SETTLED_REASONING.to_owned(), - UNSETTLED_TAIL.to_owned(), + format!("{SETTLED_REASONING}{UNSETTLED_TAIL}"), + String::new(), String::new(), String::new() ]