Parse agent_result that could be: 1. Direct string: "Final answer" → (reasoning="", result="Final answer") 2. JSON string: '{"reasoning": "...", "result": "..."}' → (reasoning="...", result="...")
(agent_result: Any)
| 187 | return score |
| 188 | |
| 189 | def parse_agent_result(agent_result: Any) -> Tuple[str, Any]: |
| 190 | """ |
| 191 | Parse agent_result that could be: |
| 192 | 1. Direct string: "Final answer" → (reasoning="", result="Final answer") |
| 193 | 2. JSON string: '{"reasoning": "...", "result": "..."}' → (reasoning="...", result="...") |
| 194 | """ |
| 195 | import json |
| 196 | |
| 197 | # Case 1: Direct string result |
| 198 | if isinstance(agent_result, str) and not agent_result.strip().startswith('{'): |
| 199 | return "", agent_result.strip() |
| 200 | |
| 201 | # Case 2: JSON string with reasoning and result |
| 202 | if isinstance(agent_result, str): |
| 203 | try: |
| 204 | parsed = json.loads(agent_result.strip()) |
| 205 | if isinstance(parsed, dict): |
| 206 | reasoning = parsed.get("reasoning", "") |
| 207 | result = parsed.get("result", "") |
| 208 | return reasoning, str(result) |
| 209 | except json.JSONDecodeError: |
| 210 | # If JSON parsing fails, treat as direct string |
| 211 | return "", agent_result.strip() |
| 212 | |
| 213 | # Fallback for other types |
| 214 | return "", str(agent_result) if agent_result else "" |
| 215 | |
| 216 | def create_optimizer(optimizer_type: str, reward_fn: Optional[Callable[[str, str, str], Any]] = None): |
| 217 | """Create optimizer instance based on type.""" |
no test coverage detected