Skip to content

Commit e3619c3

Browse files
committed
don't abort reasoning when emitting a tool call
1 parent 2828b32 commit e3619c3

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

common/chat-parser.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,17 +1513,24 @@ common_chat_msg common_chat_parse(const std::string & input, bool is_partial, co
15131513
return common_chat_peg_parse(syntax.parser, input, is_partial, syntax);
15141514
}
15151515
common_chat_msg_parser builder(input, is_partial, syntax);
1516+
bool partial_exception_caught = false;
15161517
try {
15171518
common_chat_parse(builder);
15181519
} catch (const common_chat_msg_partial_exception & ex) {
15191520
LOG_DBG("Partial parse: %s\n", ex.what());
1521+
partial_exception_caught = true;
15201522
if (!is_partial) {
15211523
builder.clear_tools();
15221524
builder.move_to(0);
15231525
common_chat_parse_content_only(builder);
15241526
}
15251527
}
15261528
auto msg = builder.result();
1529+
// Mark tool_call_in_progress if we caught a partial exception during partial parsing
1530+
// and there are tool calls in progress (indicates incomplete tool call parsing)
1531+
if (is_partial && partial_exception_caught && !msg.tool_calls.empty()) {
1532+
msg.tool_call_in_progress = true;
1533+
}
15271534
if (!is_partial) {
15281535
LOG_DBG("Parsed message: %s\n", common_chat_msgs_to_json_oaicompat<json>({msg}).at(0).dump().c_str());
15291536
}

common/chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct common_chat_msg {
5151
std::vector<common_chat_tool_call> tool_calls;
5252
std::string reasoning_content;
5353
common_chat_reasoning_status reasoning_status;
54+
bool tool_call_in_progress = false;
5455
std::string tool_name;
5556
std::string tool_call_id;
5657

tools/server/server-context.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,11 @@ struct server_context_impl {
12101210
if (slot.reasoning == REASONING_STATE_REASONING) {
12111211
slot.n_reasoning_tokens++;
12121212

1213-
if (slot.n_reasoning_tokens >= reasoning_budget) {
1213+
// Detect if we are in the middle of emitting a tool call this step.
1214+
// The parser sets tool_call_in_progress when it catches a partial exception
1215+
// while parsing tool calls, indicating incomplete tool call parsing.
1216+
// We also check for tool call diffs in this token as a fallback.
1217+
if (!parsed_msg.tool_call_in_progress && slot.n_reasoning_tokens >= reasoning_budget) {
12141218
SLT_INF(slot, "reasoning budget exceeded, forcing close with '%s', n_reasoning_tokens = %d, reasoning_budget = %d\n",
12151219
slot.reasoning_end_tag.c_str(), slot.n_reasoning_tokens, reasoning_budget);
12161220

0 commit comments

Comments
 (0)