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
lines 112–112◇ if (calls.empty() || tools_json.empty())lines 112–112return true;lines 114–114◇ for (const auto& call : calls)lines 115–115◇ if (!one_call_satisfies_schema(call, tools_json))lines 115–115return false;lines 117–117return true;

Requirements

REQ-INFER-010

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

Show on the map →

Called by calls_satisfy_schema

Calls call, has_all_required, required_params

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
lines 91–91bool known = false;lines 92–92auto required = required_params(tools_json, call.name, known);lines 94–94◇ if (!known || required.empty())lines 94–94return true;lines 96–96auto args = nlohmann::json::parse(call.arguments_json, nullptr, false);lines 97–97return args.is_object() && has_all_required(args, required, call.name);

Requirements

REQ-INFER-010

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

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
lines 175–175ParsedModelResponse out;lines 177–177◇ if (!try_template_parse(llama, raw, out))lines 178–178◇ if (adapter != nullptr)lines 179–179auto parsed = adapter->parse_tool_calls(raw);lines 180–180out.content = std::move(parsed.cleaned_content);lines 181–181out.tool_calls = std::move(parsed.tool_calls);lines 182–182elselines 183–183out.content = raw;lines 192–192◇ if (adapter != nullptr)lines 193–193out.content = adapter->strip_think_blocks(out.content);lines 195–195return out;

Requirements

REQ-INFER-010

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

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;
}
Activity · 10 steps
lines 138–138◇ if (llama == nullptr || !llama->common_chat_parse_reliable())lines 139–139return false;lines 141–141auto parsed = llama->parse_response(raw);lines 142–142apply_action_envelope_recovery(parsed.tool_calls, raw);lines 143–143◇ if (!calls_satisfy_schema(parsed.tool_calls, llama->active_tools_json()))lines 144–144return false;lines 146–146out.content = std::move(parsed.content);lines 147–147out.tool_calls = std::move(parsed.tool_calls);lines 148–148out.used_template = true;lines 149–149return true;

Requirements

REQ-INFER-010