diff --git a/hud/agents/claude.py b/hud/agents/claude.py index 0489fe29..9532f1b8 100644 --- a/hud/agents/claude.py +++ b/hud/agents/claude.py @@ -4,9 +4,10 @@ import copy import logging +import requests from typing import TYPE_CHECKING, Any, ClassVar, cast -from anthropic import AsyncAnthropic, BadRequestError +from anthropic import AsyncAnthropic, BadRequestError, AuthenticationError from anthropic.types.beta import BetaContentBlockParam, BetaImageBlockParam, BetaTextBlockParam import hud @@ -209,6 +210,8 @@ async def get_response(self, messages: list[BetaMessageParam]) -> AgentResponse: raise else: raise + except AuthenticationError: + raise ValueError("Anthropic API key is set but is not valid.") messages.append( cast( diff --git a/hud/agents/openai.py b/hud/agents/openai.py index 9eb0df1a..29deabfa 100644 --- a/hud/agents/openai.py +++ b/hud/agents/openai.py @@ -3,10 +3,11 @@ from __future__ import annotations import logging +import requests from typing import Any, ClassVar, Literal import mcp.types as types -from openai import AsyncOpenAI +from openai import AsyncOpenAI, AuthenticationError from openai.types.responses import ( ResponseComputerToolCall, ResponseInputMessageContentListParam, @@ -175,14 +176,17 @@ async def get_response(self, messages: ResponseInputMessageContentListParam) -> input_param: ResponseInputParam = [{"role": "user", "content": input_content}] # type: ignore[reportUnknownMemberType] - response = await self.openai_client.responses.create( - model=self.model, - tools=[computer_tool], - input=input_param, - instructions=self.system_prompt, - truncation="auto", - reasoning={"summary": "auto"}, # type: ignore[arg-type] - ) + try: + response = await self.openai_client.responses.create( + model=self.model, + tools=[computer_tool], + input=input_param, + instructions=self.system_prompt, + truncation="auto", + reasoning={"summary": "auto"}, # type: ignore[arg-type] + ) + except AuthenticationError: + raise ValueError("OpenAI API key is set but is not valid.") else: # Follow-up step - check if this is user input or tool result latest_message = messages[-1] if messages else {}