@@ -60,7 +60,9 @@ std::vector<common_chat_msg> common_chat_msgs_parse_oaicompat(const json & messa
60
60
}
61
61
msg.role = message.at (" role" );
62
62
63
- if (message.contains (" content" )) {
63
+ auto has_content = message.contains (" content" );
64
+ auto has_tool_calls = message.contains (" tool_calls" );
65
+ if (has_content) {
64
66
const auto & content = message.at (" content" );
65
67
if (content.is_string ()) {
66
68
msg.content = content;
@@ -81,19 +83,8 @@ std::vector<common_chat_msg> common_chat_msgs_parse_oaicompat(const json & messa
81
83
} else if (!content.is_null ()) {
82
84
throw std::runtime_error (" Invalid 'content' type: expected string or array, got " + content.dump () + " (ref: https://github.com/ggml-org/llama.cpp/issues/8367)" );
83
85
}
84
- } else {
85
- throw std::runtime_error (" Expected 'content' (ref: https://github.com/ggml-org/llama.cpp/issues/8367)" );
86
- }
87
- if (message.contains (" reasoning_content" )) {
88
- msg.reasoning_content = message.at (" reasoning_content" );
89
- }
90
- if (message.contains (" name" )) {
91
- msg.tool_name = message.at (" name" );
92
- }
93
- if (message.contains (" tool_call_id" )) {
94
- msg.tool_call_id = message.at (" tool_call_id" );
95
86
}
96
- if (message. contains ( " tool_calls " ) ) {
87
+ if (has_tool_calls ) {
97
88
for (const auto & tool_call : message.at (" tool_calls" )) {
98
89
common_chat_tool_call tc;
99
90
if (!tool_call.contains (" type" )) {
@@ -118,6 +109,18 @@ std::vector<common_chat_msg> common_chat_msgs_parse_oaicompat(const json & messa
118
109
msg.tool_calls .push_back (tc);
119
110
}
120
111
}
112
+ if (!has_content && !has_tool_calls) {
113
+ throw std::runtime_error (" Expected 'content' or 'tool_calls' (ref: https://github.com/ggml-org/llama.cpp/issues/8367 & https://github.com/ggml-org/llama.cpp/issues/12279)" );
114
+ }
115
+ if (message.contains (" reasoning_content" )) {
116
+ msg.reasoning_content = message.at (" reasoning_content" );
117
+ }
118
+ if (message.contains (" name" )) {
119
+ msg.tool_name = message.at (" name" );
120
+ }
121
+ if (message.contains (" tool_call_id" )) {
122
+ msg.tool_call_id = message.at (" tool_call_id" );
123
+ }
121
124
122
125
msgs.push_back (msg);
123
126
}
0 commit comments