When it fires
Once per answer, when the provider’s answer is complete and before it is saved to the chat history: on the path that does not stream and on the streaming one, for the chatbot, the admin assistant and the search answer, and for the text of a quick action whose callback answers without the provider. $payload['assistant_surface'] is admin_assistant or search outside the chat; in the chat it is not set.
What the filter returns is the answer from then on: the history stores it, the JSON answer and the last event of a stream carry it, and kimaai_chat_response_saved fires after it. On a streamed answer the visitor first sees the text as it arrives, and the filtered text replaces it when the answer is complete. That is why KimaAI does not let an answer stream while the output guardrails are on and banned terms are set.
KimaAI hooks it itself: the citations and the groundedness note (Pro) at priority 20, the output guardrails at priority 30, so a callback at the default priority 10 runs before them and its text is checked for banned terms too.
Parameters
| Name | Type | Description |
|---|---|---|
ai_response | string | The provider’s answer text. |
payload | array | The request as the provider got it, keys in snake_case, with session_id, chatbot_settings and assistant_surface outside the chat. |
provider | string | The provider, openai, openrouter or ollama. |
Returns
string: The answer text to save and show.
Example
add_filter(
'kimaai_chatbot_final_message',
function (string $ai_response, array $payload): string {
// Every answer about prices says what they include.
if (stripos($ai_response, 'price') !== false && stripos($ai_response, 'VAT') === false) {
$ai_response .= "\n\nAll prices include VAT.";
}
return $ai_response;
},
10,
2
);
Related
kimaai_chatbot_guardrails_violation: fires when the answer contains a banned term.kimaai_chat_response_saved: fires once the answer is saved.kimaai_chatbot_response_payload: changes the JSON answer, only when it does not stream.