Skip to content

fix(python-sdk): port current JS stripAnsi regex to strip_ansi_escape_codes#1545

Open
mishushakov wants to merge 3 commits into
mainfrom
strip-ansi-python-sdk
Open

fix(python-sdk): port current JS stripAnsi regex to strip_ansi_escape_codes#1545
mishushakov wants to merge 3 commits into
mainfrom
strip-ansi-python-sdk

Conversation

@mishushakov

@mishushakov mishushakov commented Jul 13, 2026

Copy link
Copy Markdown
Member

Summary

The Python SDK's strip_ansi_escape_codes (used to clean template build log messages) still used the old ansi-regex pattern, while the JS SDK's stripAnsi was rewritten in #895. This ports the current JS regex to Python so both SDKs clean logs identically: OSC sequences (hyperlinks, window titles) are matched non-greedily up to the first string terminator — including content spanning newlines — and CSI sequences are stripped without requiring a terminator.

Following review feedback, both implementations now also strip the remaining ECMA-48 string controls — DCS (Sixel, tmux passthrough), SOS, PM, and APC — through their string terminator, so control payloads don't leak into cleaned logs. This goes beyond upstream chalk/ansi-regex, click, and Rich, none of which fully strip DCS payloads, and restores what the old Python pattern handled.

Also mirrors the Python test suite into the JS SDK (which previously had no stripAnsi tests) — 20 identical cases per side — and verified byte-for-byte identical output between the two implementations on all of them. Includes a patch changeset for e2b and @e2b/python-sdk.

Example

Log messages that previously leaked OSC or DCS sequences into template build output are now cleaned:

from e2b.template.utils import strip_ansi_escape_codes

strip_ansi_escape_codes("\x1b]8;;https://e2b.dev\x07E2B\x1b]8;;\x07")  # "E2B"
strip_ansi_escape_codes("\x1b]0;title\nstill title\x07done")           # "done"
strip_ansi_escape_codes("\x1b[38:2::255:0:0mRED\x1b[0m")               # "RED"
strip_ansi_escape_codes("\x1bPq#0;2;0;0;0~~@@\x1b\\image")             # "image" (Sixel DCS)

🤖 Generated with Claude Code

…_codes

The Python port still used the old ansi-regex pattern while the JS SDK
was rewritten (#895) to match OSC sequences non-greedily up to the
first string terminator and CSI sequences without requiring one.
Aligns both implementations and mirrors the 14-case Python test suite
into the JS SDK, which had no stripAnsi tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2a058f7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
e2b Patch
@e2b/python-sdk Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cursor

cursor Bot commented Jul 13, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Localized log-sanitization changes with broad mirrored tests; no auth, data, or API surface changes beyond cleaner template build messages.

Overview
Aligns template build log cleaning so the Python SDK’s strip_ansi_escape_codes matches the JS SDK’s stripAnsi regex, and both SDKs strip the same ECMA-48 control sequences.

Both implementations now treat OSC, DCS, SOS, PM, and APC as string controls: non-greedy match from ESC through the first string terminator (BEL, ESC\, or ST), including payloads that span newlines. CSI/SGR handling stays aligned (including colon-style params) without requiring a CSI terminator. Python compiles with re.ASCII so \d behavior matches JavaScript on CSI params.

Adds a mirrored Vitest suite for stripAnsi (JS had none) and expands the Python tests with the same cases (hyperlinks, Sixel DCS, unterminated DCS intro, non-ASCII digits). Patch changeset for e2b and @e2b/python-sdk.

Reviewed by Cursor Bugbot for commit 2a058f7. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Package Artifacts

Built from b81fc9c. Download artifacts from this workflow run.

JS SDK (e2b@2.33.1-strip-ansi-python-sdk.0):

npm install ./e2b-2.33.1-strip-ansi-python-sdk.0.tgz

CLI (@e2b/cli@2.13.3-strip-ansi-python-sdk.0):

npm install ./e2b-cli-2.13.3-strip-ansi-python-sdk.0.tgz

Python SDK (e2b==2.32.0+strip.ansi.python.sdk):

pip install ./e2b-2.32.0+strip.ansi.python.sdk-py3-none-any.whl

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f282d25cd8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/python-sdk/e2b/template/utils.py Outdated
Addresses PR review feedback: the OSC-only branch regressed vs the old
Python pattern, which stripped ST-terminated string controls like DCS
(Sixel, tmux passthrough) through their terminator. Widen the branch to
all ECMA-48 string controls (ESC ]/P/X/^/_ ... ST) in both the JS and
Python implementations, keeping them byte-for-byte identical.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Comment thread packages/python-sdk/e2b/template/utils.py
Python's \d matches any Unicode Nd digit by default, so malformed CSI
sequences with e.g. Arabic-Indic digits were stripped in Python but
kept by JS, whose \d is always ASCII-only. re.ASCII restores
byte-for-byte parity; [\s\S] is unaffected. Addresses PR review.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mishushakov
mishushakov enabled auto-merge (squash) July 14, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant