diff --git a/hud/agents/base.py b/hud/agents/base.py index e6c25b46a..89f656e33 100644 --- a/hud/agents/base.py +++ b/hud/agents/base.py @@ -937,11 +937,13 @@ def find_reward(result: MCPToolResult) -> float: if isinstance(content, types.TextContent): try: json_content = json.loads(content.text) - for key, value in json_content.items(): - if key in accept_keys: - return value except json.JSONDecodeError: - pass + continue + if not isinstance(json_content, dict): + continue + for key, value in json_content.items(): + if key in accept_keys: + return value logger.error("Couldn't parse reward from result: %s", str(result.structuredContent)) return 0.0 @@ -963,9 +965,11 @@ def find_content(result: MCPToolResult) -> str | None: if isinstance(content, types.TextContent): try: json_content = json.loads(content.text) - for key, value in json_content.items(): - if key in accept_keys: - return value except json.JSONDecodeError: - pass + continue + if not isinstance(json_content, dict): + continue + for key, value in json_content.items(): + if key in accept_keys: + return value return "" diff --git a/hud/shared/exceptions.py b/hud/shared/exceptions.py index 186e7ca8b..04d1f5386 100644 --- a/hud/shared/exceptions.py +++ b/hud/shared/exceptions.py @@ -266,19 +266,19 @@ def from_httpx_error(cls, error: httpx.HTTPStatusError, context: str = "") -> Se # Try to get detailed error info from JSON if available response_json = None + message = f"Request failed with status {status_code}" try: - response_json = response.json() + parsed = response.json() + except Exception: + parsed = None + if isinstance(parsed, dict): + response_json = parsed detail = response_json.get("detail") if detail: message = f"Request failed: {detail}" - else: - # If no detail field but we have JSON, include a summary - message = f"Request failed with status {status_code}" - if len(response_json) <= 5: # If it's a small object, include it in the message - message += f" - JSON response: {response_json}" - except Exception: - # Fallback to simple message if JSON parsing fails - message = f"Request failed with status {status_code}" + elif len(response_json) <= 5: + # Small object — include it in the message + message += f" - JSON response: {response_json}" # Add context if provided if context: