(
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,
)
| 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] = [] |
no test coverage detected