Skip to content

[➕ Feature]: Add keep.dict_set helper for dict augmentation in workflow expressions #6488

Closed

Description

@DANIILSKRIPCHENKO

Is your feature request related to a problem? Please describe.
Workflow body fields that need to carry a dict (e.g. an HTTP provider's body.commonAnnotations) cannot be built from existing data + a few overrides.
The expression evaluator in keep/iohandler/iohandler.py is a hand-rolled AST walker, not Python eval(), and parse() only returns a dict when the
entire field value is a single top-level keep.X(...) call whose arguments are literals or other keep.X(...) calls. Anything else (dict-spread,
attribute access, vars, multiple top-level expressions) gets stringified via str(val) and ends up in the payload as a Python-repr blob.

In practice this means there is no way to take an existing dict (e.g. keep.dictget(keep.json_loads('{{ alert }}'), 'annotations', {})) and add a
single extra key. The only workarounds are (a) enumerate every key in the target dict by hand, or (b) drop the field entirely.

Concrete trigger: keep/providers/prometheus_provider/prometheus_provider.py L193 pops description out of annotations into AlertDto.description. A
workflow that forwards alerts back to an AM-compatible downstream (Grafana OnCall, another Alertmanager, etc.) can't reconstruct the full annotations
dict because there is no way to dict | {"description": alert.description} inside a workflow expression. We have to enumerate summary, description,
message, dashboard_url, runbook_url, … explicitly and maintain the list as new annotations appear.

Describe the solution you'd like
Add a small dict helper to keep/functions/init.py:

  def dict_set(d: dict, key: str, value) -> dict:
      """Return a new dict equal to `d` with `key` set to `value`."""
      return {**d, key: value}

  Optionally a variadic merge for the general case:

  def dict_merge(*dicts: dict) -> dict:
      """Merge dicts left-to-right; later keys override earlier."""
      out = {}
      for d in dicts:
          out.update(d)
      return out

Both compose with existing helpers and stay inside the AST walker's "one Call with literal/Call args" constraint. Forwarding annotations with
description re-added becomes one expression:

commonAnnotations: "keep.dict_set(keep.dictget(keep.json_loads('{{ alert }}'), 'annotations', {}), 'description', keep.dictget(keep.json_loads('{{
alert }}'), 'description', ''))"

Describe alternatives you've considered

  1. Enumerate every annotation explicitly. Works, but loses generic-forwarding: any new annotation on the source alert requires a workflow change. We
    currently do this — keep.dictget(keep.dictget(keep.json_loads('{{ alert }}'), 'annotations', {}), '', '') repeated per field.
  2. Dict-spread inside a keep.X(...) call, e.g. keep.json_loads(keep.json_dumps({**keep.dictget(...), "description": ...})). Fails because
    _parse_token calls ast.literal_eval on dict-literal Call args; **spread and nested Call are not literals → ValueError.
  3. Using vars / aliases to hold the base dict. vars.X is stored without rendering (step.py L167-168) so it appears literally. aliases.X is rendered
    but stringified via str(dict) (Python repr, not JSON) when interpolated; inside a keep.X(...) call the AST walker treats aliases.X as ast.Attribute
    and crashes on arg.id.
  4. Patch the provider (e.g. .pop("description") → .get). Breaks the cross-provider normalization invariant (every provider has its own "main
    description" field — Sentry culprit, GCP summary, SignalFx displayBody, Prometheus description — Keep canonicalizes them into AlertDto.description).
    Not a real alternative.

dict_set is the smallest change that preserves all current behavior and unblocks dict augmentation in workflow expressions. It's also useful far
beyond this specific pop: any workflow that forwards payloads to a downstream system with a couple of extra fields runs into the same wall.

Additional context

  • Relevant source: keep/iohandler/iohandler.py — extract_keep_functions (L118-174), parse (L189-291), _parse_token (L293-410).
  • The .pop("description") lives at keep/providers/prometheus_provider/prometheus_provider.py L193-195 (and similarly in grafana_provider.py L841).
  • Real-world use case: forwarding Prometheus security alerts to Grafana OnCall's AlertManager integration through a Keep workflow. With dict_set we
    can ship a single-line generic forwarder. Without it, the workflow has to be updated every time a new annotation is introduced upstream.

Activity

  1. added
    Feature
    on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions