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

Method _stream_response

api/gemini_handler.py:108–149  ·  view source on GitHub ↗
(self, payload: dict)

Source from the content-addressed store, hash-verified

106 raise HandlerError(f"Unexpected error processing Gemini request: {exc}") from exc
107
108 async def _stream_response(self, payload: dict):
109 try:
110 client = get_http_client()
111 async with client.stream("POST", GEMINI_OPENAI_COMPAT_URL, headers=self._headers(), json=payload) as response:
112 if response.status_code >= 400:
113 # Read error body while still inside the context manager (stream open).
114 try:
115 body = await response.aread()
116 detail = body.decode("utf-8", "replace")
117 except Exception:
118 detail = "<no body>"
119 logger.error("Gemini streaming API error %s: %s", response.status_code, detail[:1000])
120 yield f"data: {json.dumps({'error': f'API error ({response.status_code}): {detail[:500]}'})}\n\n"
121 return
122 async for line in response.aiter_lines():
123 if not line:
124 continue
125 if line.startswith("data: ") and line != "data: [DONE]":
126 try:
127 chunk = json.loads(line[6:])
128 for choice in chunk.get("choices", []):
129 delta = choice.get("delta", {})
130 # compat endpoint signals thought chunks via extra_content.google.thought
131 is_thought = delta.get("extra_content", {}).get("google", {}).get("thought", False)
132 if is_thought and "content" in delta:
133 text = delta.pop("content").strip("<thought>").strip("</thought>")
134 delta["reasoning"] = text
135 delta.pop("extra_content", None)
136 # also handle reasoning_content field (future-proofing)
137 elif "reasoning_content" in delta:
138 delta["reasoning"] = delta.pop("reasoning_content")
139 yield f"data: {json.dumps(chunk)}\n\n"
140 continue
141 except (json.JSONDecodeError, KeyError):
142 pass
143 yield line + "\n\n"
144 except httpx.RequestError as exc:
145 logger.error("Gemini streaming request failed: %s", exc)
146 yield f"data: {json.dumps({'error': f'Connection error: {exc}'})}\n\n"
147 except Exception as exc:
148 logger.exception("Unexpected error in Gemini streaming")
149 yield f"data: {json.dumps({'error': f'Unexpected error: {exc}'})}\n\n"

Callers 1

handle_requestMethod · 0.95

Calls 3

_headersMethod · 0.95
get_http_clientFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected