response_parse.cpp¶
src/inference/response_parse.cpp
@file response_parse.cpp @brief Template-first / adapter-second parse rule (gh#108).
Owners: @tvanfossen
calls_satisfy_schema
bool entropic::calls_satisfy_schema(const std::vector< ToolCall > &calls, const std::string &tools_json)
Check that every call's declared required parameters are present.
src/inference/response_parse.cpp:110
Source · 9 lines
bool calls_satisfy_schema(const std::vector<ToolCall>& calls,
const std::string& tools_json) {
if (calls.empty() || tools_json.empty()) { return true; }
for (const auto& call : calls) {
if (!one_call_satisfies_schema(call, tools_json)) { return false; }
}
return true;
}Activity · 6 steps
Requirements
one_call_satisfies_schema
static bool entropic::one_call_satisfies_schema(const ToolCall &call, const std::string &tools_json)
Check one call's declared required parameters are all present.
src/inference/response_parse.cpp:89
Source · 10 lines
static bool one_call_satisfies_schema(const ToolCall& call,
const std::string& tools_json) {
bool known = false;
auto required = required_params(tools_json, call.name, known);
// An unknown tool is not evidence of a truncated parse.
if (!known || required.empty()) { return true; }
auto args = nlohmann::json::parse(call.arguments_json, nullptr, false);
return args.is_object() && has_all_required(args, required, call.name);
}Activity · 6 steps
Requirements
parse_model_response
ParsedModelResponse entropic::parse_model_response(LlamaCppBackend *llama, ChatAdapter *adapter, const std::string &raw)
Parse a raw emission: template first, adapter second.
src/inference/response_parse.cpp:172
Called by apply_adapter_parse, iface_parse_tool_calls
Calls parse_tool_calls, strip_think_blocks, try_template_parse
Source · 25 lines
ParsedModelResponse parse_model_response(LlamaCppBackend* llama,
ChatAdapter* adapter,
const std::string& raw) {
ParsedModelResponse out;
if (!try_template_parse(llama, raw, out)) {
if (adapter != nullptr) {
auto parsed = adapter->parse_tool_calls(raw);
out.content = std::move(parsed.cleaned_content);
out.tool_calls = std::move(parsed.tool_calls);
} else {
out.content = raw;
}
}
// Content cleanup ALWAYS runs the adapter's strip, on either branch.
// Idempotent: a no-op when the template already removed reasoning. This is
// what closes the gemma4 hole — before v2.10.3 the template branch was the
// only one that stripped channels, and it is unreachable without a tooled
// render.
if (adapter != nullptr) {
out.content = adapter->strip_think_blocks(out.content);
}
return out;
}Activity · 11 steps
Requirements
try_template_parse
static bool entropic::try_template_parse(LlamaCppBackend *llama, const std::string &raw, ParsedModelResponse &out)
Tool calls from the template parser, when trustworthy.
src/inference/response_parse.cpp:136
Called by parse_model_response
Calls active_tools_json, apply_action_envelope_recovery, calls_satisfy_schema, common_chat_parse_reliable, parse_response
Source · 15 lines
static bool try_template_parse(LlamaCppBackend* llama, const std::string& raw,
ParsedModelResponse& out) {
if (llama == nullptr || !llama->common_chat_parse_reliable()) {
return false;
}
auto parsed = llama->parse_response(raw);
apply_action_envelope_recovery(parsed.tool_calls, raw); // gh#88
if (!calls_satisfy_schema(parsed.tool_calls, llama->active_tools_json())) {
return false;
}
out.content = std::move(parsed.content);
out.tool_calls = std::move(parsed.tool_calls);
out.used_template = true;
return true;
}