Skip to content

Commit 5050d86

Browse files
committed
Added request exception handling for RemoteModel.generate_response
1 parent b976764 commit 5050d86

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

ai_feedback/models/RemoteModel.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ def generate_response(
8989
filename = os.path.basename(submission_image)
9090
files[filename] = (filename, open(submission_image, 'rb'))
9191

92-
response = requests.post(self.remote_url, data=data, headers=headers, files=files)
93-
94-
return prompt, response.json()
92+
try:
93+
response = requests.post(self.remote_url, data=data, headers=headers, files=files)
94+
response.raise_for_status()
95+
response_json = response.json()
96+
except requests.exceptions.Timeout as e:
97+
raise RuntimeError("Request timed out") from e
98+
except requests.exceptions.ConnectionError as e:
99+
raise RuntimeError("Connection error") from e
100+
except requests.exceptions.HTTPError as e:
101+
# Non-2xx response
102+
status = e.response.status_code
103+
body = e.response.text
104+
raise RuntimeError(f"HTTP {status}: {body}") from e
105+
except requests.exceptions.JSONDecodeError as e:
106+
raise RuntimeError("Could not parse response") from e
107+
except requests.exceptions.RequestException as e:
108+
# Catch-all for any other requests-related errors
109+
raise RuntimeError("Request failed") from e
110+
111+
return prompt, response_json

0 commit comments

Comments
 (0)