Parse StreamGenerate response to extract final text.
(raw: str)
| 318 | |
| 319 | |
| 320 | def 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 ─────────────────────────────────────────────────── |
no test coverage detected