update
This commit is contained in:
@@ -78,6 +78,27 @@ def _gateway_failure(reason, started, *, retryable=False):
|
||||
return ModelUnavailable(reason, retryable=retryable)
|
||||
|
||||
|
||||
def _http_failure(response, prefix, started):
|
||||
# Inspect only for classification. Never log upstream bodies: they may
|
||||
# contain business material. A context rejection is not a service outage.
|
||||
reason = f'{prefix}_http_{response.status_code}'
|
||||
if response.status_code in {400, 413, 422}:
|
||||
if response.is_stream_consumed:
|
||||
body = response.text[:16384].lower()
|
||||
else:
|
||||
chunks = bytearray()
|
||||
for chunk in response.iter_bytes(chunk_size=4096):
|
||||
chunks.extend(chunk)
|
||||
if len(chunks) >= 16384:
|
||||
break
|
||||
body = bytes(chunks[:16384]).decode('utf-8', 'replace').lower()
|
||||
if any(marker in body for marker in ('maximum context length', 'context_length_exceeded',
|
||||
'input_tokens', 'max context', 'prompt is too long')):
|
||||
reason = 'model_context_full'
|
||||
return _gateway_failure(reason, started,
|
||||
retryable=response.status_code in {408, 429, 500, 502, 503, 504})
|
||||
|
||||
|
||||
class PlanningModel:
|
||||
"""One transient recovery across all planning stages, within a shared deadline."""
|
||||
def __init__(self, invoke, budget_seconds):
|
||||
@@ -159,8 +180,7 @@ def _stream_json_response(client, url, request, headers, timeout_seconds, observ
|
||||
logger.warning('bid_model_connect_retry phase=%s elapsed_ms=%d',
|
||||
type(exc).__name__, (time.perf_counter() - started) * 1000)
|
||||
if response.is_error:
|
||||
raise _gateway_failure(f'direct_http_{response.status_code}', started,
|
||||
retryable=response.status_code in {408,429,500,502,503,504})
|
||||
raise _http_failure(response, 'direct', started)
|
||||
for line in response.iter_lines():
|
||||
if progress is not None:
|
||||
progress.check_active()
|
||||
@@ -337,8 +357,7 @@ def _complete_json(instruction: str, content: dict, schema: dict | None = None,
|
||||
if response.is_error:
|
||||
if '"Arrearage"' in response.text[:16384].replace('\\"', '"'):
|
||||
raise _gateway_failure('model_account_unavailable', started)
|
||||
raise _gateway_failure(f'gateway_http_{response.status_code}', started,
|
||||
retryable=response.status_code in {408,429,500,502,503,504})
|
||||
raise _http_failure(response, 'gateway', started)
|
||||
if len(response.content) > 1_000_000:
|
||||
raise _gateway_failure('gateway_response_too_large', started)
|
||||
envelope = response.json()
|
||||
@@ -394,8 +413,7 @@ def _complete_json(instruction: str, content: dict, schema: dict | None = None,
|
||||
else:
|
||||
response = _post_json(_model_client(), url, request, headers, timeout_seconds)
|
||||
if response.is_error:
|
||||
raise _gateway_failure(f'direct_http_{response.status_code}', started,
|
||||
retryable=response.status_code in {408,429,500,502,503,504})
|
||||
raise _http_failure(response, 'direct', started)
|
||||
if len(response.content) > 1_000_000:
|
||||
raise _gateway_failure('direct_response_too_large', started)
|
||||
data = response.json()
|
||||
|
||||
Reference in New Issue
Block a user