Skip to content

[Data] Hide Ray Data internal frames from user-code error tracebacks - #64587

Merged
Sparks0219 merged 20 commits into
ray-project:masterfrom
AarryaSaraf:data-udf-error-logging
Jul 18, 2026
Merged

Sparks0219 merged 20 commits into
ray-project:masterfrom
AarryaSaraf:data-udf-error-logging

Conversation

@AarryaSaraf

@AarryaSaraf AarryaSaraf commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

When a UDF passed to a Ray Data transform raises, the error surfaced to the
user is buried: the same RayTaskError/UserCodeException traceback is printed
several times (driver propagation, abort log, executor shutdown log), and each
copy is padded with Ray Data internal frames (executor / scheduler / planner)
the user cannot act on.

This PR makes user-code errors concise while leaving genuine internal/system
errors fully verbose. Every behavior change is gated on
isinstance(..., UserCodeException):

  • RayTaskError.__str__ strips ray/data/_internal/ frames (and their
    continuation / caret lines) when the root cause is a UserCodeException.
  • The driver-side handler (omit_traceback_stdout) logs only the cleaned
    worker-side trace for user-code errors instead of re-dumping the full
    driver→worker traceback.
  • The abort log (process_completed_tasks) and the executor shutdown log no
    longer re-dump the traceback for user-code errors, since it is already
    logged once (cleaned).

Internal frames are now always omitted from stdout. The renamed
DataContext.log_internal_stack_trace flag controls only whether the full
internal trace is written to the Ray Data log file (off by default). The
old log_internal_stack_trace_to_stdout name still works but is deprecated
(removal January 2027) and forwards to the new flag with a DeprecationWarning.
Internal/system errors are untouched — full tracebacks in every channel.
RAY_DATA_RAISE_ORIGINAL_MAP_EXCEPTION=1 still bypasses the wrapping entirely.

Before / after

Repro — a UDF that divides by zero:

import ray
def f(row):
    return {"x": 1 / 0}
ray.data.range(1).map(f).take_all()

Console (stdout)

Before (master): the traceback is dumped twice, each copy padded with
internal map_operator / map_transformer / plan_udf_map_op frames:

ERROR streaming_executor_state.py:708 -- An exception was raised from a task of operator "Map(f)". ...
Traceback (most recent call last):
  File ".../ray/data/_internal/execution/streaming_executor_state.py", line 673, in process_completed_tasks
    bytes_read = task.on_data_ready(
  File ".../ray/data/_internal/execution/interfaces/physical_operator.py", line 276, in on_data_ready
    raise ex from None
  ... (worker.py / auto_init_hook / client_mode_hook frames) ...
ray.exceptions.RayTaskError(UserCodeException): ray::Map(f)() (pid=5427, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=5427, ip=127.0.0.1)
  File ".../ray/data/_internal/execution/operators/map_operator.py", line 855, in _map_task
  File ".../ray/data/_internal/execution/operators/map_transformer.py", line 221, in __next__
  File ".../ray/data/_internal/planner/plan_udf_map_op.py", line 470, in _wrapped_udf_map_fn
    _try_wrap_udf_exception(e)
  File ".../ray/data/_internal/planner/plan_udf_map_op.py", line 486, in _try_wrap_udf_exception
    raise UserCodeException("UDF failed to process a data block.") from e
ray.exceptions.UserCodeException: UDF failed to process a data block.
INFO streaming_executor.py:338 -- ⚠️  Dataset dataset_1_0 execution failed
ERROR exceptions.py:63 -- Exception occurred in user code ... set `DataContext.log_internal_stack_trace_to_stdout` to True.
Traceback (most recent call last):
  ... (the ENTIRE traceback above is repeated a second time) ...
ray.exceptions.UserCodeException: UDF failed to process a data block.

After (identical for log_internal_stack_trace = False or True): printed
once, only the user's frame:

ERROR exceptions.py:68 -- Exception occurred in user code, with the abbreviated stack trace below. The Ray Data internal stack frames are omitted from stdout. To also write the full stack trace to the Ray Data log file at `.../logs/ray-data`, set `DataContext.log_internal_stack_trace` to True.
Traceback (most recent call last):
  File ".../scratchpad/repro.py", line 13, in <module>
    ray.data.range(1).map(f).take_all()
  ... (a few driver frames) ...
  File ".../ray/data/exceptions.py", line 101, in handle_trace
    raise e.with_traceback(None)
ray.exceptions.RayTaskError(UserCodeException): ray::Map(f)() (pid=3655, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
                 ~~^~~
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=3655, ip=127.0.0.1)
ray.exceptions.UserCodeException: UDF failed to process a data block.

(With log_internal_stack_trace=True the stdout is the same, minus the one
"Exception occurred in user code" advisory line — the flag never adds internal
frames to stdout.)

Log file (.../logs/ray-data/ray-data.log)

Before (master): the log file always contained the full driver + worker
trace (internal frames included).

After, log_internal_stack_trace=False (default): only the cleaned
worker-side trace, no internal frames:

ERROR exceptions.py:95 -- Full stack trace:
ray::Map(f)() (pid=3655, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
                 ~~^~~
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=3655, ip=127.0.0.1)
ray.exceptions.UserCodeException: UDF failed to process a data block.

After, log_internal_stack_trace=True: the full driver + worker trace is
restored to the log file (equivalent to master's old log-file content), while
stdout stays clean:

ERROR exceptions.py:91 -- Full stack trace:
Traceback (most recent call last):
  File ".../ray/data/exceptions.py", line 49, in handle_trace
  File ".../ray/data/dataset.py", line 7748, in _execute_to_iterator
  File ".../ray/data/_internal/execution/streaming_executor.py", line 767, in get_next
  File ".../ray/data/_internal/execution/streaming_executor_state.py", line 502, in get_output_blocking
  File ".../ray/data/_internal/execution/streaming_executor_state.py", line 723, in process_completed_tasks
  File ".../ray/data/_internal/execution/interfaces/physical_operator.py", line 284, in on_data_ready
  File ".../ray/_private/worker.py", line 1050, in get_objects
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(UserCodeException): ray::Map(f)() (pid=4137, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
                 ~~^~~
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=4137, ip=127.0.0.1)
ray.exceptions.UserCodeException: UDF failed to process a data block.

Related issues

Follow-up to #63023.

Additional information

Extends test_exceptions.py to assert: the "Full stack trace:" record is always
hidden from stdout (hide=True) for user-code errors; the record carries only
the cleaned inlined trace by default (exc_info is None) and the full traceback
via exc_info when log_internal_stack_trace=True; system exceptions keep
hide=False; and the deprecated log_internal_stack_trace_to_stdout alias still
forwards with a DeprecationWarning.

Follow-up to ray-project#63023.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Aarrya <[email protected]>
@AarryaSaraf
AarryaSaraf requested review from a team as code owners July 7, 2026 23:51

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves the formatting and logging of user-code exceptions in Ray Data by hiding redundant internal execution frames and preventing duplicate traceback logs. The feedback suggests extending the UserCodeException checks in both streaming_executor.py and streaming_executor_state.py to also inspect the exception's cause attribute (e.g., getattr(e, 'cause', None)), ensuring that wrapped exceptions like RayTaskError are correctly identified and their tracebacks are consistently suppressed.

@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Jul 8, 2026

@ayushk7102 ayushk7102 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Left some commients

Comment thread python/ray/exceptions.py Resolved
Comment thread python/ray/data/exceptions.py Resolved
Comment thread python/ray/exceptions.py Resolved
@AarryaSaraf
AarryaSaraf requested a review from a team as a code owner July 8, 2026 22:30
For user-code (UDF) errors, Ray Data/Ray Core internal frames are now
always hidden from stdout. The `log_internal_stack_trace` flag only
controls whether the full driver+worker trace is written to the Ray Data
log file (off by default, writing just the cleaned trace). System
exceptions still surface the full trace on stdout so users can report
Ray bugs.

Signed-off-by: Aarrya <[email protected]>
Comment thread python/ray/data/context.py Resolved
ayushk7102
ayushk7102 previously approved these changes Jul 8, 2026
@ayushk7102

Copy link
Copy Markdown
Contributor

Can we also add before and after expandable sections to the PR description? Basically to show how this changes the behaviour of the logger when errors are raised in user code

@ayushk7102
ayushk7102 self-requested a review July 8, 2026 23:59
@ayushk7102
ayushk7102 dismissed their stale review July 8, 2026 23:59

revert approval until updated with feedback

…l; trim comments

Expand the deprecation warning for the old `log_internal_stack_trace_to_stdout`
alias to include a dated removal (January 2027) and an explanation that the
behavior has changed: internal Ray Data / Ray Core stack frames are now always
omitted from stdout, and `log_internal_stack_trace` controls only whether they
are written to the Ray Data log file. Also trim verbose code comments.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Aarrya <[email protected]>
Comment thread python/ray/exceptions.py Resolved
Comment thread python/ray/data/context.py Resolved

@ayushk7102 ayushk7102 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@bveeramani
bveeramani enabled auto-merge (squash) July 9, 2026 22:04
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Jul 9, 2026
…gging

Signed-off-by: Aarrya <[email protected]>

# Conflicts:
#	python/ray/data/_internal/execution/streaming_executor_state.py
auto-merge was automatically disabled July 9, 2026 22:58

Head branch was pushed to by a user without write access

@bveeramani
bveeramani enabled auto-merge (squash) July 9, 2026 23:33
@github-actions
github-actions Bot disabled auto-merge July 10, 2026 18:17

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit f839dfe. Configure here.

"written to the Ray Data log file.",
DeprecationWarning,
)
self.log_internal_stack_trace = value

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Deprecated alias diverges from field

Medium Severity

The deprecated log_internal_stack_trace_to_stdout setter forwards to log_internal_stack_trace, but assigning log_internal_stack_trace later does not update or remove the legacy attribute stored on the instance. Reading the old name after that can return a stale value, and reading it on a fresh DataContext raises AttributeError because the field was removed from the dataclass.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f839dfe. Configure here.

Comment thread python/ray/exceptions.py Resolved
Comment thread python/ray/exceptions.py Resolved
Comment thread python/ray/exceptions.py Resolved
AarryaSaraf and others added 4 commits July 15, 2026 16:11
…ws TODO

Make RayTaskError's traceback frame-stripping library-agnostic: extract the Ray Core internal-frame markers into a constant and drive _is_hidden_internal_frame from a marker list, so callers (Ray Data via _RAY_DATA_INTERNAL_FRAME_MARKERS, other libraries in future) can hide their own internal frames on top of the always-hidden core set.

Behavior-preserving: RayTaskError.__str__ output is byte-identical before/after across UDF and non-UDF causes. Also re-adds the TODO(windows) note (dropped in an earlier refactor) flagging that frame matching assumes POSIX '/' path separators.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Aarrya <[email protected]>
@Sparks0219

Copy link
Copy Markdown
Contributor

🚢

@Sparks0219
Sparks0219 enabled auto-merge (squash) July 17, 2026 23:16
@Sparks0219
Sparks0219 merged commit bed46a1 into ray-project:master Jul 18, 2026
4 checks passed
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ay-project#64587)

## Description
When a UDF passed to a Ray Data transform raises, the error surfaced to
the
user is buried: the same `RayTaskError`/`UserCodeException` traceback is
printed
several times (driver propagation, abort log, executor shutdown log),
and each
copy is padded with Ray Data internal frames (executor / scheduler /
planner)
the user cannot act on.

This PR makes user-code errors concise while leaving genuine
internal/system
errors fully verbose. Every behavior change is gated on
`isinstance(..., UserCodeException)`:

- `RayTaskError.__str__` strips `ray/data/_internal/` frames (and their
continuation / caret lines) when the root cause is a
`UserCodeException`.
- The driver-side handler (`omit_traceback_stdout`) logs only the
cleaned
  worker-side trace for user-code errors instead of re-dumping the full
  driver→worker traceback.
- The abort log (`process_completed_tasks`) and the executor shutdown
log no
  longer re-dump the traceback for user-code errors, since it is already
  logged once (cleaned).

**Internal frames are now always omitted from `stdout`.** The renamed
`DataContext.log_internal_stack_trace` flag controls only whether the
full
internal trace is written to the **Ray Data log file** (off by default).
The
old `log_internal_stack_trace_to_stdout` name still works but is
deprecated
(removal January 2027) and forwards to the new flag with a
`DeprecationWarning`.
Internal/system errors are untouched — full tracebacks in every channel.
`RAY_DATA_RAISE_ORIGINAL_MAP_EXCEPTION=1` still bypasses the wrapping
entirely.

## Before / after

Repro — a UDF that divides by zero:

```python
import ray
def f(row):
    return {"x": 1 / 0}
ray.data.range(1).map(f).take_all()
```

### Console (stdout)

**Before (master):** the traceback is dumped **twice**, each copy padded
with
internal `map_operator` / `map_transformer` / `plan_udf_map_op` frames:

```text
ERROR streaming_executor_state.py:708 -- An exception was raised from a task of operator "Map(f)". ...
Traceback (most recent call last):
  File ".../ray/data/_internal/execution/streaming_executor_state.py", line 673, in process_completed_tasks
    bytes_read = task.on_data_ready(
  File ".../ray/data/_internal/execution/interfaces/physical_operator.py", line 276, in on_data_ready
    raise ex from None
  ... (worker.py / auto_init_hook / client_mode_hook frames) ...
ray.exceptions.RayTaskError(UserCodeException): ray::Map(f)() (pid=5427, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=5427, ip=127.0.0.1)
  File ".../ray/data/_internal/execution/operators/map_operator.py", line 855, in _map_task
  File ".../ray/data/_internal/execution/operators/map_transformer.py", line 221, in __next__
  File ".../ray/data/_internal/planner/plan_udf_map_op.py", line 470, in _wrapped_udf_map_fn
    _try_wrap_udf_exception(e)
  File ".../ray/data/_internal/planner/plan_udf_map_op.py", line 486, in _try_wrap_udf_exception
    raise UserCodeException("UDF failed to process a data block.") from e
ray.exceptions.UserCodeException: UDF failed to process a data block.
INFO streaming_executor.py:338 -- ⚠️  Dataset dataset_1_0 execution failed
ERROR exceptions.py:63 -- Exception occurred in user code ... set `DataContext.log_internal_stack_trace_to_stdout` to True.
Traceback (most recent call last):
  ... (the ENTIRE traceback above is repeated a second time) ...
ray.exceptions.UserCodeException: UDF failed to process a data block.
```

**After (identical for `log_internal_stack_trace` = False *or* True):**
printed
once, only the user's frame:

```text
ERROR exceptions.py:68 -- Exception occurred in user code, with the abbreviated stack trace below. The Ray Data internal stack frames are omitted from stdout. To also write the full stack trace to the Ray Data log file at `.../logs/ray-data`, set `DataContext.log_internal_stack_trace` to True.
Traceback (most recent call last):
  File ".../scratchpad/repro.py", line 13, in <module>
    ray.data.range(1).map(f).take_all()
  ... (a few driver frames) ...
  File ".../ray/data/exceptions.py", line 101, in handle_trace
    raise e.with_traceback(None)
ray.exceptions.RayTaskError(UserCodeException): ray::Map(f)() (pid=3655, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
                 ~~^~~
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=3655, ip=127.0.0.1)
ray.exceptions.UserCodeException: UDF failed to process a data block.
```

(With `log_internal_stack_trace=True` the stdout is the same, minus the
one
"Exception occurred in user code" advisory line — the flag never adds
internal
frames to stdout.)

### Log file (`.../logs/ray-data/ray-data.log`)

**Before (master):** the log file always contained the full driver +
worker
trace (internal frames included).

**After, `log_internal_stack_trace=False` (default):** only the cleaned
worker-side trace, no internal frames:

```text
ERROR exceptions.py:95 -- Full stack trace:
ray::Map(f)() (pid=3655, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
                 ~~^~~
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=3655, ip=127.0.0.1)
ray.exceptions.UserCodeException: UDF failed to process a data block.
```

**After, `log_internal_stack_trace=True`:** the full driver + worker
trace is
restored to the log file (equivalent to master's old log-file content),
while
stdout stays clean:

```text
ERROR exceptions.py:91 -- Full stack trace:
Traceback (most recent call last):
  File ".../ray/data/exceptions.py", line 49, in handle_trace
  File ".../ray/data/dataset.py", line 7748, in _execute_to_iterator
  File ".../ray/data/_internal/execution/streaming_executor.py", line 767, in get_next
  File ".../ray/data/_internal/execution/streaming_executor_state.py", line 502, in get_output_blocking
  File ".../ray/data/_internal/execution/streaming_executor_state.py", line 723, in process_completed_tasks
  File ".../ray/data/_internal/execution/interfaces/physical_operator.py", line 284, in on_data_ready
  File ".../ray/_private/worker.py", line 1050, in get_objects
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(UserCodeException): ray::Map(f)() (pid=4137, ip=127.0.0.1)
  File ".../scratchpad/repro.py", line 11, in f
    return {"x": 1 / 0}
                 ~~^~~
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

ray::Map(f)() (pid=4137, ip=127.0.0.1)
ray.exceptions.UserCodeException: UDF failed to process a data block.
```

## Related issues
Follow-up to ray-project#63023.

## Additional information
Extends `test_exceptions.py` to assert: the "Full stack trace:" record
is always
hidden from stdout (`hide=True`) for user-code errors; the record
carries only
the cleaned inlined trace by default (`exc_info is None`) and the full
traceback
via `exc_info` when `log_internal_stack_trace=True`; system exceptions
keep
`hide=False`; and the deprecated `log_internal_stack_trace_to_stdout`
alias still
forwards with a `DeprecationWarning`.

---------

Signed-off-by: Aarrya <[email protected]>
Signed-off-by: AarryaSaraf <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants