Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion common/chat-peg-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,10 @@ common_peg_parser common_chat_peg_builder::build_json_tools_flat_keys(
return idx_a < idx_b;
});

auto ordered_body = tool_open(literal("{")) + space();
// accept an optional leading "type": "function" field
auto type_field = optional(literal("\"type\"") + space() + literal(":") + space() +
literal("\"function\"") + space() + literal(",") + space());
auto ordered_body = tool_open(literal("{")) + space() + type_field;
Comment thread
aldehir marked this conversation as resolved.
Outdated
for (size_t i = 0; i < parser_pairs.size(); i++) {
ordered_body = ordered_body + parser_pairs[i].first;
if (i < parser_pairs.size() - 1) {
Expand Down
15 changes: 12 additions & 3 deletions common/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,8 @@ static void func_args_not_string(json & messages) {
try {
args = json::parse(args.get<std::string>());
} catch (const std::exception & e) {
throw std::runtime_error("Failed to parse tool call arguments as JSON: " + std::string(e.what()));
// keep the raw arguments string when it is not valid json
LOG_WRN("%s: tool call arguments are not valid json, keeping raw string: %s\n", __func__, e.what());
Comment thread
ServeurpersoCom marked this conversation as resolved.
Outdated
}
}
}
Expand Down Expand Up @@ -2520,8 +2521,16 @@ common_chat_msg common_chat_peg_parse(const common_peg_arena & src_pars
}
return msg;
}
throw std::runtime_error(std::string("Failed to parse input at pos ") + std::to_string(result.end) + ": " +
effective_input.substr(result.end));
if (!is_partial) {
// on a final parse failure log the fragment and raise a clean error
LOG_WRN("%s: unparsed %s output: %s\n", __func__, common_chat_format_name(params.format),
effective_input.substr(result.end).c_str());
throw std::runtime_error(std::string("the model produced output that does not match the expected ") +
common_chat_format_name(params.format) + " format");
}
common_chat_msg msg;
msg.role = "assistant";
return msg;
Comment thread
ServeurpersoCom marked this conversation as resolved.
Outdated
}

common_chat_msg msg;
Expand Down
Loading