Skip to content

Commit fefa726

Browse files
committed
answer cannot be None from LM
1 parent 52860c6 commit fefa726

5 files changed

Lines changed: 28 additions & 20 deletions

File tree

src/tfbench/lm/_anthropic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from anthropic import Anthropic
3-
from ._types import LM, LMAnswer, ReasoningEffort, EFFORT_TOKEN_MAP
3+
from ._types import LM, LMAnswer, ReasoningEffort, EFFORT_TOKEN_MAP, NoneResponseError
44
from .settings import MAX_TOKENS
55

66
CLAUDE_MODELS = [
@@ -52,7 +52,7 @@ def _gen(self, prompt: str) -> LMAnswer:
5252
break
5353

5454
if text is None:
55-
raise ValueError("No text content returned from the model.")
55+
raise NoneResponseError(self.model_name)
5656

5757
return LMAnswer(answer=text)
5858

@@ -97,7 +97,7 @@ def _gen(self, prompt: str) -> LMAnswer:
9797
if text and thinking:
9898
break
9999

100-
if text is None or thinking is None:
101-
raise ValueError("No text content returned from the model.")
100+
if text is None:
101+
raise NoneResponseError(self.model_name)
102102

103103
return LMAnswer(answer=text, reasoning_steps=thinking)

src/tfbench/lm/_google.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from google import genai
77
from google.genai.types import GenerateContentConfig, ThinkingConfig
88

9-
from ._types import LM, LMAnswer, ReasoningEffort, EFFORT_TOKEN_MAP
9+
from ._types import LM, LMAnswer, ReasoningEffort, EFFORT_TOKEN_MAP, NoneResponseError
1010

1111
GEMINI_MODELS = [
1212
"gemini-2.0-flash",
@@ -61,7 +61,10 @@ def _gen(self, prompt: str) -> LMAnswer:
6161
system_instruction=[self.instruction],
6262
),
6363
)
64-
return LMAnswer(answer=response.text)
64+
content = response.text
65+
if content is None:
66+
raise NoneResponseError(self.model_name)
67+
return LMAnswer(answer=content)
6568

6669

6770
class GeminiReasoning(LM):
@@ -105,7 +108,7 @@ def _gen(self, prompt: str) -> LMAnswer:
105108

106109
candidate = response.candidates[0]
107110
if not candidate.content or not candidate.content.parts:
108-
raise ValueError("No content in the candidate response.")
111+
raise NoneResponseError(self.model_name)
109112

110113
answer = ""
111114
thinking = ""

src/tfbench/lm/_openai.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from openai import OpenAI, NOT_GIVEN
44
from openai.types.shared.reasoning_effort import ReasoningEffort
55

6-
from ._types import LM, LMAnswer
6+
from ._types import LM, LMAnswer, NoneResponseError
77

88
OAI_MODELS = [
99
"gpt-3.5-turbo-0125",
@@ -57,8 +57,12 @@ def _gen(self, prompt: str) -> LMAnswer:
5757
model=self.model_name,
5858
)
5959

60+
content = completion.choices[0].message.content
61+
if content is None:
62+
raise NoneResponseError(self.model_name)
63+
6064
return LMAnswer(
61-
answer=completion.choices[0].message.content,
65+
answer=content,
6266
)
6367

6468

src/tfbench/lm/_types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
@dataclass
1616
class LMAnswer:
17-
answer: str | None
17+
answer: str
1818
reasoning_steps: str | None = None
1919

2020

@@ -46,3 +46,8 @@ def _gen(self, prompt) -> LMAnswer:
4646
"high": 8192,
4747
"off": 0,
4848
}
49+
50+
51+
class NoneResponseError(Exception):
52+
def __init__(self, model: str):
53+
super().__init__(f"None response from model: {model}")

src/tfbench/lm/_vllm.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@
44
from openai import OpenAI
55

66
from ..env import ENV
7-
from ._types import LM, LMAnswer
8-
9-
10-
_CHAT_TEMPLATE = """# Instructions
11-
{instruction}
12-
13-
## Task
14-
{task}
15-
"""
7+
from ._types import LM, LMAnswer, NoneResponseError
168

179

1810
class VLLMChat(LM):
@@ -64,8 +56,12 @@ def _gen(self, prompt: str) -> LMAnswer:
6456
)
6557

6658
message = completion.choices[0].message
59+
content = message.content
60+
if content is None:
61+
raise NoneResponseError(self.model_name)
62+
6763
return LMAnswer(
68-
answer=message.content,
64+
answer=content,
6965
reasoning_steps=(
7066
message.reasoning_content # type: ignore
7167
if hasattr(message, "reasoning_content")

0 commit comments

Comments
 (0)