Mirror reasoning_content into content for clients that only read content.
(raw: bytes)
| 609 | |
| 610 | |
| 611 | def _normalize_reasoning_content_chunk(raw: bytes) -> bytes: |
| 612 | """Mirror reasoning_content into content for clients that only read content.""" |
| 613 | try: |
| 614 | text = raw.decode("utf-8") |
| 615 | except UnicodeDecodeError: |
| 616 | return raw |
| 617 | |
| 618 | lines = text.split("\n") |
| 619 | changed = False |
| 620 | for idx, line in enumerate(lines): |
| 621 | if not line.startswith("data:"): |
| 622 | continue |
| 623 | payload = line[5:].strip() |
| 624 | if not payload or payload == "[DONE]": |
| 625 | continue |
| 626 | try: |
| 627 | data = json.loads(payload) |
| 628 | except json.JSONDecodeError: |
| 629 | continue |
| 630 | choices = data.get("choices") |
| 631 | if not isinstance(choices, list) or not choices: |
| 632 | continue |
| 633 | first_choice = choices[0] |
| 634 | if not isinstance(first_choice, dict): |
| 635 | continue |
| 636 | delta = first_choice.get("delta") |
| 637 | if not isinstance(delta, dict): |
| 638 | continue |
| 639 | reasoning_content = delta.get("reasoning_content") |
| 640 | if reasoning_content and not delta.get("content"): |
| 641 | delta["content"] = reasoning_content |
| 642 | lines[idx] = f"data: {json.dumps(data, ensure_ascii=False)}" |
| 643 | changed = True |
| 644 | if not changed: |
| 645 | return raw |
| 646 | return "\n".join(lines).encode("utf-8") |
| 647 | |
| 648 | |
| 649 | def _recursion_guard_enabled(request: Request) -> bool: |