Skip to content

Do not post_process before finally in ModelOutputThunk.astream #577

Closed

Description

@nrfulton

#355 introduces a finally block that does mot post-processing prior to propagating exceptions. The motivation is to log failures.

Problem: the post-process functions in backends make heavy implicit assumptions about system invariants. It is in fact extremely rare that post-process will succeed when there was an exception thrown during generation. As a result, actual exceptions thrown during generation are swallowed silently and obscure key errors are thrown instead.

Solution: there is none; if an exception is encountered during generation, there is no "safe" way of getting a partial post-process done because we do not know at that point what might've gone wrong. The finally block should be removed and exceptions should be allowed to be raised during generation.

Activity

  1. added
    bug
    on Mar 4, 2026
  2. psschwei commented on Mar 5, 2026

    @psschwei
    Contributor

    finally block in question:

    mellea/mellea/core/base.py

    Lines 338 to 362 in 4cc75c8

    finally:
    # Always call post_process if computed, even on exception
    # This ensures telemetry spans are properly closed
    if self._computed:
    assert self._post_process is not None
    await self._post_process(self)
    # Only parse if no exception occurred
    if exception_to_raise is None:
    match self._action:
    case Component():
    self.parsed_repr = self._action._parse(self)
    case CBlock():
    assert self.value is not None, (
    "value must be non-None since this thunk is computed"
    )
    self.parsed_repr = self.value # type: ignore
    case _:
    raise ValueError(
    "attempted to astream from a model output thunk with no ._action set"
    )
    assert self.parsed_repr is not None, (
    "enforce constraint that a computed ModelOutputThunk has a non-None parsed_repr"
    )
    return self._underlying_value # type: ignore

  3. psschwei commented on Mar 6, 2026

    @psschwei
    Contributor

    Fixed in #580

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions