Skip to content

Commit 7256569

Browse files
committed
print received contents on parse error
1 parent 905996d commit 7256569

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

common/chat.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "json-schema-to-grammar.h"
66
#include "log.h"
77
#include "regex-partial.h"
8-
#include "parsers/harmony.h"
98

109
#include <minja/chat-template.hpp>
1110
#include <minja/minja.hpp>
@@ -1366,6 +1365,15 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
13661365
static const common_regex browser_tool_call_regex("browser\\.(search|open|find)[\\s\\S]*<\\|message\\|>");
13671366
static const common_regex python_tool_call_regex("python\\s?(?:<|constrain|>code)?<\\|message\\|>");
13681367

1368+
auto consume_until_start = [&]() {
1369+
if (auto res = builder.try_find_regex(start_regex, std::string::npos, false)) {
1370+
auto begin = res->groups[0].begin;
1371+
builder.move_to(begin);
1372+
return res->prelude;
1373+
}
1374+
return builder.consume_rest();
1375+
};
1376+
13691377
auto tool_call = [&]() {
13701378
if (auto res = builder.try_consume_regex(user_tool_call_regex)) {
13711379
auto name = builder.str(res->groups[1]);
@@ -1379,7 +1387,7 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
13791387
auto args = builder.consume_rest();
13801388
LOG_DBG("builtin tool call to browser.%s %s", name.c_str(), args.c_str());
13811389
} else {
1382-
throw common_chat_msg_parse_exception("expected function call");
1390+
throw common_chat_msg_parse_exception("expected function call, got: " + consume_until_start());
13831391
}
13841392
};
13851393

@@ -1391,15 +1399,15 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
13911399
builder.add_content(builder.consume_rest());
13921400
}
13931401
} else {
1394-
throw common_chat_msg_parse_exception("expected: \" to=\" or <|message|>");
1402+
throw common_chat_msg_parse_exception("expected: \" to=\" or <|message|>, got: " + consume_until_start());
13951403
}
13961404
};
13971405

13981406
auto final = [&]() {
13991407
if (builder.try_consume_regex(message_regex)) {
14001408
builder.add_content(builder.consume_rest());
14011409
} else {
1402-
throw common_chat_msg_parse_exception("expected: <|message|>");
1410+
throw common_chat_msg_parse_exception("expected: <|message|>, got: " + consume_until_start());
14031411
}
14041412
};
14051413

@@ -1413,7 +1421,7 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
14131421
builder.add_reasoning_content(builder.consume_rest());
14141422
}
14151423
} else {
1416-
throw common_chat_msg_parse_exception("expected: <|message|>");
1424+
throw common_chat_msg_parse_exception("expected: <|message|>, got: " + consume_until_start());
14171425
}
14181426
};
14191427

@@ -1430,30 +1438,30 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
14301438
}
14311439
}
14321440
} else {
1433-
throw common_chat_msg_parse_exception("expected: <|channel|>");
1441+
throw common_chat_msg_parse_exception("expected: <|channel|>, got: " + consume_until_start());
14341442
}
14351443
};
14361444

14371445
auto start = [&]() {
14381446
if (builder.try_consume_regex(assistant_regex)) {
14391447
channel();
14401448
} else {
1441-
throw common_chat_msg_parse_exception("expected: <|assistant|>");
1449+
throw common_chat_msg_parse_exception("expected: <|assistant|>, got: " + consume_until_start());
14421450
}
14431451
};
14441452

14451453
try {
14461454
channel();
14471455
} catch (const common_chat_msg_parse_exception & e) {
1448-
LOG_ERR("Parse error: %s", e.what());
1456+
LOG_ERR("Parse error: %s\n", e.what());
14491457
}
14501458

14511459
// Read in complete messages until done or partial exception raised
14521460
while (builder.try_find_literal("<|start|>")) {
14531461
try {
14541462
start();
14551463
} catch (const common_chat_msg_parse_exception & e) {
1456-
LOG_ERR("Parse error: %s", e.what());
1464+
LOG_ERR("Parse error: %s\n", e.what());
14571465
}
14581466
}
14591467

0 commit comments

Comments
 (0)