kimaai_chatbot_response_payload Filter #
The kimaai_chatbot_response_payload filter lets you modify the AI response payload right after the AI provider returns and before the response is sent back to the frontend.
Use it to:
- sanitize or redact content
- enforce formatting (Markdown → plain text, add prefixes, etc.)
- attach extra fields for your frontend (metadata, citations, debug flags)
- normalize response structure across providers
- apply business rules (e.g., remove links, block certain output)
When it runs #
In the chatbot REST handler:
- The plugin calls the AI provider:
AiService::run($ai_payload, 'chat') - It receives
$response(array) - It applies this filter:
$response = apply_filters('kimaai_chatbot_response_payload', $response, $ai_payload, $payload, $chatbot_settings);
- Fires
kimaai_chatbot_after_response - Returns the final
$responsein aWP_REST_Response(HTTP200)
This filter is your last chance to change what the frontend receives on success.
Filter signature #
/**
* Filter: mutate the AI response before returning to the client.
*
* @param array $response Response from the AI service.
* @param array $ai_payload The provider payload that was sent.
* @param array $payload Normalized payload.
* @param array $chatbot_settings Settings.
* @return array
*/
$response = apply_filters('kimaai_chatbot_response_payload', $response, $ai_payload, $payload, $chatbot_settings);
Parameters #
1) $response (array) #
The response array returned by AiService::run() (and subsequently returned to the frontend after your modifications).
The exact shape depends on your AiService implementation and provider, but commonly includes fields like:
message/contentroleusage(tokens, etc.)- tool call information (if applicable)
2) $ai_payload (array) #
The AI request payload that was sent (prompt, model, tool info, etc.).
3) $payload (array) #
Normalized request context (prompt, postId, sessionId, selectedTools, etc.).
4) $chatbot_settings (array) #
Settings used for this request.
Return value #
Return the modified $response array.
Basic example: add a server timestamp #
add_filter('kimaai_chatbot_response_payload', function (array $response): array {
$response['serverTime'] = time();
return $response;
}, 10, 1);
Example: redact emails in the AI output #
add_filter('kimaai_chatbot_response_payload', function (array $response): array {
// Adjust the key to match your response shape.
if (!isset($response['content']) || !is_string($response['content'])) {
return $response;
}
$response['content'] = preg_replace(
'/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/iu',
'[redacted email]',
$response['content']
) ?? $response['content'];
return $response;
}, 10, 1);
Example: strip links from the output #
Useful if you don’t want the chatbot to produce clickable URLs.
add_filter('kimaai_chatbot_response_payload', function (array $response): array {
$key = isset($response['content']) ? 'content' : (isset($response['message']) ? 'message' : null);
if (!$key || !is_string($response[$key])) {
return $response;
}
// Remove http(s) URLs.
$response[$key] = preg_replace('/https?:\/\/\S+/iu', '[link removed]', $response[$key]) ?? $response[$key];
return $response;
}, 10, 1);
Example: normalize output to a content key #
If different providers return different shapes, you can standardize for your frontend.
add_filter('kimaai_chatbot_response_payload', function (array $response): array {
if (!isset($response['content'])) {
if (isset($response['message']) && is_string($response['message'])) {
$response['content'] = $response['message'];
unset($response['message']);
}
}
return $response;
}, 10, 1);
Example: add debug metadata for admins only #
add_filter('kimaai_chatbot_response_payload', function (
array $response,
array $ai_payload,
array $payload
): array {
if (!is_user_logged_in() || !current_user_can('manage_options')) {
return $response;
}
$response['_debug'] = [
'provider' => (string) ($ai_payload['aiProvider'] ?? ''),
'model' => (string) ($ai_payload['aiModel'] ?? ''),
'sessionId' => (string) ($payload['sessionId'] ?? ''),
'tools_count' => !empty($ai_payload['tools']) ? count($ai_payload['tools']) : 0,
];
return $response;
}, 10, 3);
Notes and best practices #
- This filter runs only on the success path (HTTP
200). Exceptions are handled bykimaai_chatbot_error. - Don’t
echooutput—this is a REST request. - Keep transformations efficient; this runs for every chatbot message.
- Avoid leaking sensitive info:
- be careful adding debug fields
- consider removing token usage or internal traces from responses shown to end users
Related hooks #
kimaai_chatbot_after_response— telemetry after successful responseskimaai_chatbot_error— telemetry on exceptionskimaai_chatbot_ai_payload— modify outgoing request payloadkimaai_chatbot_prompt— normalize/redact user input earlier