MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / _select_stream_response

Function _select_stream_response

uncommon_route/proxy.py:5401–5462  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5399 return await client.send(req, stream=True)
5400
5401 async def _select_stream_response() -> tuple[httpx.Response | None, Response | None]:
5402 fallback_source_model: str | None = None
5403 attempt_queue = [attempt, *(_prepare_attempt(model_name) for model_name in fallback_models)]
5404
5405 for index, attempt_payload in enumerate(attempt_queue):
5406 if index > 0:
5407 spend_error = await _spend_error_for_model(attempt_payload["selected_model"])
5408 if spend_error is not None:
5409 return None, spend_error
5410
5411 _begin_attempt_trace(
5412 attempt_payload,
5413 fallback_from=fallback_source_model if index > 0 else None,
5414 )
5415 resp = await _open_stream_attempt(attempt_payload)
5416 if resp.status_code < 400:
5417 _complete_attempt_trace(status_code=resp.status_code, success=True)
5418 if index > 0 and fallback_source_model is not None:
5419 _apply_attempt(attempt_payload, fallback_from=fallback_source_model)
5420 return resp, None
5421
5422 content = await resp.aread()
5423 content_type = resp.headers.get("content-type", "application/json")
5424 await resp.aclose()
5425 attempt_error_code, attempt_error_message = _extract_error_details(resp.status_code, content)
5426 _complete_attempt_trace(
5427 status_code=resp.status_code,
5428 error_code=attempt_error_code,
5429 error_message=attempt_error_message,
5430 )
5431 if _should_try_fallback(resp.status_code, content):
5432 if fallback_source_model is None:
5433 fallback_source_model = str(
5434 attempt_payload["transport_body"].get("model")
5435 or attempt_payload["resolved_model"]
5436 )
5437 if index > 0:
5438 _apply_attempt(
5439 attempt_payload,
5440 fallback_from=fallback_source_model,
5441 record_successful_fallback=False,
5442 )
5443 continue
5444 if index > 0 and fallback_source_model is not None:
5445 _apply_attempt(
5446 attempt_payload,
5447 fallback_from=fallback_source_model,
5448 record_successful_fallback=False,
5449 )
5450 return None, _build_proxy_response(
5451 status_code=resp.status_code,
5452 content=content,
5453 content_type=content_type,
5454 native_transport=attempt_payload["native_anthropic_transport"],
5455 )
5456
5457 return None, _build_proxy_response(
5458 status_code=502,

Callers 1

_handle_chat_coreFunction · 0.85

Calls 10

_prepare_attemptFunction · 0.85
_spend_error_for_modelFunction · 0.85
_begin_attempt_traceFunction · 0.85
_open_stream_attemptFunction · 0.85
_complete_attempt_traceFunction · 0.85
_apply_attemptFunction · 0.85
_extract_error_detailsFunction · 0.85
_should_try_fallbackFunction · 0.85
_build_proxy_responseFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected