Remove blocks and keep only the final structured answer.
(text: str)
| 38 | |
| 39 | |
| 40 | def _clean_final_answer(text: str) -> str: |
| 41 | """Remove <think> blocks and keep only the final structured answer.""" |
| 42 | if not text: |
| 43 | return "" |
| 44 | |
| 45 | cleaned = text.strip() |
| 46 | |
| 47 | # Remove <think>...</think> blocks if present. |
| 48 | cleaned = re.sub( |
| 49 | r"<think>.*?</think>", |
| 50 | "", |
| 51 | cleaned, |
| 52 | flags=re.IGNORECASE | re.DOTALL, |
| 53 | ).strip() |
| 54 | |
| 55 | # Keep a trailing JSON object/array if the model added extra text. |
| 56 | json_match = re.search(r"(\{.*\}|\[.*\])\s*$", cleaned, flags=re.DOTALL) |
| 57 | if json_match: |
| 58 | cleaned = json_match.group(1).strip() |
| 59 | |
| 60 | # Canonicalize JSON if possible so the saved answer is clean and strict. |
| 61 | try: |
| 62 | parsed = json.loads(cleaned) |
| 63 | cleaned = json.dumps(parsed, ensure_ascii=False, separators=(",", ":")) |
| 64 | except json.JSONDecodeError: |
| 65 | pass |
| 66 | |
| 67 | return cleaned |
| 68 | |
| 69 | |
| 70 | class DirectLLMAgentRunner(AgentRunner): |