fix: keep Opper inside the lint and exception-mapping budgets

Upstream grew a status-keyed fallback for providers with no dedicated
exception handler, and pinned it with a table test. Opper was borrowing
OpenRouter's handler, which has no 403 or 500 branch, so both fell through
to a bare APIError. Dropping Opper from that handler is both what the table
expects and the better mapping: 403 is now PermissionDeniedError, 500
InternalServerError and 504 Timeout. Real transport failures already carry
their own request, so the connection-error test asserts that instead of the
stubbed endpoint the handler used to supply.

The type-discipline and basedpyright budgets have since ratcheted to zero
slack, so three rebind-ok comments that sat above their statement now have
to be on it, the transform_response encoding parameter takes the base
signature's tiktoken type rather than object, and the two remaining
diagnostics carry a reasoned pyright ignore.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KBqE4QB51dp3bdZYx5p8r5
This commit is contained in:
Jose Sabater 2026-09-03 09:52:32 +02:00
parent c687f9ffcf
commit 30648f2d2e
6 changed files with 23 additions and 22 deletions

View file

@ -2195,15 +2195,12 @@ def _map_openrouter_exception(
)
else:
# if no status code then it is an APIConnectionError: https://github.com/openai/openai-python#handling-errors
fallback_api_base: Final = (
"https://api.opper.ai/v3/compat" if custom_llm_provider == "opper" else "https://api.openai.com/v1/"
)
raise APIConnectionError(
message=f"APIConnectionError: {exception_provider} - {error_str}",
llm_provider=custom_llm_provider,
model=model,
litellm_debug_info=extra_information,
request=httpx.Request(method="POST", url=fallback_api_base),
request=httpx.Request(method="POST", url="https://api.openai.com/v1/"),
)
@ -2612,7 +2609,7 @@ def exception_type(
exception_provider=exception_provider,
extra_information=extra_information,
)
if custom_llm_provider in ("openrouter", "opper"):
if custom_llm_provider == "openrouter":
_map_openrouter_exception(
model=model,
original_exception=mappable_exception,

View file

@ -703,11 +703,9 @@ def _get_openai_compatible_provider_info(
dynamic_api_key = api_key or get_secret_str("TENCENT_API_KEY")
elif custom_llm_provider == "opper":
# rebind-ok: provider chain assigns api_base per branch
api_base = api_base or get_secret("OPPER_API_BASE") or OPPER_API_BASE
api_base = api_base or get_secret("OPPER_API_BASE") or OPPER_API_BASE # rebind-ok: set per provider branch
# rebind-ok: provider chain assigns dynamic_api_key per branch
dynamic_api_key = api_key or get_secret_str("OPPER_API_KEY")
dynamic_api_key = api_key or get_secret_str("OPPER_API_KEY") # rebind-ok: set per provider branch
elif custom_llm_provider == "fireworks_ai":
# fireworks is openai compatible, we just need to set this to custom_openai and have the api_base be https://api.fireworks.ai/inference/v1
(

View file

@ -3,7 +3,7 @@ Translate from OpenAI's `/v1/chat/completions` to the Opper AI gateway's `/v3/co
"""
from collections.abc import AsyncIterator, Iterator
from typing import Final
from typing import TYPE_CHECKING, Final
import httpx
@ -21,6 +21,9 @@ from ...openai.chat.gpt_transformation import (
)
from ..common_utils import OpperException
if TYPE_CHECKING:
import tiktoken
class OpperConfig(OpenAIGPTConfig):
@property
@ -65,7 +68,7 @@ class OpperConfig(OpenAIGPTConfig):
messages: list[AllMessageValues], # mutable-ok: mirrors the OpenAIGPTConfig signature
optional_params: dict, # mutable-ok: mirrors the OpenAIGPTConfig signature
litellm_params: dict, # mutable-ok: mirrors the OpenAIGPTConfig signature
encoding: object,
encoding: "tiktoken.Encoding | None",
api_key: str | None = None,
json_mode: bool | None = None,
) -> ModelResponse:
@ -88,7 +91,7 @@ class OpperConfig(OpenAIGPTConfig):
if response_json.get("usage"):
response_cost: Final = response_json["usage"].get("cost")
if response_cost is not None:
headers: Final = transformed._hidden_params.setdefault(
headers: Final = transformed._hidden_params.setdefault( # pyright: ignore[reportPrivateUsage] # the cost hand-off channel every provider writes
"additional_headers",
{}, # mutable-ok: _hidden_params carries plain dicts
)

View file

@ -3499,7 +3499,7 @@ def _complete_opper(ctx: _CompletionDispatchContext) -> _CompletionDispatchResul
litellm_params=litellm_params,
shared_session=shared_session,
custom_llm_provider="opper",
timeout=timeout,
timeout=timeout, # pyright: ignore[reportArgumentType] # ctx types timeout wider than the handler
headers=headers,
encoding=_get_encoding(),
api_key=api_key,

View file

@ -4700,13 +4700,11 @@ def get_optional_params(
drop_params=bool(drop_params),
)
elif custom_llm_provider == "opper":
optional_params = (
litellm.OpperConfig().map_openai_params( # rebind-ok: provider chain assigns optional_params per branch
non_default_params=non_default_params,
optional_params=optional_params,
model=model,
drop_params=(drop_params if drop_params is not None and isinstance(drop_params, bool) else False),
)
optional_params = litellm.OpperConfig().map_openai_params( # rebind-ok: set per provider branch
non_default_params=non_default_params,
optional_params=optional_params,
model=model,
drop_params=(drop_params if drop_params is not None and isinstance(drop_params, bool) else False),
)
elif custom_llm_provider == "watsonx":
optional_params = litellm.IBMWatsonXChatConfig().map_openai_params(

View file

@ -303,14 +303,19 @@ def test_opper_preserves_cache_control_in_content():
def test_opper_connection_error_reports_opper_endpoint():
"""Status-less failures carry Opper request metadata, not the OpenAI default."""
"""Status-less failures carry the Opper request they came from, not the OpenAI default."""
import httpx
import litellm
from litellm.litellm_core_utils.exception_mapping_utils import exception_type
transport_failure = Exception("connection reset")
transport_failure.request = httpx.Request("POST", f"{OPPER_API_BASE}/chat/completions")
with pytest.raises(litellm.APIConnectionError) as excinfo:
exception_type(
model="anthropic/claude-haiku-4-5",
original_exception=Exception("connection reset"),
original_exception=transport_failure,
custom_llm_provider="opper",
)