keep icon indicating copy to clipboard operation
keep copied to clipboard

[🐛 Bug]: cyaml.dump() loses block literal style (code: |) for strings containing non-ASCII characters

Open hakatt opened this issue 9 months ago • 2 comments

Description

keep/functions/cyaml.py uses QuotePreservingDumper to round-trip workflow YAML while preserving original quoting and block literal (|) style. However, when a workflow's code: block contains non-ASCII characters (e.g. an em-dash — in a comment), PyYAML's CDumper silently falls back to double-quoted style, discarding the block literal.

Steps to reproduce

  1. Create a workflow with a Python step where the code: block contains a non-ASCII character:
    steps:
    - name: my-step
      provider:
        type: python
        with:
          code: |
            {
                # note — this comment has an em-dash
                'key': 'value',
            }
    
  2. Save/POST the workflow via the API.
  3. Fetch it back with GET /workflows/<id>/raw and inspect the code: field.

Expected behaviour

code: |
  {
      # note — this comment has an em-dash
      'key': 'value',
  }

Actual behaviour

code: "{\n    # note \u2014 this comment has an em-dash\n    'key': 'value',\n}\n"

The block literal is replaced with a double-quoted inline string with \n and \uXXXX escapes. This makes the workflow YAML hard to read and edit in the UI editor.

Root cause

yaml.CDumper (and yaml.Dumper) default to allow_unicode=False, which causes any string containing characters outside ASCII to be emitted as a double-quoted scalar regardless of the requested style. PyYAML's represent_scalar only honours the block style hint when the string can be emitted as-is.

cyaml.dump() sets several sensible defaults (default_flow_style=False, sort_keys=False) but does not set allow_unicode=True.

Fix

Add allow_unicode=True to the defaults in cyaml.dump():

kwds.setdefault('allow_unicode', True)

This is a one-line change. It has no effect on purely-ASCII content and does not change any existing serialisation behaviour for ASCII strings.

Environment

  • Keep version: 0.49.1
  • Python: 3.12
  • PyYAML: 6.x (CDumper)

hakatt avatar Feb 26 '26 18:02 hakatt

Hi! I'd like to work on this issue. The fix seems straightforward — adding allow_unicode=True to the cyaml.dump() defaults. I have experience with Python and YAML processing. I can open a PR within 24 hours. Is this still available?

yuzebin avatar Feb 27 '26 07:02 yuzebin

Hi! I'd like to work on this issue. The fix seems straightforward — adding allow_unicode=True to the cyaml.dump() defaults. I have experience with Python and YAML processing. I can open a PR within 24 hours. Is this still available?

PR already opened: https://github.com/keephq/keep/pull/5687

hakatt avatar Feb 27 '26 10:02 hakatt