refactor(router): extract mid-stream decline to stay within the complexity budget

The output-ceiling branch pushed stream_with_fallbacks past the C901 limit. Both
decline paths (ineligible after content, and exhausted allowance) re-raised the
original provider error the same way, so pull that into
_raise_original_mid_stream_error. It removes the duplicated branching, drops the
function back under the complexity ceiling, and reads clearer.
This commit is contained in:
Ayush 2026-09-16 00:34:43 +05:30
parent 6f3bac769f
commit ce36fc6851

View file

@ -33,7 +33,7 @@ from collections.abc import (
)
from functools import lru_cache, partial
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Final, Literal, Optional, TypeAlias, TypeVar, Union, cast
from typing import TYPE_CHECKING, Any, Final, Literal, NoReturn, Optional, TypeAlias, TypeVar, Union, cast
import anyio
import httpx
@ -2829,9 +2829,7 @@ class Router:
# continue it (feature off, or tool/thinking/constrained output):
# surface the real error rather than restart into the same stream.
if committed and not continue_after_content:
if e.original_exception is not None:
raise e.original_exception from e
raise
self._raise_original_mid_stream_error(e)
from litellm.main import stream_chunk_builder
@ -2864,9 +2862,7 @@ class Router:
if reduced_ceilings is None:
# The caller's output allowance is already spent; surface the
# error rather than grant a fresh allowance on this fallback hop.
if e.original_exception is not None:
raise e.original_exception from e
raise
self._raise_original_mid_stream_error(e)
initial_kwargs.update(reduced_ceilings)
initial_kwargs["messages"] = self._build_completion_continuation_input(
messages, e.generated_content
@ -3063,6 +3059,14 @@ class Router:
total_tokens=(partial_usage.total_tokens or 0) + (fb.total_tokens or 0),
)
@staticmethod
def _raise_original_mid_stream_error(e: "MidStreamFallbackError") -> NoReturn:
"""Decline a continuation by surfacing the real provider error the stream
wrapper carried, rather than leaking the internal MidStreamFallbackError."""
if e.original_exception is not None:
raise e.original_exception from e
raise e
def _mid_stream_continuation_eligible(
self,
e: "MidStreamFallbackError",