Transform

  • This filter should be configured with the type URL type.googleapis.com/envoy.extensions.filters.http.transform.v3.TransformConfig.

  • v3 API reference

This filter can be used to transform HTTP requests and responses with substitution format string. For example, it can be used to:

  • Modify request or response headers based on values in the body and refresh the routes accordingly.

  • Modify JSON request or response bodies.

Configuration

The following example configuration will extract the model field from a JSON request body and add it as a request header model-header before forwarding the request to the upstream. At the same time, it will also rewrite the model field in the JSON response body as new-model.

At the response path, the filter is configured to extract the completion_tokens and prompt_tokens fields from the JSON response body and add them as response headers.

42          http_filters:
43          - name: transform
44            typed_config:
45              "@type": type.googleapis.com/envoy.extensions.filters.http.transform.v3.TransformConfig
46              request_transformation:
47                headers_mutations:
48                - append:
49                    header:
50                      key: "model-header"
51                      value: "%REQUEST_BODY(model)%"
52                    append_action: OVERWRITE_IF_EXISTS_OR_ADD
53                body_transformation:
54                  body_format:
55                    json_format:
56                      model: "new-model"
57                  action: MERGE
58              response_transformation:
59                headers_mutations:
60                - append:
61                    header:
62                      key: "prompt-tokens"
63                      value: "%RESPONSE_BODY(usage:prompt_tokens)%"
64                    append_action: OVERWRITE_IF_EXISTS_OR_ADD
65                - append:
66                    header:
67                      key: "completion-tokens"
68                      value: "%RESPONSE_BODY(usage:completion_tokens)%"
69                    append_action: OVERWRITE_IF_EXISTS_OR_ADD

Per-route configuration

Per-route overrides may be supplied via the same protobuf API in the typed_per_filter_config field of route configuration.

The following example configuration will override the global filter configuration to keep only the request headers transformation.

26                  cluster: upstream_com
27                typed_per_filter_config:
28                  transform:
29                    "@type": type.googleapis.com/envoy.extensions.filters.http.transform.v3.TransformConfig
30                    request_transformation:
31                      headers_mutations:
32                      - append:
33                          header:
34                            key: "model-header"
35                            value: "%REQUEST_BODY(model)%"

Enhanced substitution format

The substitution format specifier could be used for both headers and body transformations.

And except the commonly used format specifiers, there are some additional format specifiers provided by the transform filter:

  • %REQUEST_BODY(KEY*)%: the request body. And Key KEY is an optional lookup key in the namespace with the option of specifying nested keys separated by ‘:’.

  • %RESPONSE_BODY(KEY*)%: the response body. And Key KEY is an optional lookup key in the namespace with the option of specifying nested keys separated by ‘:’.