Skip to content

fix(retry): let IncompleteOutputException propagate without wrapping - #2280

Merged
jxnl-oai merged 1 commit into
567-labs:mainfrom
RudrenduPaul:fix/incomplete-output-exception-unwrap
May 10, 2026
Merged

jxnl-oai merged 1 commit into
567-labs:mainfrom
RudrenduPaul:fix/incomplete-output-exception-unwrap

Conversation

@RudrenduPaul

Copy link
Copy Markdown
Contributor

Motivation and Context

Closes #2273

When an LLM returns a truncated response (e.g. finish_reason=length), instructor raises IncompleteOutputException. The documented usage pattern is:

try:
    result = client.chat.completions.create(...)
except IncompleteOutputException:
    # handle truncation

However, this catch block never fires. IncompleteOutputException falls into the generic except Exception handler inside the tenacity retry loop, and after retries are exhausted it gets wrapped in InstructorRetryException. Users must catch InstructorRetryException and inspect its .last_attempt to find the original exception — an undocumented workaround that defeats the purpose of a named exception type.

What changes

In both retry_sync and retry_async:

  1. Detect IncompleteOutputException in the except Exception handler before tenacity's retry logic runs
  2. Store it and break out of the tenacity loop cleanly (tenacity never sees the exception)
  3. Re-raise it after the loop — directly, without wrapping

The change is minimal: 4 new lines in the exception handler of each function, 2 variable initializations, 2 post-loop raises.

Tests

Added tests/test_incomplete_output_exception.py with 4 tests:

  • Sync path: exception is catchable directly
  • Sync path: exception is not wrapped in InstructorRetryException
  • Sync path: with max_retries=0, API called exactly once
  • Async path: exception is catchable directly

All existing retry and hooks tests continue to pass.

Previously IncompleteOutputException was caught by the generic
`except Exception` handler inside the tenacity retry loop and
eventually wrapped in InstructorRetryException, making direct
`except IncompleteOutputException` catch blocks impossible.

Fix: detect IncompleteOutputException in both retry_sync and
retry_async before tenacity can wrap it — store it and break out
of the tenacity loop cleanly, then re-raise after the loop.

Adds 4 regression tests covering sync/async and max_retries=0/N.

Fixes 567-labs#2273

Built by Rudrendu Paul, developed with Claude Code
@jxnl-oai
jxnl-oai merged commit 5e8e2d5 into 567-labs:main May 10, 2026
@Mr-Ruben

Mr-Ruben commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

The issue still persists for ValidationError.
Look last comment on #2273

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Catching IncompleteOutputException : not possible as presently documented / tested.

3 participants