Repository navigation
Expand file tree
/
Copy pathproxy_errors.py
More file actions
467 lines (391 loc) · 17.4 KB
/
Copy pathproxy_errors.py
File metadata and controls
467 lines (391 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""Typed Airlock rate-limit error + a FastAPI exception handler (workstream B).
When Airlock's circuit breaker blocks a request pre-flight it raises
:class:`AirlockProviderBlocked` (a ``RateLimitError`` subclass) so the client
receives an HTTP 429 with a ``Retry-After`` header and an enriched but
OpenAI-compatible body — distinguishable from a passthrough provider 429 without
string-parsing. A local Fast Guardian threat backoff uses the separately typed
:class:`AirlockThreatBackoff` contract. Handlers are registered on the LiteLLM
proxy app via
:func:`install_airlock_error_handlers_on_proxy_app`, mirroring the other
``install_*_on_proxy_app`` hooks in ``model_override_headers``.
"""
from __future__ import annotations
import math
import re
from typing import Any
from litellm import RateLimitError
from litellm.proxy._types import ProxyException
from airlock.litellm_adapter import resolve_proxy_app
from airlock.transparency import model_suggestion_header
# Redact key-like tokens (provider keys, bearer tokens, long secret-ish strings)
# before any upstream reason text is echoed back to the client.
_SECRET_RE = re.compile(
r"sk-[A-Za-z0-9_\-]{6,}"
r"|Bearer\s+[A-Za-z0-9._\-]{6,}"
r"|[A-Za-z0-9_\-]{32,}"
)
def sanitize_reason(reason: str | None, limit: int = 300) -> str:
"""Defensively redact key-like tokens and bound the length of reason text.
Applied at the boundary where untrusted upstream text enters an
Airlock-emitted error, so neither the top-level ``error.message`` nor the
``error.airlock.reason`` field can leak a secret or bloat the response.
"""
text = _SECRET_RE.sub("[REDACTED]", str(reason or ""))
return text[:limit]
class AirlockProviderBlocked(RateLimitError):
"""An Airlock circuit-breaker block (not a passthrough provider 429).
Subclasses ``RateLimitError`` so existing ``except RateLimitError`` paths keep
working, while carrying the structured fields the handler needs.
"""
def __init__(
self,
message: str,
*,
llm_provider: str,
model: str,
cooldown_seconds: float,
scope: str,
reason: str,
client_id: str,
) -> None:
super().__init__(message=message, llm_provider=llm_provider, model=model)
self.cooldown_seconds = float(cooldown_seconds)
self.scope = scope
self.reason = reason
self.client_id = client_id
class AirlockModelNotFound(Exception):
"""A refused near-match, rendered as an OpenAI-shaped 404."""
def __init__(
self,
requested_model: str,
suggestions: list[dict[str, str | float]],
*,
reason: str = "dropped_qualifier",
) -> None:
self.requested_model = requested_model
self.suggestions = suggestions
self.reason = reason
models = [str(item["model"]) for item in suggestions if item.get("model")]
message = (
f"Model '{requested_model}' is not available. "
f"Try '{models[0]}'. Alternatives: {', '.join(models[1:]) or 'none'}."
)
super().__init__(message)
class AirlockInvalidReasoningEffort(Exception):
"""A known-invalid OpenAI reasoning effort rejected before provider dispatch."""
def __init__(self, requested: str, model: str, supported: frozenset[str]) -> None:
self.requested = requested
self.model = model
self.supported = tuple(sorted(supported))
options = ", ".join(self.supported)
super().__init__(
f"reasoning_effort {requested!r} is not supported by {model!r}; "
f"use one of: {options}."
)
class AirlockEndpointNotSupported(ProxyException):
"""A configured alias cannot be used on the requested API endpoint."""
def __init__(self, model: str, endpoint: str) -> None:
self.model = model
self.endpoint = endpoint
self._airlock_error_code = "model_endpoint_not_supported"
super().__init__(
message=f"Model {model!r} is not configured for the {endpoint!r} endpoint.",
type="invalid_request_error",
param="model",
code=400,
openai_code=self._airlock_error_code,
)
def to_dict(self) -> dict:
body = super().to_dict()
body["code"] = self._airlock_error_code
body["airlock"] = {"endpoint": self.endpoint, "model": self.model}
return body
class AirlockGatewayRoutingOverride(ProxyException):
"""A client tried to override an operator-owned gateway routing policy."""
def __init__(self, option: str) -> None:
self.option = option
self._airlock_error_code = "gateway_routing_override_not_allowed"
super().__init__(
message=(
f"OpenRouter routing option {option!r} is operator-controlled and "
"cannot be set by a client request."
),
type="invalid_request_error",
param=option,
code=400,
openai_code=self._airlock_error_code,
)
def to_dict(self) -> dict:
body = super().to_dict()
body["code"] = self._airlock_error_code
return body
class AirlockDeepSeekToolNotSupported(ProxyException):
"""A DeepSeek request contains a non-function tool LiteLLM would drop."""
def __init__(self, option: str = "tools") -> None:
self.option = option
self._airlock_error_code = "deepseek_non_function_tool_not_supported"
super().__init__(
message="DeepSeek supports OpenAI function tools only.",
type="invalid_request_error",
param=option,
code=400,
openai_code=self._airlock_error_code,
)
def to_dict(self) -> dict:
body = super().to_dict()
body["code"] = self._airlock_error_code
return body
class AirlockAdmissionShed(RateLimitError):
"""A local admission-gate rejection with an honest retry time."""
def __init__(self, message: str, *, retry_after: float) -> None:
super().__init__(message=message, llm_provider="airlock", model="admission")
self.retry_after = float(retry_after)
class AirlockThreatBackoff(ProxyException, RateLimitError): # type: ignore[misc]
"""A local Fast Guardian threat backoff, not a provider rate limit.
Retains only the remaining duration needed to tell the client when to retry;
client identity, threat signals, and triggering request details stay inside
the Guardian/logging boundary.
LiteLLM's proxy path requires ``ProxyException`` while Airlock's Guardian
contract requires ``RateLimitError``. Those independently maintained base
classes annotate their overlapping protocol fields (``code``, ``headers``,
and ``type``) incompatibly, although this adapter explicitly normalizes
every one below before it crosses either boundary. Keep the suppression on
this intentional compatibility join only; do not broaden it to the module.
"""
def __init__(self, *, retry_after: float) -> None:
retry_after_seconds_value = max(1, math.ceil(float(retry_after)))
# RateLimitError preserves existing rate-limit catches and telemetry.
# Calling it explicitly matters because ProxyException is first in the
# MRO so LiteLLM's guardrail pipeline preserves our stable body schema.
RateLimitError.__init__(
self,
message="Too many requests. Please retry later.",
llm_provider="airlock",
model="threat_backoff",
headers={"Retry-After": str(retry_after_seconds_value)},
)
# Do not call ProxyException.__init__: with this intentional multiple
# inheritance its cooperative ``super()`` would re-enter
# RateLimitError. Mirror its small public protocol after RateLimitError
# has initialized the OpenAI exception base.
self.message = "Too many requests. Please retry later."
self.type = "airlock_threat_backoff"
self.param = None
self.openai_code = "threat_backoff"
self.code = "429"
self.headers = {"Retry-After": str(retry_after_seconds_value)}
self.provider_specific_fields = {
"airlock": {
"source": "threat_backoff",
"retry_after": retry_after_seconds_value,
}
}
self.retry_after = float(retry_after)
def to_dict(self) -> dict:
"""Use LiteLLM's direct guardrail path without nesting Airlock fields."""
return {
"message": self.message,
"type": self.type,
"param": self.param,
"code": self.openai_code,
"airlock": {
"source": "threat_backoff",
"retry_after": retry_after_seconds(self.retry_after),
},
}
def retry_after_seconds(cooldown_seconds: float) -> int:
"""Whole-second ``Retry-After`` value, at least 1."""
return max(1, math.ceil(cooldown_seconds))
def block_response_payload(exc: AirlockProviderBlocked) -> tuple[dict, dict]:
"""Build the (body, headers) for an Airlock block. OpenAI-shaped, enriched."""
retry_after = retry_after_seconds(exc.cooldown_seconds)
body = {
"error": {
# Defensive: the message is Airlock-built, but sanitize again in case a
# caller embedded raw upstream text in it.
"message": sanitize_reason(str(getattr(exc, "message", "") or exc), 500),
"type": "airlock_circuit_breaker",
"code": "provider_blocked",
"param": None,
"airlock": {
"scope": exc.scope,
"provider": exc.llm_provider,
"cooldown_seconds": round(exc.cooldown_seconds, 1),
"retry_after": retry_after,
"reason": sanitize_reason(exc.reason),
"source": "circuit_breaker",
},
}
}
headers = {
"Retry-After": str(retry_after),
"X-Airlock-Provider-State": "quarantined",
"X-Airlock-Block-Scope": exc.scope,
}
return body, headers
def model_not_found_response_payload(exc: AirlockModelNotFound) -> tuple[dict, dict]:
"""Build an OpenAI-compatible 404 for a refused near-match."""
body = {
"error": {
"message": sanitize_reason(str(exc), 500),
"type": "invalid_request_error",
"code": "model_not_found",
"param": "model",
"airlock": {"suggestions": exc.suggestions},
}
}
header = model_suggestion_header(
exc.requested_model, exc.suggestions, reason=exc.reason
)
return body, {"X-Airlock-Model-Suggestion": header} if header else {}
def admission_shed_response_payload(exc: AirlockAdmissionShed) -> tuple[dict, dict]:
"""Build the OpenAI-compatible response for local admission shedding."""
retry_after = retry_after_seconds(exc.retry_after)
body = {
"error": {
"message": sanitize_reason(str(exc), 500),
"type": "airlock_admission",
"code": "admission_shed",
"param": None,
"airlock": {
"source": "admission",
"retry_after": retry_after,
},
}
}
headers = {
"Retry-After": str(retry_after),
"X-Airlock-Admission": f"shed; retry_after={retry_after}",
}
return body, headers
def threat_backoff_response_payload(
exc: AirlockThreatBackoff,
) -> tuple[dict, dict]:
"""Build the non-disclosing response for a local threat backoff."""
retry_after = retry_after_seconds(exc.retry_after)
body = {
"error": {
"message": "Too many requests. Please retry later.",
"type": "airlock_threat_backoff",
"code": "threat_backoff",
"param": None,
"airlock": {
"source": "threat_backoff",
"retry_after": retry_after,
},
}
}
return body, {"Retry-After": str(retry_after)}
async def airlock_provider_blocked_handler(request: Any, exc: Exception):
"""FastAPI exception handler → 429 with Retry-After + enriched body."""
from fastapi.responses import JSONResponse
assert isinstance(exc, AirlockProviderBlocked)
body, headers = block_response_payload(exc)
return JSONResponse(status_code=429, content=body, headers=headers)
async def airlock_model_not_found_handler(request: Any, exc: Exception):
"""FastAPI exception handler → an enriched model-not-found response."""
from fastapi.responses import JSONResponse
assert isinstance(exc, AirlockModelNotFound)
body, headers = model_not_found_response_payload(exc)
return JSONResponse(status_code=404, content=body, headers=headers)
def invalid_reasoning_effort_response_payload(
exc: AirlockInvalidReasoningEffort,
) -> tuple[dict, dict]:
"""Build the stable OpenAI-shaped P-2 validation response."""
body = {
"error": {
"message": sanitize_reason(str(exc), 500),
"type": "invalid_request_error",
"code": "invalid_reasoning_effort",
"param": "reasoning_effort",
"airlock": {
"requested": exc.requested,
"model": exc.model,
"supported": list(exc.supported),
},
}
}
return body, {}
def endpoint_not_supported_response_payload(
exc: AirlockEndpointNotSupported,
) -> tuple[dict, dict]:
"""Build an OpenAI-compatible 400 for an unsupported model endpoint."""
return (
{
"error": {
"message": sanitize_reason(str(exc), 500),
"type": "invalid_request_error",
"code": "model_endpoint_not_supported",
"param": "model",
"airlock": {"endpoint": exc.endpoint, "model": exc.model},
}
},
{},
)
async def airlock_invalid_reasoning_effort_handler(request: Any, exc: Exception):
"""FastAPI exception handler → an OpenAI-compatible 400."""
from fastapi.responses import JSONResponse
assert isinstance(exc, AirlockInvalidReasoningEffort)
body, headers = invalid_reasoning_effort_response_payload(exc)
return JSONResponse(status_code=400, content=body, headers=headers)
async def airlock_endpoint_not_supported_handler(request: Any, exc: Exception):
"""FastAPI exception handler → an OpenAI-compatible 400."""
from fastapi.responses import JSONResponse
assert isinstance(exc, AirlockEndpointNotSupported)
body, headers = endpoint_not_supported_response_payload(exc)
return JSONResponse(status_code=400, content=body, headers=headers)
async def airlock_admission_shed_handler(request: Any, exc: Exception):
"""FastAPI exception handler → local admission 429 with Retry-After."""
from fastapi.responses import JSONResponse
assert isinstance(exc, AirlockAdmissionShed)
body, headers = admission_shed_response_payload(exc)
return JSONResponse(status_code=429, content=body, headers=headers)
async def airlock_threat_backoff_handler(request: Any, exc: Exception):
"""FastAPI exception handler → a local-threat 429 with Retry-After."""
from fastapi.responses import JSONResponse
assert isinstance(exc, AirlockThreatBackoff)
body, headers = threat_backoff_response_payload(exc)
return JSONResponse(status_code=429, content=body, headers=headers)
def install_airlock_error_handlers_on_proxy_app() -> bool:
"""Register Airlock-owned typed error handlers on the LiteLLM proxy app.
Registered for the subclass specifically (not the base ``RateLimitError``) so
passthrough provider 429s keep LiteLLM's own handling — the perimeter only
shapes Airlock's own breaker blocks. Idempotent via ``app.state``.
"""
try:
from fastapi import FastAPI
except ImportError:
return False
app = resolve_proxy_app()
if not isinstance(app, FastAPI):
return False
if not getattr(app.state, "airlock_provider_blocked_handler_installed", False):
app.add_exception_handler(
AirlockProviderBlocked, airlock_provider_blocked_handler
)
app.state.airlock_provider_blocked_handler_installed = True
if not getattr(app.state, "airlock_model_not_found_handler_installed", False):
app.add_exception_handler(AirlockModelNotFound, airlock_model_not_found_handler)
app.state.airlock_model_not_found_handler_installed = True
if not getattr(
app.state, "airlock_invalid_reasoning_effort_handler_installed", False
):
app.add_exception_handler(
AirlockInvalidReasoningEffort, airlock_invalid_reasoning_effort_handler
)
app.state.airlock_invalid_reasoning_effort_handler_installed = True
if not getattr(
app.state, "airlock_endpoint_not_supported_handler_installed", False
):
app.add_exception_handler(
AirlockEndpointNotSupported, airlock_endpoint_not_supported_handler
)
app.state.airlock_endpoint_not_supported_handler_installed = True
if not getattr(app.state, "airlock_admission_shed_handler_installed", False):
app.add_exception_handler(AirlockAdmissionShed, airlock_admission_shed_handler)
app.state.airlock_admission_shed_handler_installed = True
if not getattr(app.state, "airlock_threat_backoff_handler_installed", False):
app.add_exception_handler(AirlockThreatBackoff, airlock_threat_backoff_handler)
app.state.airlock_threat_backoff_handler_installed = True
app.state.airlock_error_handlers_installed = True
return True