(self, request_data: dict)
| 78 | return payload |
| 79 | |
| 80 | async def handle_request(self, request_data: dict): |
| 81 | if not self.api_key: |
| 82 | raise ConfigError("GEMINI_API_KEY is not configured on the server.") |
| 83 | |
| 84 | model_name = request_data.get("model") |
| 85 | if not model_name: |
| 86 | raise ValueError("Request data must include a 'model' field.") |
| 87 | |
| 88 | target_model = self.model_map.get(model_name, {}).get("model_id", model_name) |
| 89 | payload = self._build_payload(request_data, target_model) |
| 90 | |
| 91 | if request_data.get("stream", False): |
| 92 | return StreamingResponse( |
| 93 | self._stream_response(payload), |
| 94 | media_type="text/event-stream", |
| 95 | ) |
| 96 | |
| 97 | try: |
| 98 | client = get_http_client() |
| 99 | response = await client.post(GEMINI_OPENAI_COMPAT_URL, headers=self._headers(), json=payload) |
| 100 | response.raise_for_status() |
| 101 | data = response.json() |
| 102 | for choice in data.get("choices", []): |
| 103 | msg = choice.get("message", {}) |
| 104 | if "reasoning_content" in msg: |
| 105 | msg["reasoning"] = msg.pop("reasoning_content") |
| 106 | return data |
| 107 | except httpx.RequestError as exc: |
| 108 | logger.error("Gemini API request failed: %s", exc) |
| 109 | raise BackendAPIError(f"Could not connect to Gemini API: {exc}", status_code=503) from exc |
| 110 | except httpx.HTTPStatusError as exc: |
| 111 | status_code = exc.response.status_code |
| 112 | try: |
| 113 | message = exc.response.json().get("error", {}).get("message", exc.response.text) |
| 114 | except Exception: |
| 115 | message = exc.response.text |
| 116 | logger.error("Gemini API error %s: %s", status_code, message[:500]) |
| 117 | raise BackendAPIError(f"Gemini API Error ({status_code}): {message}", status_code=status_code) from exc |
| 118 | except Exception as exc: |
| 119 | logger.exception("Unexpected error during Gemini API call for model %s", target_model) |
| 120 | raise HandlerError(f"Unexpected error processing Gemini request: {exc}") from exc |
| 121 | |
| 122 | async def _stream_response(self, payload: dict): |
| 123 | try: |
nothing calls this directly
no test coverage detected