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

Function _handle_chat_core

uncommon_route/proxy.py:3910–5812  ·  view source on GitHub ↗
(
        body: dict,
        request: Request,
        *,
        api_format: str = "openai",
        endpoint_name: str = "chat_completions",
        source_body: dict[str, Any] | None = None,
        source_preview_body: dict[str, Any] | None = None,
    )

Source from the content-addressed store, hash-verified

3908 return JSONResponse(result)
3909
3910 async def _handle_chat_core(
3911 body: dict,
3912 request: Request,
3913 *,
3914 api_format: str = "openai",
3915 endpoint_name: str = "chat_completions",
3916 source_body: dict[str, Any] | None = None,
3917 source_preview_body: dict[str, Any] | None = None,
3918 ) -> Response:
3919 if not upstream:
3920 msg = _SETUP_GUIDE.strip()
3921 if api_format == "anthropic":
3922 return JSONResponse(anthropic_error_response(503, msg), status_code=503)
3923 return JSONResponse(
3924 {"error": {"message": msg, "type": "configuration_error"}},
3925 status_code=503,
3926 )
3927 upstream_chat = _upstream_chat_url(upstream)
3928
3929 model = (body.get("model") or "").strip().lower()
3930 is_streaming = body.get("stream", False)
3931 response_model = str(body.pop("_client_requested_model", "") or model).strip()
3932 recursion_guarded = _recursion_guard_enabled(request)
3933
3934 if not model:
3935 default_mode = _routing_store.default_mode()
3936 model = VIRTUAL_MODEL_IDS[default_mode]
3937 body["model"] = model
3938
3939 requested_model = model
3940 routing_mode = routing_mode_from_model(model)
3941 if recursion_guarded and routing_mode is not None:
3942 msg = "Virtual UncommonRoute models cannot be routed recursively"
3943 if api_format == "anthropic":
3944 return JSONResponse(anthropic_error_response(400, msg), status_code=400)
3945 return JSONResponse(
3946 {"error": {"message": msg, "type": "invalid_request_error"}},
3947 status_code=400,
3948 )
3949 is_virtual = routing_mode is not None
3950 route_start = time.perf_counter_ns()
3951 route_method: str = "pool"
3952 confidence = 0.0
3953 savings = 0.0
3954 estimated_cost = 0.0
3955 baseline_cost = 0.0
3956 selected_model = model
3957 tier_value = ""
3958 reasoning = "passthrough"
3959 route_reasoning = ""
3960 raw_confidence = 0.0
3961 confidence_source = ""
3962 calibration_version = ""
3963 calibration_sample_count = 0
3964 calibration_temperature = 1.0
3965 calibration_applied_tags: list[str] = []
3966 feature_tags: list[str] = []
3967 constraint_tags: list[str] = []

Callers 3

handle_chat_completionsFunction · 0.85
handle_messagesFunction · 0.85
handle_responsesFunction · 0.85

Calls 15

anthropic_error_responseFunction · 0.90
routing_mode_from_modelFunction · 0.90
get_busFunction · 0.90
estimate_output_budgetFunction · 0.90
estimate_tokensFunction · 0.90
request_capability_laneFunction · 0.90
model_served_qualityFunction · 0.90
RoutingDecisionClass · 0.90
FallbackOptionClass · 0.90
routeFunction · 0.90
RouteRecordClass · 0.90

Tested by

no test coverage detected