MCPcopy
hub / github.com/Sophomoresty/gemini-web2api / extract_response_text

Function extract_response_text

gemini_web2api.py:320–350  ·  view source on GitHub ↗

Parse StreamGenerate response to extract final text.

(raw: str)

Source from the content-addressed store, hash-verified

318
319
320def extract_response_text(raw: str) -> str:
321 """Parse StreamGenerate response to extract final text."""
322 import re as _re
323 bard_err = _re.search(r'BardErrorInfo\s*\[(\d+)\]', raw)
324 if bard_err:
325 raise RuntimeError(f"Gemini upstream rejected request: BardErrorInfo [{bard_err.group(1)}]")
326 texts = []
327 for line in raw.split("\n"):
328 if '"wrb.fr"' not in line or len(line) < 200:
329 continue
330 try:
331 arr = json.loads(line)
332 inner_str = arr[0][2]
333 if not inner_str or len(inner_str) < 50:
334 continue
335 inner = json.loads(inner_str)
336 if isinstance(inner, list) and len(inner) > 4 and inner[4]:
337 for part in inner[4]:
338 if isinstance(part, list) and len(part) > 1 and part[1]:
339 if isinstance(part[1], list):
340 for t in part[1]:
341 if isinstance(t, str) and len(t) > 0:
342 texts.append(t)
343 except (json.JSONDecodeError, IndexError, TypeError):
344 pass
345 text = ""
346 for t in reversed(texts):
347 if t.strip():
348 text = t
349 break
350 return clean_gemini_text(text)
351
352
353# ─── OpenAI Format Helpers ───────────────────────────────────────────────────

Callers 2

_call_geminiMethod · 0.70

Calls 1

clean_gemini_textFunction · 0.85

Tested by

no test coverage detected