MCPcopy Create free account
hub / github.com/Roy3838/Observer / handle_request

Method handle_request

api/gemini_handler.py:66–106  ·  view source on GitHub ↗
(self, request_data: dict)

Source from the content-addressed store, hash-verified

64 return payload
65
66 async def handle_request(self, request_data: dict):
67 if not self.api_key:
68 raise ConfigError("GEMINI_API_KEY is not configured on the server.")
69
70 model_name = request_data.get("model")
71 if not model_name:
72 raise ValueError("Request data must include a 'model' field.")
73
74 target_model = self.model_map.get(model_name, {}).get("model_id", model_name)
75 payload = self._build_payload(request_data, target_model)
76
77 if request_data.get("stream", False):
78 return StreamingResponse(
79 self._stream_response(payload),
80 media_type="text/event-stream",
81 )
82
83 try:
84 client = get_http_client()
85 response = await client.post(GEMINI_OPENAI_COMPAT_URL, headers=self._headers(), json=payload)
86 response.raise_for_status()
87 data = response.json()
88 for choice in data.get("choices", []):
89 msg = choice.get("message", {})
90 if "reasoning_content" in msg:
91 msg["reasoning"] = msg.pop("reasoning_content")
92 return data
93 except httpx.RequestError as exc:
94 logger.error("Gemini API request failed: %s", exc)
95 raise BackendAPIError(f"Could not connect to Gemini API: {exc}", status_code=503) from exc
96 except httpx.HTTPStatusError as exc:
97 status_code = exc.response.status_code
98 try:
99 message = exc.response.json().get("error", {}).get("message", exc.response.text)
100 except Exception:
101 message = exc.response.text
102 logger.error("Gemini API error %s: %s", status_code, message[:500])
103 raise BackendAPIError(f"Gemini API Error ({status_code}): {message}", status_code=status_code) from exc
104 except Exception as exc:
105 logger.exception("Unexpected error during Gemini API call for model %s", target_model)
106 raise HandlerError(f"Unexpected error processing Gemini request: {exc}") from exc
107
108 async def _stream_response(self, payload: dict):
109 try:

Callers

nothing calls this directly

Calls 8

_build_payloadMethod · 0.95
_stream_responseMethod · 0.95
_headersMethod · 0.95
ConfigErrorClass · 0.90
BackendAPIErrorClass · 0.90
HandlerErrorClass · 0.90
get_http_clientFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected