Skip to main content
The SSE Find & Replace action enables you to modify Server-Sent Events stream content by finding and replacing text patterns using regular expressions. This is particularly useful for modifying streaming AI model responses in real-time, such as redacting sensitive information or transforming content as it streams to clients.

Configuration reference

The Traffic Policy configuration reference for this action.

Supported phases

on_event_stream_message

Type

sse-find-replace

Configuration fields

replacements
array
Required
Array of replacement rules to apply to SSE messages. Rules are applied in order.Minimum 1 replacement required.
field
string
The SSE field to apply the replacement to. Valid values are data and retry. Defaults to data if not specified. CEL interpolation is supported.
from
string
Required
Supports CEL
Regular expression pattern to match. Supports RE2 syntax. CEL interpolation is supported for dynamic patterns.
Replacement string. Use $1, $2, etc. to reference capture groups from the pattern. CEL interpolation is supported for dynamic replacements. If omitted or empty, matched text is deleted.

Behavior

When this action executes, it processes each SSE message as it streams through ngrok, applying replacement rules to the specified fields.

SSE message structure

Server-Sent Events consist of messages with fields like data, event, id, and retry. This action currently supports modifying the data and retry fields.

Pattern matching

The from field accepts RE2 regular expressions. All matches within the specified field are replaced, not just the first occurrence.

Streaming behavior

Unlike the request/response body find & replace actions, this action processes messages in real-time as they stream through. Each SSE message is processed independently.

Capture groups

You can use capture groups in your pattern and reference them in the replacement:
replacements:
  - field: data
    from: '"sensitive_id":\s*"([^"]+)"'
    to: '"sensitive_id": "[REDACTED]"'

CEL expressions

The field, from, and to parameters all support CEL interpolation for dynamic values:
replacements:
  - field: "${vars.target_field}"
    from: "${vars.pattern}"
    to: "${vars.replacement}"
When using CEL in the from field, the resulting string must be a valid regular expression.

Examples

Redacting sensitive data from streaming AI responses

When using streaming AI responses (like OpenAI’s chat completions with stream: true), you can redact sensitive information in real-time.

Example Traffic Policy document

on_event_stream_message:
  - actions:
      - type: sse-find-replace
        config:
          replacements:
            # Redact SSN patterns in streaming content
            - field: data
              from: "\\d{3}-\\d{2}-\\d{4}"
              to: "[SSN REDACTED]"
            # Redact email addresses
            - field: data
              from: "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
              to: "[EMAIL REDACTED]"

Modifying streaming AI output format

Transform streaming content as it’s delivered to clients:
on_event_stream_message:
  - actions:
      - type: sse-find-replace
        config:
          replacements:
            # Add emphasis to certain keywords
            - field: data
              from: "\\b(IMPORTANT|WARNING|NOTE)\\b"
              to: "**$1**"

Filtering profanity from streaming responses

Remove or replace inappropriate content from AI responses in real-time:
on_event_stream_message:
  - actions:
      - type: sse-find-replace
        config:
          replacements:
            - field: data
              from: "\\b(badword1|badword2|badword3)\\b"
              to: "[filtered]"

Modifying retry behavior

Change the retry interval in SSE streams:
on_event_stream_message:
  - actions:
      - type: sse-find-replace
        config:
          replacements:
            - field: retry
              from: "\\d+"
              to: "5000"

Action result variables

The following variables are made available for use in subsequent expressions and CEL interpolations after the action has run. Variable values will only apply to the last action execution, results are not concatenated.
actions.ngrok.sse_find_replace.replacements
array
Array of objects describing which replacement rules matched.
replacement_index
integer
The zero-based index of the replacement rule that matched.
matched_content
string
The first matched content from this replacement rule.