MCPcopy
hub / github.com/IBM/AssetOpsBench / _clean_final_answer

Function _clean_final_answer

src/agent/direct_llm_agent/runner.py:40–67  ·  view source on GitHub ↗

Remove blocks and keep only the final structured answer.

(text: str)

Source from the content-addressed store, hash-verified

38
39
40def _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
70class DirectLLMAgentRunner(AgentRunner):

Callers 1

runMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected